自己学习基于docker搭建 php7.4.3+nginx+mysql 项目运行环境
自己学习了一下Centos下基于docker搭建 PHP7.4运行环境,记个笔记
首先自己弄一台服务器,我选购的学生优惠版腾讯云主机(2核2G 4M带宽,50G云盘),拿到实例之后自己选装了Centos7
一:安装docker
1:安装所需的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
2:由于官方源地址比较慢,可以自己配置源地址,这里使用阿里云源地址
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3:查询docker版本 ,可以根据自己需要的版本安装
#更新软件包版本
yum makecache fast
#查询docker版本
yum list docker-ce.x86_64 --showduplicates | sort -r #会从高到低列出Docker-ce的版本,可以选择想要安装的版本
4:安装 Docker Engine-Community
yum install docker-ce docker-ce-cli containerd.io #我这里直接安装最新版本
yum install docker-ce-18.06.3 docker-ce-cli-18.06.3 containerd.io #安装18.06.3版本的docker
5:启动docker
[root@VM-16-2-centos tzjx]# systemctl start docker #启动
[root@VM-16-2-centos tzjx]# systemctl stop docker #停止
[root@VM-16-2-centos tzjx]# systemctl start docker #启动
[root@VM-16-2-centos tzjx]# systemctl restart docker #重启
这里记录一下常用的docker 命令
[root@VM-16-2-centos tzjx]# docker images #查看已经拉取到本地的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f9c14fe76d50 8 days ago 143MB
mysql latest 05db07cd74c0 8 days ago 565MB
php 7.4-fpm 38f2b691dcb8 6 months ago 443MB
[root@VM-16-2-centos tzjx]# docker ps #查看运行中的容器
[root@VM-16-2-centos tzjx]# docker ps -a #查看运所有的容器
[root@VM-16-2-centos tzjx]# docker run :创建一个新的容器并运行
此命令要注意 以下参数选项含义
--name 容器名称
--net=service 给创建的容器指定网络组
-v 本地目录:容器目录 //目录映射
-p 本地端口:容器端口 //端口映射
-m 设置容器使用内存最大值
-d 增加一个驻留在进程中长期运行的命令
--link 可以将多个容器的网络进行连接, 比如给php容器分配了 service网络组,运行nginx容器时,可以用link 将php容器的网络加入到nginx容器
启动后并返回容器id
[root@VM-16-2-centos tzjx]# docker exec -it nginx-web /bin/bash 进入安装nginx的容器 (之后搭建环境可能用到)
[root@VM-16-2-centos tzjx]# docker exec -it php-web /bin/bash 进入安装php容器 (之后搭建环境的步骤可能用到)
二:拉取镜像并启动容器
1.访问: https://hub.docker.com 查看mysql的版本也可以通过docker search mysql搜索镜像
拉取镜像 docker pull mysql 拉取的为mysql最新版本的镜像
也可根据需要安装mysql版本,例如要安装mysql 5.7.31 docket pull mysql:5.7.31
[root@VM-16-2-centos tzjx]# docker search mysql #搜索mysql 镜像
2.安装最新mysql镜像,latest为最新的版本,下载速度一般,需要耐心等待
[root@VM-16-2-centos tzjx]# docker pull mysql
3.安装完成后,使用 docker images 查看安装到本地的镜像,可以按照此步骤安装 php 和 nginx
拉取nginx镜像和php镜像 分别执行
[root@VM-16-2-centos tzjx]# docker pull nginx
[root@VM-16-2-centos tzjx]# docker pull php:7.4-fpm
三:启动 nginx php mysql 容器
1.docker :创建service 网络组(方便后续运行php容器以及nginx容器时使用同一个网络组)
##容器网络的查看方式
[root@VM-16-2-centos tzjx]# docker network ls
##给后续的容器创建网络组
[root@VM-16-2-centos tzjx]# docker network create service
##查看网络内部信息
[root@VM-16-2-centos tzjx]# docker network inspect service
##移除指定的网络组(自己后期不想用可以rm 命令删除)
[root@VM-16-2-centos tzjx]# docker network rm service
2.创建并启动PHP容器
新建/home/docker/nginx/www目录作为宿主机项目目录,根据自己的需要做映射,也可以按照自己喜好定义其他宿主机目录作为要映射的项目根目录
docker run 时 使用-v 参数做好宿主机项目目录与php容器项目目录的映射关系
注:在使用nginx的时候必须先启动php容器,启动php容器后可以把php容器网络加入到nginx中,启动php时候可以不做9000端口映射
具体命令示例如下:(可以根据自己想映射的宿主机目录做对应调整,我这里的宿主机项目目录为 /home/docker/nginx/www)
[root@VM-16-2-centos tzjx]# docker run --name php-web --net=service -p 9000:9000 -v /home/docker/nginx/www:/var/www/html -v /home/docker/php/php.ini:/usr/local/etc/php/php.ini -d php:7.4-fpm
3.创建并启动Nginx容器
注:在创建nginx容器的时候,处理程序目录映射关系时,应该把php容器中的/var/www/html目录和nginx容器中的/usr/share/nginx/html目录映射到同一宿主机目录下(即这两容器中的项目目录都映射到宿主机的同一项目目录)
具体命令如下:
docker run -d -p 80:80 -p 443:443 --net=service --name nginx-test -v /home/docker/nginx/www:/usr/share/nginx/html -v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/docker/nginx/conf/conf.d:/etc/nginx/conf.d -v /home/docker/nginx/logs:/var/log/nginx -v /home/docker/nginx/crt:/etc/nginx/crt -v /home/docker/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf -v /home/docker/nginx/ssl:/etc/nginx/ssl -v /etc/localtime:/etc/localtime:ro --link php-web:php nginx
注意:docker ps 查看时,如果nginx容器没有运行起来,docker logs 容器id #新建容器的时候会返回容器ID 查看日志记录的错误。一般是映射关系有问题,导致无法运行nginx容器
映射配置文件的目录 在新建容器后,需要自己在宿主机目录/home/docker/nginx/conf 目录新建 nginx.conf 在/home/docker/nginx/conf/conf.d 目录新建default.conf 以及 conf 目录需要的文件,也可以通过拷贝命令直接把容器配置文件拷贝到宿主机主机对应的映射目录:举例如下
如果进入不到容器内,则可以按照下面最简单的方法随便新建一个nginx容器并启动,把配置文件拷贝出来,再删除这个容器。docker run -d --name nginx-web -p 80:80 nginx #随便创建一个容器
#例如返回容器ID cc53a678091d
docker cp cc53a678091d:/etc/nginx /home/docker/nginx/conf #拷贝该容器的nginx 配置文件到宿主机的 /home/docker/nginx/conf 目录
docker rm nginx-web #删除随手创建的这个nginx容器
注意此时宿主机需要做映射的nginx配置文件已经全都复制好了,可以 docker start 命令启动之前创建好 没有启用成功的 nginx-test 容器
4.关于nginx.conf的配置代码实例以及注释说明
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html/tzjx/public; #定义项目根目录(根据我的理解,其中/usr/share/nginx/html是php容器制定的项目目录,而后面/tzjx/public是映射的home/docker/www/tzjx/public 因为创建nginx容器时做了目录映射)
#nginx的容器的目录
index index.php index.html index.htm;
if (!-e $request_filename) {
#配置重写,url去掉index.php
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
}
}
location ~ \.php(.*)$ {
#必须是容器中的目录,而不是你挂在宿主主机的目录
root /var/www/html//tzjx/public; #定义项目根目录(根据我的理解,其中/var/www/html是nginx容器的项目目录,而/tzjx/public是映射的home/docker/www/tzjx/public 因为创建php容器时做了目录映射)
fastcgi_pass php:9000;
#如果没有在nginx容器中加入php网络,这需监听 php容器IP:9000
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
#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 /usr/share/nginx/html/tzjx;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
四.当docker中的php缺失某些扩展时,如何安装想要的扩展?
想查看php容器里有哪些扩展,可以这么操作
1.执行 docker exec -it php-web /bin/bash
2.进如php容器之后执行 php -m 查看php已经有的扩展
3.我的php缺失某些扩展时如何安装?下面我以gd扩展为例
①想要安装php的某个扩展,先执行第1布命令,进入php容器中
②编译GD库扩展
# 更新软件源:执行 apt-get update
# 安装相关库 apt-get install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev
# 解压源码 docker-php-source extract
# 切入源码文件夹 cd /usr/src/php/ext/gd
# docker-php-ext-configure gd --with-webp=/usr/include/webp --with-jpeg=/usr/include --with-freetype=/usr/include/freetype2
# make && make install
③给php安装gd扩展 docker-php-ext-install gd
给php安装pdo_mysql扩展
docker-php-ext-install pdo pdo_mysql
4.安装完扩展,docker restart php-web 重启一下php容器。phpinfo()查看新装的扩展
浙公网安备 33010602011771号