Skip to content

How to setup PHP easily on your machine

Posted on:December 1, 2023 at 03:22 PM

My goal with this post is you have a php enverioment running on your machine quickly and easy. I saw some people strogoling to have this done looking for tutorials that you need to follow a bunch of steps for the end you could run php just on command line :/ .

On the sections bellow you will find a setup with one copy and past command and you have this running with php and database and a small php structure for you use as study guide in case you are new on php land.

Requirements

TBefore you run the command to setup php on your localhost, you MUST have two tools already installed on your computer.

  1. Git
  2. Docker

You must also be able to run docker wihout 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 requeriments 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

Troubles

In case you face the error bellow on your terminal.

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

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 to 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

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 php mordem frameworks.

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

But if you find some strange behavour feel free to open an issue or even better provide a PR to improve the project 🚀.

Study with the current structure

As you 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 to also give you a code that you could use to investige what is happening and study.

Then I can say it is not 100% perfect But I want to add more doc and good examples into the code, then be tuned 😎.