Docker 部署 nginx php 应用
最近在学习docker。 通过搭建nginx+php的开发环境实例来学习。 这里我记录了构建过程。 首先给出一份docker-compose.yml部署配置文件
version: '3'
services:
nginx:
container_name: myweb
image: nginx
ports:
- '80:80'
volumes:
- '/etc/nginx/:/etc/nginx' # 配置文件
- '/var/log/nginx/:/var/log/nginx/' # 日志
- '/data/php/yxyy/html:/data/php/yxyy/html' # 项目目录
php:
container_name: myphp
image: php:7.2-fpm
volumes:
- '/data/php/yxyy/html/:/data/php/yxyy/html/'
- '/usr/local/php7.2/:/usr/local/etc/'
以下是搭建的整个过程。 不会记录dockers的安装。 下面记录nginx+php的搭建过程
部署 nginx
先安装一个临时的nginx容器
$ docker run --name tmpweb -d -p 80:80 nginx
在浏览器中输入127.0.0.1显示熟悉的界面,进入 tmpweb 容器
$ docker exec -it tmpweb /bin/bash
搜索配置文件位置
$ find / -name nginx.conf
# 在 /ect/nginx 中找到配置文件
复制一份容器中nginx配置到宿主机/ect/nginx
$ docker cp tmpweb:/ect/nginx /ect/nginx
修改nginx配置,如站点根目录/etc/nginx/conf.d/default.conf。 我的项目在/data/php/yxyy/html
删除 tmpweb 容器,启动一个容器挂载自定义配置文件
docker run --name myweb -d -p 80:80 \
-v /ect/nginx:/ect/nginx \
-v /var/log/nginx/:/var/log/nginx/ \
-v /data/php/yxyy/html/:/data/php/yxyy/html/ nginx
在浏览器输入127.0.0.1验证是否正常
部署 PHP
php的安装过程也和nginx类似。 我使用的镜像是 php:7.2-fpm。 下面是我的安装过程:
$ docker run --name tmpphp -d -p 80:80 php:7.2-fpm # 安装临时php:7.2-fpm镜像
$ docker exec -it tmpphp /bin/bash # 进入容器
$ find / -name php-fpm.conf
#在 /usr/local/etc/ 中找到配置文件
$ docker cp tmpphp:/usr/local/etc/ /usr/local/php7.2/ # 拷贝一份容器内的配置到宿主机
$ docker run --name myphp -d -p 9000:9000 \
-v /usr/local/php7.2/:/usr/local/etc/ \
-v /data/php/yxyy/html/:/data/php/yxyy/html/ php:7.2-fpm
在这一点上,我认为还可以。 浏览器进行访问,结果显示502
解决 502
查看nginx日志如下:
*19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
连接有问题。 检查PHP容器是否启动:
$ docker ps
# 结果显示php容器是启动的
查看php容器日志:
$ docker logs myphp
# 也没有错误
查看主机进程:
$ ps -ef | grep php
# php 主进程和worker 进程都在运行
既然这些进程已经有了,那么问题就只回出现在它们之间的通信上。 主机上的telnet:
$ telnet 127.0.0.1 9000
# 可以正常的链接
容器不能相互通信吗? 查找资料说是不能通信,但我想验证一下:
# 首先进入myweb容器telnet 127.0.0.1 9000,没有telnet
# OK,然后安装。 安装后,telnet 127.0.0.1 9000 拒绝。 来这里确认无法直接通信
重新运行nginx容器并添加 --link myphp
$ docker run --name myweb -d -p 80:80 --link myphp \
-v /ect/nginx:/ect/nginx \
-v /var/log/nginx/:/var/log/nginx/ \
-v /data/php/yxyy/html/:/data/php/yxyy/html/ nginx
理想是美好的,现实是残酷的,还是无法沟通。 根据资料,link 将被放弃。 我安装的docker比较新。 我想这可能是原因。 使用 docker compose
集群管理启动它。 这是 docker-compose.yml 配置:
version: '3'
services:
nginx:
container_name: myweb
image: nginx
ports:
- '80:80'
volumes:
- '/etc/nginx/:/etc/nginx' # 配置文件
- '/var/log/nginx/:/var/log/nginx/' # 日志
- '/data/php/yxyy/html:/data/php/yxyy/html' # 项目目录
php:
container_name: myphp
image: php:7.2-fpm
volumes:
- '/data/php/yxyy/html/:/data/php/yxyy/html/'
- '/usr/local/php7.2/:/usr/local/etc/'
一顿操作猛如虎,一看结果250,我仍然看不到。 这时候就想砸电脑,进入容器:
$ telnet 127.0.0.1 9000
# 还是连接不上? 如果将 127.0.0.1 更改为容器名称会发生什么?
$ telnet myphp 9000
# 没想到,就在这个时候,一种幸福的感觉从容不迫的袭来。 幸福来得太突然
修改nginx配置,将127.0.0.1改成myphp(容器名),重启, 高光时刻到了。但是,其实是我想的太多了。 502解决,404意外出现。
解决 404
File not found
看到404,第一反应是路径不对。 反复确认后,路径就OK了。 我认为这只能是权限问题。 查看nginx日志:
*32 FastCGI sent in stderr: "Primary script unknown" while reading response header
ls -l
你真的没有查看权限。 当你看到你没有获得权限时,你会微笑。 这有点有趣。 给我 777 权限。 不幸的是,我仍然不能。 使用 root 用户执行。
- 修改php FMP的配置 www.conf: user=root,group=root;
- 修改nginx.conf:user=root。重启容器查看。
查看容器日志 docker logs phpxxx 显示
ERROR: [pool www] please specify user and group other than root
php 不能被 root 执行。 都改成nginx了。 结果还是不好,
cat /etc/passwd 检查没有这个用户,只有php的WWW数据用户。 修改nginx和php配置,改成WWW数据用户,重启。
最后!😊
相关文章
Common errors when compiling PHP module curl
发布时间:2025/04/08 浏览次数:184 分类:OPERATING SYSTEM
-
When compiling a PHP module, errors may occur if there are any omissions. Here is a summary of the errors that occurred during the compilation of the PHP module curl. curl can be said to be a commonly used module in PHP. However, in many ca
How to Install Nginx on Ubuntu 20.04?
发布时间:2025/04/07 浏览次数:157 分类:OPERATING SYSTEM
-
Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.
How to use Let's Encrypt with Nginx to configure https in Ubuntu 20.04
发布时间:2025/04/07 浏览次数:122 分类:OPERATING SYSTEM
-
Let's Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, enabling encrypted HTTPS on your web server. It simplifies the process by providing a software client, Certbot, which a
PHPStorm shortcut keys for Win/Linux/Mac
发布时间:2025/04/07 浏览次数:125 分类:OPERATING SYSTEM
-
edit Win / Linux Mac Notes Frequency of use Ctrl + Space ⌃Space Code auto-completion (usually conflicts with input method) ★☆☆☆☆ Ctrl + Shift + Enter ⌘ ⇧ ↩ Intelligent code completion (such as: if ()) ★☆☆☆☆ Ctrl
Install WordPress with Nginx on Ubuntu 18.04
发布时间:2025/04/07 浏览次数:86 分类:OPERATING SYSTEM
-
WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at