docker 搭建LNMP环境

php7

blog.jkdev.cn/index.php/archives/230

https://www.bilibili.com/video/BV1Mv411q7de?from=search&seid=3698297787679581674

https://www.bilibili.com/video/BV115411W7ga?from=search&seid=3698297787679581674

https://www.bilibili.com/video/BV1ta4y147FE?t=1618

https://www.runoob.com/docker/docker-install-php.html

启动php

docker pull php:7.3-fpm
    
docker run  
--name myphp73-fpm 
-p 9001:9000 
-v /root/docker/nginx/www:/usr/share/nginx/html 
--restart always 
--link mysql-service5.7:mysql-service5.7  #mysql容器的名字
-d php:7.3-fpm
    
    

创建 ~/nginx/conf/conf.d 目录:

mkdir -p /root/docker/nginx/{www,logs,conf}

启动nginx

docker pull nginx

docker run  
--name mynginx 
-p 8081:80 
-p 443:443 
-v /etc/localtime:/etc/localtime:ro 
-v /root/docker/nginx/www:/usr/share/nginx/html 
-v /root/docker/nginx/conf:/etc/nginx 
-v /root/docker/nginx/logs:/var/log/nginx 
--restart always 
--link myphp73-fpm:myphp73-fpm  #php容器的名字 
-d nginx 


#说明
--link myphp-fmt:php :把php-fpm的网络并入nginx,并通过修改nginx的/etc/hosts,
把域名php映射成127.0.0.1,让nginx通过php:9000访问php-fmt
访问站点:http://ip:8083/index.php显示phpinfo信息


添加nginx的配置文件

在该目录下添加 /root/docker/nginx/conf/conf.d/test-php.conf 文件,内容如下:

vim /root/docker/nginx/conf/conf.d/test-php.conf

server {
    listen       80;
    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;
    error_log   /var/log/nginx/localhost.error.log;
    root  /usr/share/nginx/html;

    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   myphp73-fpm:9000; #myphp73-fpm容器的名字
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

curl localhost:8081/index.php

122.114.30.38:8081/index.php

http://122.114.30.38:8081/1.php

documentroot

documentrootfastcgi_script_name;
fastcgi_pass 172.17.0.3:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}

出现502 gateway错误
这里的fastcgi_pass要是phprpm容器的内网ip地址,而不是127.0.0.1

出现404 file not found
其实nginx版本不通,document_root高版本已不支持,写成绝对路径,但大部署介绍写的是宿主机路径,这是错的。要写成phpfpm容器内的路径,如:/var/www/html
还有我在安装phpfpm时挂载文件时-v宿主机要写到:后面
还碰到进容器后无权限,要在-v挂载后加–privileged=true
有的说要去修改phpfpm的配置文件把listen 127.0.0.1改成0.0.0.0这是不需要的
以上应该就能访问通了

如果部署的时候有权限不足
那要把宿主机对应的html文件夹设置成777

有的需要php特殊插件
phpfrm内有docker-php-ext-install命令
进入phpfrm容器内执行即可,如扩展mysql执行 docker-php-ext-install pdo pdo_mysql
如果ext里面没有还可以用如下的

apt update #更新软件源
apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev #安装各种库
docker-php-source extract #解压源码
cd /usr/src/php/ext/gd #gd源码文件夹
docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2 #准备编译
docker-php-ext-install gd #编译安装
php -m | grep gd

在所有环境都成功部署好后,有的在运行应用时,会出现"Primary script unknown"错误,一般这是由于nginx和php-fpm的用户不同导致的。把nginx.conf和php-fpm.d/www.conf 的user设为同一个就可以了。

原文地址:https://www.cnblogs.com/haima/p/13424970.html