PHP is the best language in the world.

The classic LNMP (Linux + Nginx + PHP + mysql) environment has a lot of ready-made deployment scripts, but in today’s popular Docker, there are still many students have some problems in how to deploy. Docker and Docker-compose compose to deploy PHP applications on the server.

Let’s start with a review of past PHP configurations in Nginx:

location ~ \.php$ {
    try_files       $uri =404;
    include         fastcgi_params;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Copy the code

All PHP files are parsed using the PHP engine, which runs on local 9000 ports (accessible via IP/Unix Domain Sockets), and since it can be parsed locally, it can also be parsed via remote services.

So our nginx service is configured as follows:

server { listen 80; charset utf-8; # access_log /var/log/nginx/nginx.access.log main; # error_log /var/log/nginx/error.log; root /var/www/html; index index.php index.html; add_header X-Cache $upstream_cache_status; location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass php-fpm:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }}Copy the code

In addition, there is a problem that PHP code needs to exist in nginx and PHP engine, otherwise it will not be able to parse, but this is not a problem, docker volume naturally support!

Look directly at the docker-comemage.yml configuration below:

version: '2'
services:
    nginx:
        image: nginx:stable-alpine
        ports:
          - 80:80
        volumes:
            - ./conf/nginx/conf.d:/etc/nginx/conf.d
        volumes_from:
          - php-fpm
        restart: always

    php-fpm:
        image: php:7.1-fpm-alpine
        volumes:
            - ./code:/var/www/html
        restart: always
Copy the code

Note:

  • Php-fpm mounts the local directory code to /var/www/html
  • Nginx conf uses service_name to access phP-fpm
  • Nginx shares the /var/www/html of php-fpm with the volumes_from directive

In code directory index. PHP:

<? php echo phpinfo();Copy the code

Then run:

# bash
docker-compose up
Copy the code

Open the browser to see the familiar phpInfo:


So the question comes, there is a small partner to ask, how to rely on? Okay, so that’s what I’m going to talk about.

Put a Dockerfile in your project:

FROM PHP :7.1-fpm-alpine RUN docker-php-install pdo pdo mysql COPY SRC /var/www/htmlCopy the code

Docker-comemage.yml = docker-comemage.yml = docker-comemage.yml = docker-comemage.yml = docker-comemage.yml = docker-comemage.yml

version: '2'
services:
    nginx:
        image: nginx:stable-alpine
        ports:
          - 8000:80
        volumes:
            - ./conf/nginx/conf.d:/etc/nginx/conf.d
        volumes_from:
          - php-fpm
        restart: always

    php-fpm:
        image: {YOUR_PHP_IMAGE_NAME}:{TAG}
        restart: always
Copy the code

This sample code:

ImplementsIO/docker-labsgithub.com

More examples of ImplementsIO/ Docker-Labs for more examples of ImplementsIO/ Docker-Labs for more examples of ImplementsIO and Deployment for Star in the following project:

ImplementsIO/docker-labsgithub.com