> For the complete documentation index, see [llms.txt](https://docs.st0722.top/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.st0722.top/web/docker/pei-zhi-nginx+php-huan-jing.md).

# 配置Nginx+PHP环境

用docker搭建一个完整的HTTP网页服务

原教程CSDN地址：<https://blog.csdn.net/Srodong/article/details/105030387>

部署步骤：

### 安装Nginx

`docker pull nginx`

`docker run --name nginx -p 8080:80 -d nginx`

参数说明：

* \--name nginx：容器名称。
* -p 8080:80： 端口进行映射，将本地 8080 端口映射到容器内部的 80 端口。
* -d nginx： 设置容器在在后台一直运行。

### 安装php <a href="#c2-a0-e5-ae-89-e8-a3-85php" id="c2-a0-e5-ae-89-e8-a3-85php"></a>

`docker pull php:5.6-fpm`

`docker run --name myphp-fpm -v ~/nginx/www:/www -d php:5.6-fpm`

参数说明：

1. \--name myphp-fpm : 将容器命名为 myphp-fpm。
2. -v \~/nginx/www:/www: 将主机中项目的目录 www 挂载到容器的 /www
3. 我也不知道为什么这里原作者要指定版本5.6，到我写这个教程的23年底PHP版本已经更新到了8.2，不知道为何不用新版，有兴趣的可以把:5.6删掉试试

创建 \~/nginx/conf/conf.d目录：

`mkdir ~/nginx/conf/conf.d`

P.S.我在用这条命令创建的时候失败了，不知道什么原因，后来是自己手动新建文件夹的

在该目录下添加 \~/nginx/conf/conf.d/runoob-test-php.conf 文件，内容如下：&#x20;

```
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;
    }
 
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
```

配置文件说明：

* php:9000: 表示 php-fpm 服务的 URL，下面我们会具体说明。
* /www/: 是 myphp-fpm 中 php 文件的存储路径，映射到本地的 \~/nginx/www 目录。

启动 nginx：

```sh
docker run --name runoob-php-nginx -p 8083:80 -d \
    -v ~/nginx/www:/usr/share/nginx/html:ro \
    -v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
    --link myphp-fpm:php \
    nginx
```

* -p 8083:80: 端口映射，把 nginx 中的 80 映射到本地的 8083 端口。
* \~/nginx/www: 是本地 html 文件的存储目录，/usr/share/nginx/html 是容器内 html 文件的存储目录。
* \~/nginx/conf/conf.d: 是本地 nginx 配置文件的存储目录，/etc/nginx/conf.d 是容器内 nginx 配置文件的存储目录。
* \--link myphp-fpm:php: 把 myphp-fpm 的网络并入 *nginx*，并通过修改 nginx 的 /etc/hosts，把域名 php 映射成 127.0.0.1，让 nginx 通过 php:9000 访问 php-fpm。

接下来我们在 \~/nginx/www 目录下创建一个示例PHP来检查PHP环境运行是否正确：index.php代码如下：

```php
<?php phpinfo(); ?>
```

如果PHP环境正确运行则会显示一个PHP配置信息表。

然后网络环境就搭建好了，你就可以在这个目录下上传你的PHP页面了，访问ip:8083就可以看到你的PHP页面，当然最好用上面的任何一种方法进行反代使你可以用域名访问并添加HTTPS
