首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP在单个容器中启动后启动Nginx (或侦听http端口)?

如何在PHP在单个容器中启动后启动Nginx (或侦听http端口)?
EN

Server Fault用户
提问于 2020-12-17 00:03:39
回答 2查看 2.5K关注 0票数 0

在云运行的单个容器中运行Nginx+PHP有问题。我的容器是基于阿尔卑斯和管理Nginx和PHP启动由主管。总的来说,它工作得很好,但从Nginx开始监听HTTP端口到PHP派生的时间间隔很短。这将导致出现502个HTTP错误,其中包含以下日志消息:

代码语言:javascript
复制
6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 169.254.8.129, server: _, request: "POST / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000"

这里的问题是,Cloud决定容器在打开8080端口时已准备好处理请求。在端口打开后,Cloud立即发送一个请求,该请求在第一次尝试时总是失败,因为FPM还没有准备好。日志消息NOTICE: fpm is running, pid 4在第一个请求到达并失败后出现。

当PHP准备就绪时,如何管理Nginx来打开它的端口?

主管配置:

代码语言:javascript
复制
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/run/supervisord.pid

[program:php-fpm]
command=php-fpm -F
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

Nginx配置:

代码语言:javascript
复制
# Write temporary files to /tmp so they can be created as a non-privileged user
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;

access_log /dev/stdout;
error_log /dev/stderr notice;

server {
        listen 8080 default_server;

        index index.php;

        keepalive_requests    10;
        keepalive_timeout     60 60;

        root /var/www/html/app/public;

        charset utf-8;

        server_name _;

        # Deny hidden files (.htaccess, .htpasswd, .DS_Store).
        location ~ /\. {
            deny            all;
            access_log      off;
            log_not_found   off;
        }
        ########################
        # mappings             #
        ########################

        location ~ \.(js|css|png|jpg|ico) {
            expires 5d;
            try_files $uri $uri/ =404;
            return 404;
        }

        # Allow fpm ping and status from localhost
        location ~ ^/(fpm-status|fpm-ping)$ {
            access_log off;
            allow 127.0.0.1;
            deny all;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
        }

        location / {
            client_max_body_size 100m;
            try_files $uri @fpm;
        }

        location @fpm {
            # worker may take long time to finish (max 1 hour)
            fastcgi_read_timeout 3600s;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param HTTP_PROXY "";
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/html/app/public/index.php;
            fastcgi_param SCRIPT_NAME index.php;
        }
}

我启用了FPM ping/status页面。我能用它们触发Nginx端口打开吗?

Update 1:

我试着调整监督的优先级,然后开始几秒钟:

代码语言:javascript
复制
...
[program:php-fpm]
...
priority=100
startsecs=3

[program:nginx]
...
priority=200

但没有成功:

代码语言:javascript
复制
[18-Dec-2020 00:31:04] NOTICE: ready to handle connections
[18-Dec-2020 00:31:04] NOTICE: fpm is running, pid 3
2020-12-18 00:30:30,689 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 3 seconds (startsecs)
2020-12-18 00:30:28,388 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Error POST 502 549 B 3.286 s Google-Cloud-Scheduler https://***.run.app/
169.254.8.129 - - [18/Dec/2020:00:30:27 +0000] "POST / HTTP/1.1" 502 150 "-" "Google-Cloud-Scheduler"
169.254.8.129 - - [18/Dec/2020:00:30:27 +0000] "POST / HTTP/1.1" 502 150 "-" "Google-Cloud-Scheduler" "35.187.131.214"
2020/12/18 00:30:27 [error] 6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 169.254.8.129, server: _, request: "POST / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "***.run.app"
2020-12-18 00:30:26,937 INFO spawned: 'nginx' with pid 4
2020-12-18 00:30:26,829 INFO spawned: 'php-fpm' with pid 3
2020-12-18 00:30:25,730 INFO supervisord started with pid 1
2020-12-18 00:30:25,704 CRIT Supervisor is running as root. Privileges were not dropped because no user

这两个应用程序仍然由主管同时启动,nginx首先初始化。主管应用于应用程序的RUNNING状态对于云运行没有任何意义。

EN

回答 2

Server Fault用户

发布于 2020-12-18 03:31:51

现在,我得到了以下入口点脚本,该脚本确保在启动nginx之前运行PHP:

代码语言:javascript
复制
#!/usr/bin/env sh
set -e

# Start PHP-FPM as a daemon in the background
php-fpm -D

# Wait until PHP-FPM is up and accepts connections. Fail if not started in 10 secs.
for run in $(seq 20)
do
  if [ "$run" -gt "1" ]; then
    echo "Retrying..."
  fi
  RESPONSE=$(
    SCRIPT_NAME=/fpm-ping \
    SCRIPT_FILENAME=/fpm-ping \
    REQUEST_METHOD=GET \
    cgi-fcgi -bind -connect 127.0.0.1:9000 || true)
  case $RESPONSE in
    *"pong"*)
      echo "FPM is running and ready. Starting nginx."
      # Run nginx without exiting to keep the container running
      nginx -g 'daemon off;'
      exit 0
      ;;
  esac
  sleep .5
done
echo "FPM has failed to start on-time, exiting"
exit 1

apk add fcgi命令是必需的(对于Alpine )。

我还假设php-fpm -D命令总是在FPM准备好之后退出,所以不需要循环,只需一个接一个地运行命令。但我还没试过呢。

票数 2
EN

Server Fault用户

发布于 2020-12-17 18:09:58

主管确实可以使用startdelay选项,但目前还没有。要延迟nginx的启动,请更改指定给Supervisor (相关螺纹)的命令:

  1. 运行一个shell来睡眠,然后直接启动nginx : command=bash -c“exec 5 && exec nginx -g 'daemon off;'”
  2. 运行一个脚本,该脚本在启动nginx: command=delayd-nginx.sh. delayed-nginx.sh:#!/bin/bash休眠5 exec -g 'daemon;

原始答案

我没有专门使用Supervisor,但是这个答案可能会为您工作。这将导致Supervisor只在php-fpm运行一次就启动nginx。

代码语言:javascript
复制
[program:php-fpm]
command=php-fpm -F
...
priority=100

[program:nginx]
command=nginx -g 'daemon off;'
...
priority=200

您还可能需要将startsecs设置为更高的值(比默认的1秒),以便主管只在php-fpm运行更长时间(例如,5秒)之后才考虑启动它:

代码语言:javascript
复制
[program:php-fpm]
command=php-fpm -F
...
priority=100
startsecs=5

请参阅主管文档中的更多内容。

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1046515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档