CentOS7环境下使用Docker搭建PHP运行环境的过程详解

相关文章:
CentOS7下使用yum安装Docker
Win10环境下使用Docker搭建PHP运行环境
1、创建私有网络

docker network create lnmp

私有网络创建成功:
CentOS7环境下使用Docker搭建PHP运行环境的过程详解
文章图片

2、安装Nginx
镜像地址:https://hub.docker.com/_/nginx?tab=tags
CentOS7环境下使用Docker搭建PHP运行环境的过程详解
文章图片

可以安装最新版Nginx,这里通过搜索标签,拉取 Nginx1.18.0 镜像:
docker pull nginx:1.18.0

使用 docker images 命令查看 安装Nginx镜像成功:
CentOS7环境下使用Docker搭建PHP运行环境的过程详解
文章图片

运行Nginx:
#运行容器docker run --name nginx -p 8080:80 -v /root/docker/nginx/html:/usr/share/nginx/html -d nginx:1.18.0 #移动到配置目录cd /root/docker/nginx #复制配置文件docker cp nginx:/etc/nginx/conf.d conf.d #停止容器docker stop nginx #删除容器 docker rm nginx #再次运行docker run --name nginx -p 8080:80 --network lnmp -v /root/docker/nginx/html:/usr/share/nginx/html -v/root/docker/nginx/conf.d:/etc/nginx/conf.d/ -d nginx:1.18.0

测试:在Nginx站点根目录html目录下创建index.html文件,并写入文字:
echo "Nginx Server" >> /root/docker/nginx/html/index.html

浏览器访问主机地址 127.0.0.1:8080 如下,则Nginx安装成功:
CentOS7环境下使用Docker搭建PHP运行环境的过程详解
文章图片

3、安装MySQL
镜像地址:https://hub.docker.com/_/mysql?tab=tags ,这里拉取MySQL5.7.34镜像:
docker pull mysql:5.7.35

运行MySQL:
docker run --name mysql5.7 --network lnmp -v /root/docker/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d --privileged=true mysql:5.7.35

4、安装PHP
镜像地址:https://hub.docker.com/_/php?tab=tags,这里拉取PHP7.4镜像:
docker pull php:7.4-fpm

运行PHP:
#运行镜像docker run --name php7.4 --network lnmp -d php:7.4-fpm #创建目录mkdir -p /root/docker/php #移动目录cd /root/docker/php/ #复制www.confdocker cp php7.4:/usr/local/etc/php-fpm.d/www.conf www.conf #进入容器docker exec -it php7.4 bash #移动目录cd /usr/src/ #解压文件xz -d php.tar.xz #解压文件tar -xvfphp.tar #退出镜像exit #复制php.inidocker cp php7.4:/usr/src/php-7.4.22/php.ini-production php.ini #停止镜像docker stop php7.4 #删除镜像docker rm php7.4 #再次运行镜像docker run --name php7.4 --network lnmp -v /root/docker/nginx/html:/var/www/html -v /root/docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /root/docker/php/php.ini:/usr/local/etc/php/php.ini -d php:7.4-fpm

编辑Nginx配置文件 vim /root/docker/nginx/conf.d:
server {listen80; server_namelocalhost; #charset koi8-r; #access_log/var/log/nginx/log/host.access.logmain; location / {root /usr/share/nginx/html; index index.php index.html index.htm; try_files $uri $uri/ =404; } error_page404/404.html; location = /40x.html {root/user/share/nginx/html; } # redirect server error pages to the static page /50x.html#error_page500 502 503 504/50x.html; location = /50x.html {root/usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ \.php$ {root/var/www/html/; fastcgi_passphp7.4:9000; fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; includefastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }}

创建index.php文件:vim /root/docker/nginx/html/index.php

重启Nginx镜像:(进程ID通过 docker ps 命令查看)
docker restart 43aea5a90446

此时浏览器访问主机地址 127.0.0.1:8080 如下,则PHP安装成功:
CentOS7环境下使用Docker搭建PHP运行环境的过程详解
文章图片

【CentOS7环境下使用Docker搭建PHP运行环境的过程详解】到此这篇关于CentOS7环境下使用Docker搭建PHP运行环境的文章就介绍到这了,更多相关Docker搭建PHP运行环境内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读