Docker容器搭建NGINX与PHP环境详细配置与演示

一、资源编排

1、操作系统:CentOS Linux 7 (Core)

2、Docker容器版本:Docker version 20.10.21, build baeda1f

3、NGINX版本:NGINX 1.22

4、PHP版本:7.4

5、PHP程序包:易优eyou官网某模板站点源码

二、拉取基础镜像

[root@website ~]# docker pull php:7.4-fpm
[root@website ~]# docker pull nginx:1.22

三、创建PHP容器相关配置文档

1、PHP程序涉及到容器与宿主机映射关系的文件通常有三个,分别在容器的绝对路径下:

[root@website ruiside]# docker cp php:/usr/local/etc/php-fpm.d/www.conf php/
[root@website ruiside]# docker cp php:/usr/local/etc/php/php.ini-production php/php.ini
[root@website ruiside]# docker cp php:/usr/local/etc/php-fpm.conf php/

2、启动PHP容器

[root@website ruiside]# docker run -p 9000:9000 -itd --name php -v /usr/local/ruiside/nginx/html:/usr/share/html -v /usr/local/ruiside/php/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /usr/local/ruiside/php/php.ini:/usr/local/etc/php/php.ini -v /usr/local/ruiside/php/php-fpm.conf:/usr/local/etc/php-fpm.conf --privileged=true -d php:7.4-fpm

3、验证容器

四、创建NGINX容器相关配置文档

1、NGINX容器启动映射关系涉及到三个方面

[root@website ruiside]# docker cp nginx:/etc/nginx/conf.d/default.conf nginx/

2、编辑default.conf文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
            if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?s=/$1 last;
        break;
      }
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

3、启动NGINX容器

docker run --name nginx -p 80:80 -itd -v /usr/local/ruiside/nginx/html:/usr/share/nginx/html -v /usr/local/ruiside/nginx/conf.d:/etc/nginx/conf.d/ -v  /usr/local/ruiside/nginx/logs:/var/log/nginx --link php:php --privileged=true -d nginx:1.22

4、验证NGINX

五、发布web站点

访问地址:http://192.168.100.239