问题
之前利用docker搭PHP+Nginx+MySQL屡试不爽,但在1024这天换了一台服务器,出现了意料之外的问题:
- 浏览器访问服务器超时TIME_OUT。
- curl localhost:80 和 curl localhost:9000,主机上无响应,在容器内也是Failure to connect,或无响应
解决
经过问题排查,原因为docker0和eth0网卡冲突,皆为172.*.*.*/24段。
nano /etc/docker/daemon.json
- 添加“bip”将docker0改为192.168.*.*段
"bip": "192.168.1.3/24"
Ctrl+O保存,Ctrl+X退出
sudo service docker restart
ifconfig
参考 https://cloud.tencent.com/developer/article/1768097 https://www.zhihu.com/question/278340552/answer/399564710
再记Docker搭建PHP+Nginx+MYSQL简程
cd ~
mkdir -p nginx/www nginx/logs nginx/conf
cd ~/nginx/conf
nano myphp.conf
这里是使用/www目录,故而编写为fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
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;
}
}
Ctrl+O,Ctrl+X
cd ~/nginx/www
nano index.php
<?
echo phpinfo();
Ctrl+O,Ctrl+X
cd ~/nginx
sudo chmod 777 -R www
拉取并运行镜像
docker run --name myphp-fpm -p 9000:9000 -v ~/nginx/www:/www -d php:7.4.25-fpm
docker run --name myphp-nginx -p 80:80 -p 443:443 -d \
-v ~/nginx/www/:/usr/share/nginx/html:ro \
-v ~/nginx/conf/:/etc/nginx/conf.d:ro \
--link myphp-fpm:php nginx
docker run --name db_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=【密码】 -d mysql
参考 https://www.runoob.com/docker/docker-install-php.html
|