I was recently approached by a colleague who asked me to add some functionality to a PHP site I wrote a long time ago. I was developing a native development environment built using XAMPP, but now my laptop has been updated and I don’t have the development environment at that time. In line with the principle of not loading useless software on the computer as far as possible, I can think of using Docker to build a local development environment, so this article introduces how to quickly build a local PHP development environment based on Docker, for the reference of students who need to use.
directory
- preparation
- Format file
- Running effect
- Install the extension
- The resources
This paper builds PHP development environment under Mac based on 5.6-FPM-ALpine3.8 and NGingx.
preparation
First download the required image file
$docker pull PHP :5.6- FPM - alPINE3.8 $docker pull nginx $docker pull mysqlCopy the code
Users need to go to hub.docker.com, search PHP and find the version they want to install through tags. My project cannot support the latest PHP because it has been developed for a long time, so I installed version 5.6.
We need to prepare a working directory, such as LNMP, in the working directory to prepare the website root directory, Nginx configuration file directory, Nginx log directory.
$ mkdir lnmp
$ cd lnmp
$ mkdir -p nginx/www nginx/logs nginx/conf
Copy the code
Create a new index. PHP file in the new WWW directory to check whether the PHP environment is set up successfully.
phpinfo();
? >
Copy the code
Prepare the nginx configuration file php.conf in the nginx/conf directory.
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name; include fastcgi_params; }}Copy the code
Format file
Create the docker-comemess. yml orchestration file in your working directory.
For a detailed introduction to docker-compose, please refer to my previous article on docker-compose usage.
version: "2.1"
services:
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ~/Projects/sh-valley/docker-conf/lnmp/nginx/www:/usr/share/nginx/html
- ~/Projects/sh-valley/docker-conf/lnmp/nginx/conf:/etc/nginx/conf.d
- ~/Projects/sh-valley/docker-conf/lnmp/nginx/logs:/var/log/nginx
networks:
- lnmp-network
php:
image: PHP: 5.6 - FPM - alpine3.8
volumes:
- ~/Projects/sh-valley/docker-conf/lnmp/nginx/www:/www
networks:
- lnmp-network
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
networks:
- lnmp-network
networks:
lnmp-network:
Copy the code
At this point, all the preparations are complete and we are ready to start and see the results.
Running effect
$ docker-compose up -d
Creating network "lnmp_php-network" with the default driver
Creating lnmp_nginx_1 ... done
Creating lnmp_php_1 ... done
Copy the code
You’ll soon see the familiar PHPInfo interface.
Install the extension
The default PHP image provides few extensions, missing common extensions such as mysql and GD2, so we need to install and enable extensions ourselves.
First go into the PHP container and use the php-m command to see what extensions are available locally.
You can install the extension using the docker-php-ext-install command.
$ docker-php-ext-install mysql
Copy the code
Once installed, the extension can be enabled in php.ini. We can see from phpInfo that php.ini is not enabled by default in the container environment. We can copy php.ini-development to php.ini from /usr/local/etc/ PHP. Enable the extensions you need by modifying the configuration in php.ini. Here are a few extension installation commands for your reference.
docker-php-ext-source
Create one in the container/usr/src/php
directorydocker-php-ext-enable
Enable PHP extensions that save us manual editingphp.ini
The process ofdocker-php-ext-install
Install and enable the PHP extensionsdocker-php-ext-configure
Often withdocker-php-ext-install
Collocation, used when a custom extension configuration is required
The resources
- Docker seconds build Php7 development environment
- Docker-compose usage introduction
- Docker PHP
- Docker complete configuration nginx+ PHP +mysql
- Docker PHP installation extension steps in detail