JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Docker deploys nginx php application

Author:JIYIK Last Updated:2025/03/26 Views:

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_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/'

The following is the entire process of the build. The installation of dockers will not be recorded. The following records the process of building nginx+php

Deploy nginx

First install a temporary nginx container

$ docker run --name tmpweb -d -p 80:80  nginx

Enter 127.0.0.1 in the browser to display the familiar interface and enter the tmpweb container

$ docker exec -it tmpweb /bin/bash

Search Configuration File Location

$ find / -name nginx.conf  
# 在 /ect/nginx 中找到配置文件

Copy the nginx configuration in the container to the host machine/ect/nginx

$ docker cp tmpweb:/ect/nginx /ect/nginx

Modify the nginx configuration, such as the site root directory /etc/nginx/conf.d/default.conf. My project is in /data/php/yxyy/html

Delete the tmpweb container and start a container to mount the custom configuration file

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

Enter 127.0.0.1 in the browser to verify whether it is normal

Deploy PHP

The installation process of php is similar to that of nginx. The image I use is php:7.2-fpm. Here is my installation process:

$ 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 

At this point, I think it's OK. The browser accesses it and the result shows 502

Solving 502

Check the nginx log as follows:

 *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"

There is a connection problem. Check whether the PHP container is started:

$ docker ps 
# 结果显示php容器是启动的

View the PHP container log:

$ docker logs myphp 
# 也没有错误

View the host process:

$ ps -ef | grep php  
# php 主进程和worker 进程都在运行

Now that these processes are already there, the problem will only be in the communication between them. Telnet on the host:

$ telnet 127.0.0.1 9000  
# 可以正常的链接

Can't containers communicate with each other? I found that they can't communicate, but I want to verify it:

# 首先进入myweb容器telnet 127.0.0.1 9000,没有telnet

# OK,然后安装。 安装后,telnet 127.0.0.1 9000 拒绝。 来这里确认无法直接通信

Rerun the nginx container and add --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

Ideals are beautiful, but reality is cruel and we still cannot communicate. According to the data, link will be abandoned. The docker I installed is relatively new. I think this may be the reason. Use docker composecluster management to start it. This is the docker-compose.yml configuration:

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/'

After a lot of hard work, I still can't see the result of 250. At this time, I want to smash the computer and enter the container:

$ telnet 127.0.0.1 9000  
# 还是连接不上? 如果将 127.0.0.1 更改为容器名称会发生什么?
$ telnet myphp 9000  
# 没想到,就在这个时候,一种幸福的感觉从容不迫的袭来。 幸福来得太突然

Modify nginx configuration, change 127.0.0.1 to myphp (container name), restart, the highlight moment has arrived. However, in fact, I thought too much. 502 is solved, 404 appears unexpectedly.

Solving 404

File not found

When I saw the 404 error, my first reaction was that the path was wrong. After repeated confirmation, the path was OK. I think it can only be a permission problem. Check the nginx log:

*32 FastCGI sent in stderr: "Primary script unknown" while reading response header

ls -lYou really don't have permission to see it. You smile when you see that you don't have permission. That's kind of funny. Give me 777 permissions. Unfortunately, I still can't. Execute with root user.

  • Modify the configuration www.conf of php FMP: user=root, group=root;
  • Modify nginx.conf: user=root. Restart the container to view the result.

View the container logs docker logs phpxxx display

ERROR: [pool www] please specify user and group other than root

php cannot be executed by root. I changed it to nginx. But the result is still not good.

cat /etc/passwd Check that there is no such user, only the WWW data user of PHP. Modify the configuration of nginx and php, change to the WWW data user, and restart.

Finally! 😊

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Displays PHP configuration information on localhost

Publish Date:2025/04/13 Views:107 Category:PHP

phpinfo() is a built-in function in PHP which outputs all the information of PHP configuration on the local host. We have to phpinfo() create a PHP file with a simple function call. Sometimes, the file may not work properly and output a 404

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial