使用docker搭建nginx+php7.4环境

docker 安装nginx

  • 我们可以使用docker search nginx命令查找 Docker Hub 上的 nginx 镜像,这里直接拉取官方的镜像

    [yaoyuan@VM-20-5-centos ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    5eb5b503b376: Pull complete 
    1ae07ab881bd: Pull complete 
    78091884b7be: Pull complete 
    091c283c6a66: Pull complete 
    55de5851019b: Pull complete 
    b559bad762be: Pull complete 
    Digest: sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
  • 等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

    [yaoyuan@VM-20-5-centos ~]$ docker images
    REPOSITORY                    TAG       IMAGE ID       CREATED        SIZE
    2206691630/category-service   latest    cf2743298db3   11 hours ago   38.5MB
    nginx                         latest    c316d5a335a5   2 weeks ago    142MB
    cap1573/consul                latest    80a8d3400e10   2 months ago   124MB
    wangbinxingkong/fast          latest    878667fbb389   6 months ago   51.4MB
  • 使用nginx镜像开启nginx应用容器

    [yaoyuan@VM-20-5-centos docker_data]$ pwd
    /data/docker_data
    [yaoyuan@VM-20-5-centos docker_data]$ docker run -p 80:80 -d --name nginx_wwwz88jcom  -v /data/docker_data/nginx/www:/usr/share/nginx/www nginx
    3968ad4b0bd400209d97ac69ae19be9e8598f94f54ef1c5577849b18729e3397

    -p 80:80:将容器的80端口映射到主机的80端口
    -d 后台运行(守护进程)
    --name mynginx:将容器命名为mynginx
    -v 将主机中当前目录下的www挂载到容器的www目录

  • 查看容器启动情况

    [lighthouse@VM-20-5-centos www]$ docker ps -a
    CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS         PORTS                                                                      NAMES
    3968ad4b0bd4   nginx                         "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp                                                         nginx_wwwz88jcom
    2be397ea0633   2206691630/category-service   "/category-service"      11 hours ago    Up 11 hours    0.0.0.0:8082->8082/tcp                                                     peaceful_shockley
    9d5cb1e98e9b   cap1573/consul:latest         "docker-entrypoint.s…"   19 hours ago    Up 19 hours    8300-8302/tcp, 8301-8302/udp, 8600/tcp, 8600/udp, 0.0.0.0:8500->8500/tcp   consul_server_1
    e60dc0bdfe23   wangbinxingkong/fast:latest   "/fast/dockercurl"       20 hours ago    Up 19 hours    0.0.0.0:8081->8081/tcp                                                     friendly_tu
  • 通过浏览器访问,运行成功

Docker 安装 PHP

  • 和nginx一样,我们可以先通过docker search php查找镜像,这里直接拉取官方的镜像,标签为7.1-fpm,其他版本的可自行选择
[yaoyuan@VM-20-5-centos www]$ docker pull php:7.4-fpm
7.4-fpm: Pulling from library/php
5eb5b503b376: Already exists 
8b1ad84cf101: Pull complete 
38c937dadeb7: Pull complete 
6a2f1dc96e59: Pull complete 
2759fc508f65: Pull complete 
f23e54c4f34c: Pull complete 
a9145be66a0f: Pull complete 
47923ea476fc: Pull complete 
309c9a173313: Pull complete 
949f215c5eff: Pull complete 
Digest: sha256:89f487558afd63a5d73676e0bad86d030dcd2d3a3ab176f7a6c5af3650522ab7
Status: Downloaded newer image for php:7.4-fpm
docker.io/library/php:7.4-fpm
  • 等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为php,标签为7.1-fpm的镜像。
[yaoyuan@VM-20-5-centos www]$ docker images
REPOSITORY                    TAG       IMAGE ID       CREATED        SIZE
2206691630/category-service   latest    cf2743298db3   11 hours ago   38.5MB
php                           7.4-fpm   b57d91023619   2 weeks ago    460MB
nginx                         latest    c316d5a335a5   2 weeks ago    142MB
cap1573/consul                latest    80a8d3400e10   2 months ago   124MB
wangbinxingkong/fast          latest    878667fbb389   6 months ago   51.4MB
  • 使用php镜像开启php-frm应用容器
[yaoyuan@VM-20-5-centos www]$ docker run -p 9000:9000 -d --name php-fpm_wwwz88jcom -v /data/docker_data/nginx/www:/usr/share/nginx/www php:7.4-fpm
268d0ec3a9975f140adf67531d6c4f0cb3400747a16239e0d47c8b730c4421ef
[yaoyuan@VM-20-5-centos www]$ 

-p 9000:9000 :将容器的9000端口映射到主机的9000端口
-d 后台运行(守护进程)
--name myphp-fpm:将容器命名为myphp-fpm
-v 将主机中当前目录下的www挂载到容器的www目录

  • 查看容器启动情况
[yaoyuan@VM-20-5-centos www]$ docker ps -a
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS                                                                      NAMES
268d0ec3a997   php:7.4-fpm                   "docker-php-entrypoi…"   2 minutes ago    Up 2 minutes    0.0.0.0:9000->9000/tcp                                                     php-fpm_wwwz88jcom
3968ad4b0bd4   nginx                         "/docker-entrypoint.…"   15 minutes ago   Up 15 minutes   0.0.0.0:80->80/tcp                                                         nginx_wwwz88jcom
2be397ea0633   2206691630/category-service   "/category-service"      11 hours ago     Up 11 hours     0.0.0.0:8082->8082/tcp                                                     peaceful_shockley
9d5cb1e98e9b   cap1573/consul:latest         "docker-entrypoint.s…"   19 hours ago     Up 19 hours     8300-8302/tcp, 8301-8302/udp, 8600/tcp, 8600/udp, 0.0.0.0:8500->8500/tcp   consul_server_1
e60dc0bdfe23   wangbinxingkong/fast:latest   "/fast/dockercurl"       20 hours ago     Up 19 hours     0.0.0.0:8081->8081/tcp                                                     friendly_tu

到这里,可以看到nginx和php都运行成功(STATUS为up说明正在运行)

  • 查看ip信息
    [yaoyuan@VM-20-5-centos www]$ docker inspect php-fpm_wwwz88jcom | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.6",
                    "IPAddress": "172.17.0.6",

修改nginx相关配置

容器里面是没有vim,不能直接修改配置文件。当然我们也可以在容器里面安装vim,这里介绍另一种方法

  • 进入正在运行的nginx容器
[yaoyuan@VM-20-5-centos www]$ docker exec -it nginx_wwwz88jcom bash
root@3968ad4b0bd4:/# 

配置文件地址 /etc/nginx/conf.d/default.conf

root@3968ad4b0bd4:/# cd /etc/nginx/conf.d/
root@3968ad4b0bd4:/etc/nginx/conf.d# ls
default.conf
root@3968ad4b0bd4:/etc/nginx/conf.d# 
  • 然后使用ctrl + p + q退出容器,使用exit的话会让容器停止。
  • 使用专用的复制命令将配置文件复制到宿主机,然后在宿主机进行编辑
[yaoyuan@VM-20-5-centos conf.d]$ sudo docker cp nginx_wwwz88jcom:/etc/nginx/conf.d/default.conf /data/docker_data/nginx/conf.d/default.conf
[yaoyuan@VM-20-5-centos conf.d]$ 
  • 在宿主机修改配置文件的php部分,内容如下:

    location ~ \.php$ {
    fastcgi_pass   172.17.0.6:9000; 
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/www$fastcgi_script_name;
    fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
    include        fastcgi_params;
    }

    其中的ip也可以修改为宿主机的ip或者上面docker命令查看的ip。

  • 然后使用复制命令将宿主机的文件复制到容器

    [yaoyuan@VM-20-5-centos conf.d]$ docker cp /data/docker_data/nginx/conf.d/default.conf nginx_wwwz88jcom:/etc/nginx/conf.d/default.conf
  • 进入容器中,重新载入nginx配置文件

[yaoyuan@VM-20-5-centos conf.d]$ docker exec -it nginx_wwwz88jcom bash
root@3968ad4b0bd4:/# service nginx reload
Reloading nginx: nginx.

大功告成

使用docker搭建nginx+php7.4环境

如果php后续需要安装扩展。

https://hub.docker.com/_/php?tab=description 可以去docker php介绍里面看看。里面有开启扩展的方法

pecl install redis-5.1.1 #安装扩展
docker-php-ext-enable redis #开启扩展

原创文章,作者:星辰,如若转载,请注明出处:http://www.z88j.com/39278.html

(12)
打赏 微信扫一扫 微信扫一扫
上一篇 2022年2月10日 下午4:58
下一篇 2022年2月21日 下午5:33

相关推荐

发表回复

登录后才能评论