Home  >  Article  >  Backend Development  >  Detailed explanation of containerization and deployment of PHP applications

Detailed explanation of containerization and deployment of PHP applications

php中世界最好的语言
php中世界最好的语言Original
2018-05-18 14:46:182851browse

This time I will bring you a detailed explanation of the containerization and deployment of PHP applications. What are the precautions for containerization and deployment of PHP applications? The following is a practical case, let's take a look.

PHP is the best language in the world.

The classic LNMP (linux nginx php mysql) environment has many ready-made deployment scripts, but today when Docker is popular, many students still have some problems on how to deploy, so this article is simple Introduce how to use Docker and docker-compose to deploy php applications on the server.

First of all, let’s review the past php configuration 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;
}
All php files are parsed using the php engine, and php runs on the local port 9000 (accessible through ip/unix domain sockets) , since it can be local, of course it can also be parsed through 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;
 }
}
At the same time, a more difficult problem is that the php code needs to exist in both nginx and php engines, otherwise it cannot be parsed, but this It’s not a problem, docker’s volume naturally supports it!

Look directly at the docker-compose.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
Note:

php-fpm mounts the local directory code to /var/www/html

nginx conf uses service_name to access php-fpm
nginx shares php-fpm's /var/www/html through the volumes_from directive
Write this in index.php in the code directory:

<?php
echo phpinfo();
Then run:

# bash
docker-compose up
Open the browser and you can see the familiar phpinfo:

Then the question is coming, some friends want to ask it , what to do with dependencies? Okay, that's what I'm going to go ahead and say.

Put a Dockerfile in your project:

FROM php:7.1-fpm-alpine
RUN docker-php-install pdo pdo-mysql
COPY src /var/www/html
To build, you can choose the Alibaba Cloud Image Service build function or the automatic build provided by Docker, and then update the previous docker-compose.yml That’s it:

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
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of php namespace

##How to convert Chinese characters and pinyin with PHP Chinese tool class ChineseUtil

The above is the detailed content of Detailed explanation of containerization and deployment of PHP applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn