Skip to content

A beginner's guide to setting up PHP on your computer

Published:ย atย 10:00 AM

My goal with this post is you have a PHP environment running on your machine quickly and easy. I saw some people struggling to have this done, and tutorials that you need to follow a bunch of steps for the end, you could run PHP on command line only ๐Ÿ˜”.

But on sections bellow, you going to find a setup with one copy and past command, and you have a complete php stack running with a small php structure for you use as study guide in case, you are new on php land.

Requirements

Before we start, you MUST have two tools already installed on your computer.

  1. Git
  2. Docker

You must also be able to run docker without sudo, in case you didnโ€™t have setup non root, check this article.

Check if it is working properly running the commands bellow ๐Ÿ‘‡

git --version
docker compose version
# You gonna see some thing an output like this.
# git version 2.39.2 (Apple Git-143)
# Docker Compose version v2.23.0-desktop.1

For Windows users I would say that you must install WSL to have this working.

Letโ€™s make this work ๐Ÿš€

After you have done the requirements step, we can now run command to have this ready to play with php ๐Ÿ˜.

Copy and past the command bellow inside of your project folder.

Note: You donโ€™t need to run line by line, just copy the whole code block bellow and past on your terminal.

git clone https://github.com/shield-wall/easy-php-setup.git \
&& cd easy-php-setup \
&& docker compose up -d --wait \
&& docker compose exec php composer install \
&& mkdir var \
&& mkdir var/cache \
&& chmod 777 var/cache \
&& sleep 10 \
&& docker compose exec -T db sh -c 'exec mysql --defaults-extra-file=.docker/mysql/config.cnf' < .docker/mysql/dump.sql

Troubleshooting

In case the error bellow pop up on your terminal.

# ERROR 2002 (HY000): Can't connect to local MYSQL server through socket '/var/run/mysqld/mysqld.sock' (2)

No worries, it is an easy issue to solve ๐Ÿ˜‰.

The reason for this is because your database is not up yet ๐Ÿ˜ฅ, But it is fine you just need to wait few minutes or run the command bellow and check if it is up.

docker compose ps db
# You should wait to see the status "Up", It could take 1 or 2 minutes depends of your machine.
# easy-php-setup-db-1   mysql     "docker-entrypoint.sโ€ฆ"   db        2 days ago   Up 30 hours   3306/tcp, 33060/tcp

After you see the db container Up, Run the command bellow.

docker compose exec -T db sh -c 'exec mysql --defaults-extra-file=.docker/mysql/config.cnf' < .docker/mysql/dump.sql

In case you find out other issue that is not related here, open an issue on github.

Commands

CommandDescription
docker compose up -dStart the containers
docker compose downStop containers
docker compose exec php composer installExecute some php command that you need, or composer command.

Frameworks

This project should work, out the box, for the most modern php frameworks such as Symfony and Laravel.

In case you just want to use the environment, you can remove all files and keep .docker and docker-compose.yml only.

But if you find some strange behaviour, feel free to open an issue or even better provide a PR to improve the project ๐Ÿš€.

Study guide

As you can see I added a tiny php structure, I did this for two reasons.

  1. I donโ€™t want to give you a poor example doing everything in one single file, and you think that is the way that we use in nowadays.
  2. I want also give you a code, that you could use to investigate what is happening and study.

Then I would say it is not 100% perfect, But I want to add more doc and good examples into the code, then stay tuned ๐Ÿ˜Ž.


Previous Post
How to enhance final classes from open source code with decorator pattern