Skip to main content

11.基于docker搭建php环境

lnmplamp是个非常了不起了的解决方案,以前,在这个方案净来之前,做个网站的成本太高了。后台出现了lamp即免费的linux + 免费的apach + 免费的mysql + 免费简单的php的免费解决方案。在不输给其它的收费解方案时,使用方面也不差。这就大大降低建站的成本了。这一方案也极大促进互联网的发展。
docker是个非常好迁移方案。以前要保证据完整迁移性,最保险的是做镜像备份,然后在到新的机器上恢复,真的很繁琐很麻烦很不人性化。而备份镜像有大量的数据是环境之类的为保证软件的运行用的,只有一小部分才是这个软件的本身。而docker的迁移就是迁移这个小部分的软件,而这个软件依赖的系统和环境则是通过配置文件,要么构建出来要么下载已经可以用的镜像,来保证这个软件能稳定运行起来。整个过程可以是自动流程化。
而基于dockerphp环境,用来测试开发是再好不过了。

1 准备工作

  • 1.1 要已经安装好docker的机器
tip

如果没有,请结合你系统的情况根据《官方安装说明文档》

warning

1.2 示例约定

如果是命令行则是$开头,如:

    $ echo 'hello world'

$emsp; 如果是代码过多会用...来省略一部分,如:

<?php

namespace App\Http\Controllers;
.
.
.
class UsersController extends Controller
{
public function index()
{
$users = User::all();
return view('users.index', compact('users'));
}
}

2 基本部分

2.1 初始化项目

    $ mkdir lnmp && cd mkdir && git init #初始化一个叫lnmp的项目
$ mkdir -p php/5.3 nginx mysql

在根目录新建.gitignore忽略配置文件。内容为:

/.DS_Store
/.idea

# 环境变量文件
.env

#忽略mysql的数据存放目录下的文件,但保持这个目录
/mysql/data/*
!/mysql/data/.gitkeep

文件列表为:

.
├── .gitignore // 忽略配置文件
├── mysql // 存放mysql 相关配置和数据源
│ └── data // mysql 源数据保存目录
│ └── .gitkeep // git 收录当前目录的空白文件, 以下同名文件作用一样
├── nginx // 存放nginx 相关配置
│ └── .gitkeep
├── php // 保存php相关的配置
│ └── 5.3
│ └── .gitkeep
└── wwwroot
└── .gitkeep

检查下git下的新增的跟踪情况,并初始化提交。

$ git add -A
$ git status
...
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: mysql/data/.gitkeep
new file: nginx/.gitkeep
new file: php/5.3/.gitkeep
new file: wwwroot/.gitkeep
$ git commit -m "init"

2.2 配置mysql服务

在根目录新建docker-compose.yml文件,加入以下配置

version: '3'

services:

mysql: # 服务名
image: mysql:8.0 # 镜像和版本
ports:
- "3306:3306" # 本地端口:容器内部端口
volumes:
- ./mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:ro # 本地配置文件挂载
- ./mysql/data:/var/lib/mysql/:rw # 把源数据保存到外面来
restart: always # 开机启动
environment:
MYSQL_ROOT_PASSWORD: "12345678" # 密码
container_name: mysql # 别名

启动mysql服务

$ sudo docker-compose up # 在项目根目录上
Creating mysql ... done
Attaching to mysql
mysql | 2020-09-01 12:37:06+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql | 2020-09-01 12:37:07+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql | 2020-09-01 12:37:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql | 2020-09-01 12:37:07+00:00 [Note] [Entrypoint]: Initializing database files
mysql | 2020-09-01 12:37:14+00:00 [Note] [Entrypoint]: Database files initialized
mysql | 2020-09-01 12:37:14+00:00 [Note] [Entrypoint]: Starting temporary server
mysql | 2020-09-01 12:37:15+00:00 [Note] [Entrypoint]: Temporary server started.
mysql | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql |
mysql | 2020-09-01 12:37:18+00:00 [Note] [Entrypoint]: Stopping temporary server
mysql | 2020-09-01 12:37:21+00:00 [Note] [Entrypoint]: Temporary server stopped
mysql |
mysql | 2020-09-01 12:37:21+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
mysql |

启动成功后,可以通过3306端口进行连接该服务了。ip 为本地ip,账号root,密码12345678。 在本地用mysql客端或连接工具进行测试。

2.2.1 拆分mysql容器服务环境变量

根目录新建.env,并把docker-composemysql的本地端口号和密码拆分过来:

## mysql configure
MYSQL_ROOT_PASSWORD=12345678
MYSQLPORT=3306

docker-compose.yml则修改为:

...
mysql: # 服务名
image: mysql:8.0 # 镜像和版本
ports:
- "${MYSQLPORT}:3306" # 本地端口:容器内部端口
...
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}" # 密码
...

重新启动看看:

$ sudo docker rm -f mysql # 删除容器
$ rm -rf mysql/data # 删除mysql数据卷
& sudo docker-compose up # 启动mysql 服务
Creating mysql ... done
Attaching to mysql
mysql | 2020-09-01 12:57:46+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql | 2020-09-01 12:57:46+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
...

当前目标是配置mysql并启动,已经成功了,作下提交:

$ cp .env .env.example  # 备份示例环境变量配置
$ git add -A
$ git status
...
new file: .env.example
new file: docker-compose.yml
new file: mysql/mysql.cnf

$ git commit -m "【mysql】配置mysql服务"

2.3 配置nginx服务

2.3.1 添加默认静态网站

默认静态网站是指访问到当前主机是,没有匹配对应域名对应的网站目录时,则nginx会自动去访问默认目录的文件,也就是通常使用ip去访问一台主机时见到的默认页面,就是属于默认目录(网站)。
在项目目录下的wwwroot添加default目录并添加index.html文件和50x.html文件,如:

  • wwwroot/default/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 以及wwwroot/default/50x.html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

由于wwwroot不再一个空目录了,则用于让git跟踪空目录用的wwwroot/.gitkeep文件可以删除了。

$ rm wwwroot/.gitkeep

最后这一步作好后作下提交

$ git add -A
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: wwwroot/.gitkeep
new file: wwwroot/default/50x.html
new file: wwwroot/default/index.html
$ git commit -m "【nginx】添加默认静态网站目录" # 提交本次的工作

2.3.2 启动nginx服务并访问默认的网站

tip

默认的网站目录新建了,现在需要来访问它。要做的就是配置nginx的配置文件,启动后,nginx会监听80端口。当访问主机的80端口时,由于ip没有做指定的网站目录,则nginx去访问上面2.3.1小节创建的默认目录,并显示出来。

nginx/nginx.conf; 内容:

# 本文件在项目位置nginx/nginx.conf 会被挂载进容器的/etc/nginx/nginx.conf

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

#/etc/nginx/conf.d/目录下所有后缀.conf的配置文件,
include /etc/nginx/conf.d/*.conf;
}

配置文件 nginx/conf.d/default.conf, 内容为:

server {
listen 80;
listen [::]:80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /wwwroot/default;
index index.html index.htm;
}

#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 html;
# 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;
#}
}
warning

最后配置docker-compose.ymlnginx服务,并将文件nginx/nginx.conf和目录nginx/conf.d挂载进容器中使其配置生效,然后把网站的也挂载进去,使其能访问主机的文件

docker-compose.yml配置如下:

...
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}" # 密码
container_name: mysql # 别名

nginx:
image: nginx:1.19.2
ports:
- "80:80" # 2 主机端口:容器端口
- "443:443" # 2 主机端口:容器端口
volumes:
- ./wwwroot:/wwwroot:rw # 2 网站的目录
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:rw # 2 nginx主配置文件
- ./nginx/conf.d:/etc/nginx/conf.d:rw # 2 nginx的配置目录,nginx会加载整个合法命名的目录下所有配置
restart: always
container_name: nginx

最后启动下docker看看是否生效了

$ docker-compose rm # 删除原来的容器
Going to remove mysql
Are you sure? [yN] y
Removing mysql ... done
$ docker-compose up
Creating mysql ... done
Creating nginx ... done
Attaching to nginx, mysql
mysql | 2020-09-01 15:45:26+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
nginx | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx | 10-listen-on-ipv6-by-default.sh: error: IPv6 listen already enabled
nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx | /docker-entrypoint.sh: Configuration complete; ready for start up
mysql | 2020-09-01 15:45:26+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql | 2020-09-01 15:45:26+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.

warning

启动成功后,访问下http://127.0.0.1,结果会是wwwroot/default/index.html的内容。 本次的工作是配置nginx服务并成功运行已经成功了。最后作下本次工作的提交。

$ rm nginx/.gitkeep # 这个文件已经没用了
$ git add -A
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: docker-compose.yml
deleted: nginx/.gitkeep
new file: nginx/conf.d/default.conf
new file: nginx/nginx.conf
$ git commit -m "【nginx】配置nginx默认网站"

2.3.3 配置指定域名静态网站

tip

本次的工作是在本地解析test.com域名访问本地127.0.0.1并在nginx配置一个新静态网站供这个域名专门访问。

首先配置本地的解析文件hosts, maclinux的文件在/etc/hosts.加入:

...
127.0.0.1 test.com

添加wwwroot/test.com目录。
网站首页: wwwroot/test.com/index.html,内容为:

hi! this is domain for test.com

404页面 wwwroot/test.com/404.html,内容为:

这里是404页面,这个页面的作用是请求出错了,但错误是在于用户那一边。通常我会提示,你怎么出错了.

50x页面 wwwroot/test.com/50x.html,内容为:

这里是50x页面,这个页面的作用是请求出错了,但错误是在于服务器边。通常出于安全,我不会告诉你服务器哪里出错了,但程序会记录下来的。
tip

最后省添加test.com.conf配置文件

nginx/conf.d/test.com.conf, 内容为:

server {
listen 80;
listen [::]:80;
server_name test.com; # 指定域名

location / {
root /wwwroot/test.com; # 指定网站目录
index index.html; # 默认页面
}

error_page 404 /404.html; # 404 错误要访问的文件
error_page 500 502 503 504 /50x.html; # 50x错误要访问的文件
}

保存好以上的文件后,重新启动下并访问http://test.com,正常则说明成功了。到这了里,这一小节的大部分工作是完成了,但还有优化的地方。最好有个日志,所以要新建个目录来存放入访问的成功的日志和失败的日志,这样使其出错了可以还有日志这条线索。
新建nginx/logs/.gitkeep文件。并修改.gitignore配置:

...
#保持nginx/logs目录,不跟踪生成的日志文件
/nginx/logs/*
/nginx/logs/.gitkeep

然后修改docker-compose.yml将来用于存放日志的目录挂载到容器中,使nginx产生的日志保存下来:

...
- ./wwwroot:/wwwroot:rw # 2 网站的目录
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:rw # 2 nginx主配置文件
- ./nginx/conf.d:/etc/nginx/conf.d:rw # 2 nginx的配置目录,nginx会加载整个合法命名的目录下所有配置
- ./nginx/logs:/etc/nginx/logs:rw # 2.3.3 nginx网站访问日志记录
...

然后修改nginx/conf.d/test.com.conf使访问这个网站会生成日志:

...
error_page 404 /404.html; # 404 错误要访问的文件
error_page 500 502 503 504 /50x.html; # 50x错误要访问的文件

access_log /etc/nginx/logs/test.com.access.log; # 访问日志 记录所有成功或失败的访问
error_log /etc/nginx/logs/test.com.error.log; # 失败日志 状态码不为200
}

最后重启下服务,并查看是否成功:

$ docker-compose rm
Going to remove mysql, nginx
Are you sure? [yN] y
Removing mysql ... done
Removing nginx ... done
$ docker-compose up
Creating nginx ... done
Creating mysql ... done
Attaching to nginx, mysql
...

然后访问http://test.com并查看nginx/logs是否有日志产生。
好,本小节的的工作是完成指定域名访问指定的静态网站。现在做下工作提交来算是完成了这个阶段。

$ git add -A
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .gitignore
modified: docker-compose.yml
new file: nginx/conf.d/test.com.conf
new file: nginx/logs/.gitkeep
new file: wwwroot/test.com/404.html
new file: wwwroot/test.com/50x.html
new file: wwwroot/test.com/index.html

$ git commit -m "【nginx】配置一个域名静态网站"

2.4 配置php-fpm服务

tip

php-fpm是一个基于于fast CGI协议的进程管理器,用于处理nginx等待网站引擎转发过来的动态网站请求。然后自己启动一个子进程进行php脚本的运行并返回给nginx处理结果。fast 默认运行在9000端口上。

修改docker-compose.yml

...
- ./nginx/logs:/etc/nginx/logs:rw # 2.3.3 nginx网站访问日志记录
restart: always
container_name: nginx

php53:
image: leleos/php-fpm:5.3
ports:
- "9000:9000"
volumes:
- ./wwwroot:/wwwroot:rw
restart: always
container_name: php53

以上就是php-fpm的容器配置了,而由于mysqlphpnginx之间的是有依赖关系的,php依赖于mysqlnginx又依赖php。所以最好的启动顺序为mysql phpnginx。所以完整的配置为:

...
- ./nginx/conf.d:/etc/nginx/conf.d:rw # 2 nginx的配置目录,nginx会加载整个合法命名的目录下所有配置
- ./nginx/logs:/etc/nginx/logs:rw # 2.3.3 nginx网站访问日志记录
restart: always
container_name: nginx
depends_on:
- "php53" # 依赖于php53

php53:
image: leleos/php-fpm:5.3
ports:
- "9000:9000"
volumes:
- ./wwwroot:/wwwroot:rw
restart: always
container_name: php53
depends_on:
- "mysql" # 依赖于 nginx

好,重启服务试试

$ docker-compose stop
$ docker-compose rm
Going to remove nginx, php53, mysql
Are you sure? [yN] y
Removing nginx ... done
Removing mysql ... done
$ docker-compose up
Creating mysql ... done
Creating php53 ... done
Creating nginx ... done
Attaching to mysql, php53, nginx
mysql | 2020-09-01 18:47:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
mysql | 2020-09-01 18:47:20+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
...
warning

本次工作就是添加以上的配置,来完了php-fpm服务

$ git add -A
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: docker-compose.yml
$ git commit -m "【php】配置php服务"

2.5 配置php + msql + nginx组成动态网站

tip

现在的情况是,在一台主机上,运行着3个容器服务,主机可以自由访问各个容器暴露出来的接口,但,容器与容器之间却不行。而一个动态请求,会由于nginx转发给php-fpmphp需要CURD时则需要连接数据库,由于容器把它们隔离起来不能访问,但实际上它们需要能访问的。所以需要配置个网络,让它们之前可以相互连接

配置docker-compose.yml(步骤2.5 )

version: '3'

services:

mysql: # 服务名
image: mysql:8.0 # 镜像和版本
...
container_name: mysql # 别名
networks:
- default # 2.5 加入默认网络

nginx:
image: nginx:1.19.2
...
networks:
- default # 2.5 加入默认网络

php53:
image: leleos/php-fpm:5.3
...
- "mysql" # 依赖于 nginx
networks:
- default # 2.5 加入默认网络

# 2.5 定义默认网络
networks:
default:
warning

当加入共同的网络后,容器和容器之前就可以相互访问了,比如nginx容器要访问php-fpm容器,如

$ docker exec -it php53  /bin/sh # 在宿主机上进行php-fpm容器内
$ ping nginx # 直接ping nginx容器
PING nginx (172.20.0.4) 56(84) bytes of data.
64 bytes from nginx.lnmp_default (172.20.0.4): icmp_seq=1 ttl=64 time=0.103 ms
...

经过测试是可行的,不过容器之前的访问变成了别名了,容器的ip每次启动不一定是上次一样的,但,网络别名(叫域名也行)一定是指向容器ip的,而这个别名就是容器的服务名。

2.5.1 配置动态网站

warning

上次的域名静态网站test.com这次我们来配置为能执行php脚本的动态网站.

完整docker-compose.yml

version: '3'

services:

mysql: # 服务名
image: mysql:8.0 # 镜像和版本
ports:
- "${MYSQLPORT}:3306" # 本地端口:容器内部端口
volumes:
- ./mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:ro # 本地配置文件挂载
- ./mysql/data:/var/lib/mysql/:rw # 把源数据保存到外面来
restart: always # 开机启动
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}" # 密码
container_name: mysql # 别名
networks:
- default # 2.5 加入默认网络

nginx:
image: nginx:1.19.2
ports:
- "80:80" # 2 主机端口:容器端口
- "443:443" # 2 主机端口:容器端口
volumes:
- ./wwwroot:/wwwroot:rw # 2 网站的目录
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:rw # 2 nginx主配置文件
- ./nginx/conf.d:/etc/nginx/conf.d:rw # 2 nginx的配置目录,nginx会加载整个合法命名的目录下所有配置
- ./nginx/logs:/etc/nginx/logs:rw # 2.3.3 nginx网站访问日志记录
- ./nginx/fastcgi_params:/etc/nginx/fastcgi_params:rw # 2.5.1 动态网站配置
restart: always
container_name: nginx
depends_on:
- "php53" # 依赖于php53
networks:
- default # 2.5 加入默认网络

php53:
image: leleos/php-fpm:5.3
ports:
- "9000:9000"
volumes:
- ./wwwroot:/wwwroot:rw
restart: always
container_name: php53
depends_on:
- "mysql" # 依赖于 nginx
networks:
- default # 2.5 加入默认网络

# 2.5 定义默认网络
networks:
default:

被配置为适应动态网站的nginx/conf.d/test.com/conf,如下:

server {
listen 80;
listen [::]:80;
server_name test.com; # 指定域名

location / {
root /wwwroot/test.com; # 指定网站目录
index index.php index.html; # 默认页面 2.5.1动态网站配置
}

# 2.5.1 动态网站配置
location ~ \.php$ {
root /wwwroot/test.com;
fastcgi_pass php53:9000; # 2.5.3 php53指向 php-fpm的容器
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}

error_page 404 /404.html; # 404 错误要访问的文件
error_page 500 502 503 504 /50x.html; # 50x错误要访问的文件

access_log /etc/nginx/logs/test.com.access.log; # 访问日志 记录所有成功或失败的访问
error_log /etc/nginx/logs/test.com.error.log; # 失败日志 状态码不为200
}

添加新的nginx配置文件nginx/fastcgi_params,内容如下:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

添加php测试文件wwwroot/test.com/index.php,内容如下,

<?php

// 用于测试数库能否连接
$servername = "mysql"; # mysql 指向msqyl服务容器
$username = "root";
$password = "12345678";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

// 打印php信息
phpinfo();

那么提交下本次的工作算完成了:

$ git add -A
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: docker-compose.yml
modified: nginx/conf.d/test.com.conf
new file: nginx/fastcgi_params
new file: wwwroot/test.com/index.php
$ git commit -m "【php】2.5.1 配置动态网站"
warning

-个用于跑网站的lnmp就出来,不否定会不会有其它的问题,至少现在看不出来什么问题。

示例代码

参考资料