SoFunction
Updated on 2025-03-10

Use docker to build the entire process of php7 and nginx running environment (official mirror)

This article shares the entire process of using docker to build the operating environment of php7 and nginx. It is shared for your reference and learning. Let’s take a look at the detailed introduction below:

Environment introduction

Root directory: /docker

Website root directory: /docker/www

nginx related directory: /docker/nginx/

Preparation

1. Use docker accelerator

curl -sSL /daotools/set_mirror.sh | sh -s 
service docker restart

2. Download related images

docker pull nginx
docker pull php:7.1.0-fpm

3. Create relevant directories

mkdir -p /docker/www
mkdir -p /docker/nginx/

4. Edit

vim /docker/nginx//

# The following is the example contentserver {
  listen  80 default_server;
  server_name _;
  root   /usr/share/nginx/html;

  location / {
   index   ;
   autoindex off;
  }
  location ~ \.php(.*)$ {
   root   /var/www/html/;
   fastcgi_pass 172.17.0.2:9000;
   fastcgi_index ;
   fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param PATH_INFO $fastcgi_path_info;
   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
   include  fastcgi_params;
  }
}

Building an environment

1. Start PHP image

docker run -p 9000:9000 --name myphp \
-v /docker/www/:/var/www/html/ \
--privileged=true \
-d php:7.1.0-fpm

#View the IP address of the php imagedocker inspect --format='{{.}}' myphp

172.17.0.2

#Modify the configuration file so that the value of fastcgi_pass is 172.17.0.2:9000
vim /docker/nginx//

fastcgi_pass 172.17.0.2:9000;

2. Start nginx image

docker run -p 80:80 --name mynginx \
-v /docker/www:/usr/share/nginx/html \
-v /docker/nginx/:/etc/nginx/ \
--privileged=true \
-d nginx

3. Check the running status of the mirror

docker ps

CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS          NAMES
93213e1eac73  nginx    "nginx -g 'daemon off" 3 seconds ago  Up 2 seconds  0.0.0.0:80->80/tcp mynginx
e93281652098  php:7.1.0-fpm  "docker-php-entrypoin" 8 minutes ago  Up 8 minutes  0.0.0.0:9000->9000/tcp      myphp

4. Generate php test files

echo "<?php phpinfo();" > /docker/www/

Browser access http://localhost/ Verification

nginx virtual machine configuration

Taking configuring a virtual machine as an example, the project directory address is /docker/www/

vim /docker/nginx//

# The example content is as follows
server {
  listen  80;
  server_name ;
  root   /usr/share/nginx/html//;
  location / {
   index   ;
   autoindex off;
  }
  location ~ \.php(.*)$ {
   root   /var/www/html//;
   fastcgi_pass 172.17.0.2:9000;
   fastcgi_index ;
   fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param PATH_INFO $fastcgi_path_info;
   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
   include  fastcgi_params;
  }
}


#Restart nginx image
docker restart mynginx

Commonly used docker commands

1. Stop all running containers

docker kill $(docker ps -a -q)

2. Delete all containers that have stopped running

docker rm $(docker ps -a -q)

3. Check the container running status

docker stats

4. Enter the container for command line operations

docker exec -it content-name-or-id /bin/bash

Frequently Asked Questions

In CentOS7 environment, because of the host's SELINUX, the configuration file () cannot be accessed in the nginx container, and the container cannot provide web services.

Solution:

#########################################Switch off SELINUX on host host#Temporary Closesetenforce 0
#Permanently close Modify /etc/sysconfig/selinux fileSELINUX=disabled

#################### Method 2#####################Run containers in privileged way#--privileged parameter is truedocker run -it --privileged=true -d nginx

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.