Docker部署nginx+php环境

如果还没有安装docker可以看这篇文章:

docker离线安装包 + shell脚本一键安装docker-CSDN博客文章浏览阅读217次,点赞4次,收藏4次。摘要:帮助小白,快速部署docker容器。https://blog.csdn.net/qq_58733481/article/details/134579640?spm=1001.2014.3001.5501

环境(以下内容是连接外网的情况下进行操作)

系统linux
发行版本Centos 8 / 64位
docker24.0.6
nginxlatest
php7.4

1、安装nginx

docker pull nginx:latest

2、安装PHP

docker pull php:7.4-fpm

3、 启动PHP容器

docker run -d --name myphp-fpm -v /docker/nginx/www:/www 38f2b691dcb8

        注: myphp-fpm:是我给php容器起的名字

                /docker/nginx/www是我在本地创建的一个文件目录,用来对应docker的www

                ef0ed3bc573e 是php的镜像ID。

                -d 后台运行

                -v  挂载文件 实现服务器和容器之间文件共享

4、启动Nginx容器

docker run --name nginx -p 8888:80 -d -v /docker/nginx/www:/usr/share/nginx/html -v /docker/nginx/conf/conf.d:/etc/nginx/conf.d --link myphp-fpm:php 2a36393edaf1

        注:--name 容器名字

                -p 服务器和容器之间端口映射。

                -d 后台运行

                -v 数据卷挂载(实现文件共享)

                --link 关联容器

5、将配置文件和网页文件拷贝到nginx容器内 (index.php  default.conf)

docker cp /docker/nginx/www/index.php nginx:/usr/share/nginx/html

docker cp /docker/nginx/conf/conf.d/default.conf nginx:/etc/nginx/conf.d

5.1、index文件内容

<?php
    echo phpinfo();
?>

5.2 default.conf文件内容

server {
    listen       80;
    server_name  localhost;
 
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
#fastcgi_pass 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass   myphp-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

6、验证部署情况

浏览器访问:http://{ip}:8888/index.php

服务器中访问:curl http://localhost:8888

提示:若出现无法页面等情况,记得把服务器防火墙给关掉或者开放端口哦。

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值