docker搭建lnmp环境步骤(win10家庭版)
2020-11-11 本文已影响0人
zxwen_zl
避坑!用docker搭建php开发环境,希望能给同样有需要搭建环境的同学起到一些帮助,少踩一些坑。
1、下载用到的镜像
docker pull php:7.2-fpm
docker pull mysql
docker pull nginx
2、创建相应容器
1、安装顺序,php->nginx->mysql
#由于docker运行的VM里边setting中Shared Folder中只有c/Users,挂载文件放到c:/users 目录下可以了
#复制文件时使用C:/Users/zxwen_zl,挂载时使用 /c/Users/zxwen_zl
2、安装php
docker run --name php -d php:7.2-fpm
#复制配置文件
docker cp php:/usr/local/etc/php-fpm.d/www.conf C:/Users/zxwen_zl/docker/php-conf/www.conf
docker cp php:/usr/local/etc/php/php.ini-production C:/Users/zxwen_zl/docker/php-conf/php.ini
docker kill php
docker rm php
docker run --name php -v /c/Users/zxwen_zl/docker/www:/var/www/html -v /c/Users/zxwen_zl/docker/php-conf/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /c/Users/zxwen_zl/docker/php-conf/php.ini:/usr/local/etc/php/php.ini -d php:7.2-fpm
3、安装nginx
docker run --name nginx -p 80:80 -d -v /c/Users/zxwen_zl/docker/www:/var/www/html -d nginx
#复制配置文件
docker cp nginx:/etc/nginx/conf.d C:/Users/zxwen_zl/docker/nginx-conf/conf.d
docker kill nginx
docker rm nginx
docker run --name nginx -p 80:80 -v /c/Users/zxwen_zl/docker/www:/var/www/html -v /c/Users/zxwen_zl/docker/nginx-conf/conf.d:/etc/nginx/conf.d/ --link php:php -d nginx
#修改/c/Users/zxwen_zl/docker/nginx-conf/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html; //修改根目录
#charset koi8-r;
access_log /var/log/nginx/host.access.log main; //打开访问日志
error_log /var/log/nginx/host.error.log warn; //打开错误日志
location / {
index index.html index.htm index.php;
}
#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$ {
fastcgi_pass php:9000; //指向端口9000上的主机名php
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
}
}
修改完重启nginx容器 docker restart nginx
或在nginx容器 service nginx reload
4、安装mysql
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /d/docker/mysql/data:/var/lib/mysql -v /d/docker/mysql/conf:/etc/mysql/conf.d -d --privileged=true mysql
#mysql安装到c盘会报错,启动不了,放到了d盘
5、打开/c/Users/zxwen_zl/docker/www创建index.php文件(phpinfo)
6、访问地址:ip/index.php
看到phpinfo页面,大功告成!!!
3、报错问题列表
1、WIN10下 docker报错Error response from daemon: invalid mode
解决办法:docker挂载目录不能使用D:/docker这种写法,正确写法:/d/docker
2、访问报错
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
解决方法:1、nginx配置文件
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
改为
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
或2、查看php并入到nginx里,即配置nginx时是否使用 --link php:php(当初这个问题找了一天才发现)
3、访问报错
upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.33.1, server: waiter-host.chuchujie.com, request: "GET /admin.php?s=/Index/test HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "waiter-host.chuchujie.com"
解决办法:nginx配置文件,
fastcgi_pass 127.0.0.1:9000;
改为
fastcgi_pass php:9000;