worker mpm php,Ubuntu 16.04 安裝 Apache2 + mod_fcgid + mpm_worker 跑 php 7.x

原本是安裝 fastcgi 來跑 php 7.0,後來因為遇到 chunked mode 的問題改裝 mod_fcgid,剛好記錄下來作法。

這篇是以 Ubuntu 16.04 + php 7.0 做範例:

Apache2 用 mod_fcgid 跑 php 7.0

在 Ubuntu 我習慣用 ppa 來裝 package,php 的 ppa 是 ppa:ondrej/php

$ sudo add-apt-repository ppa:ondrej/php -y

$ sudo apt-get update

安裝 apache2 和 php 7.0

$ sudo apt-get install -y php7.0 php7.0-cgi libapache2-mod-fcgid

我這邊只安裝基本的 php 7.0 base,有需要其他的再自己安裝,基本上都是叫做 php7.0-xxx。

Apache mpm 原本 event 改用 worker 多線程

$ sudo a2dismod mpm_event

$ sudo a2enmod fcgid mpm_worker rewrite

修改 fcgid.conf

$ sudo tee /etc/apache2/mods-available/fcgid.conf <

AddHandler fcgid-script .php

FcgidWrapper /usr/local/bin/php-wrapper .php

FcgidConnectTimeout 20

AddHandler fcgid-script .php

EOF

比較特別的是 FcgidWrapper,一般這邊指定的是 php-cgi,但官方有特別提到,如果要調整 mod_fcgid 的參數必須要透過 FcgidWrapper 來跑腳本

PHP applications are usually configured using the FcgidWrapper directive and a corresponding wrapper script. The wrapper script can be an appropriate place to define any environment variables required by the application, such as PHP_FCGI_MAX_REQUESTS or anything else. (Environment variables can also be set with FcgidInitialEnv, but they then apply to all applications.)

所以會透過 php-wrapper 這隻 script 處理變數後再執行 php-cgi,就像這樣:

$ tee /usr/local/bin/php-wrapper <

#!/bin/sh

# Set desired PHP_FCGI_* environment variables.

# Example:

# PHP FastCGI processes exit after 500 requests by default.

PHP_FCGI_MAX_REQUESTS=0

export PHP_FCGI_MAX_REQUESTS

# Replace with the path to your FastCGI-enabled PHP executable

exec /usr/local/bin/php-cgi

EOF

修改 PHP_FCGI_MAX_REQUESTS 的用意是預設 PHP_FCGI_MAX_REQUESTS 只能併發 500 個 processes,Apache2 建議改成 10000 或更高,如果 Request 超出這個數值則 FasCGI 會退出並且出現 500 Internal Server Error,如果要完全避免可以設為 0 但是會有 PHP application leaks resources 的問題。

By default, PHP FastCGI processes exit after handling 500 requests, and they may exit after this module has already connected to the application and sent the next request. When that occurs, an error will be logged and 500 Internal Server Errorwill be returned to the client. This PHP behavior can be disabled by setting PHP_FCGI_MAX_REQUESTS to 0, but that can be a problem if the PHP application leaks resources. Alternatively, PHP_FCGI_MAX_REQUESTS can be set to a much higher value than the default to reduce the frequency of this problem. PHP_FCGI_MAX_REQUESTS to resolve the problem.

另外在 VirtualHost 這邊要允許 +ExecCGI 執行,否則你的 mod_fcgid 會跑不起來 …

sudo tee /etc/apache2/sites-available/000-default.conf <

DocumentRoot /var/www/htdocs/public

Options -Indexes +ExecCGI

AllowOverride All

Order allow,deny

Allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

EOF

啟動 Apache2 服務

$ sudo systemctl enable apache2

$ sudo systemctl start apache2

另外有一點蠻值得提的,mod_fcgid 預設會停用子線程管理,有關於效能和資源應用的問題:

PHP child process management (PHP_FCGI_CHILDREN) should always be disabled with mod_fcgid, which will only route one request at a time to application processes it has spawned; thus, any child processes created by PHP will not be used effectively. (Additionally, the PHP child processes may not be terminated properly.) By default, and with the environment variable setting PHP_FCGI_CHILDREN=0, PHP child process management is disabled.

預設啟動 Apache2 的時候沒有 connection 時不會有 cgi processes,只有連線進來的時候才會自動生成。

請按讚:

喜歡 正在載入...

2 Replies to “Ubuntu 16.04 安裝 Apache2 + mod_fcgid + mpm_worker 跑 php 7.x”

給 Mr. 沙先生一點建議

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。针对你的问题,我可以给你一些简单的部署步骤: 1.安装nginx和PHP 在CentOS 6.8中安装nginx和PHP的方式如下: ``` sudo yum install nginx sudo yum install php-fpm ``` 2.配置nginx 在配置nginx之前,你需要备份一下默认的nginx配置文件。备份命令如下: ``` sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak ``` 然后,你可以通过编辑/etc/nginx/nginx.conf文件来配置nginx。 下面是一个简单的nginx配置示例,可以将它添加到/etc/nginx/nginx.conf文件中: ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 在这个示例配置中,我们将nginx的根目录设置为/usr/share/nginx/html,并且将index.php添加到默认文档列表中。同时,我们还配置了一个location块,用于处理PHP文件。 3.配置PHP 在配置PHP之前,你需要备份一下默认的php-fpm配置文件。备份命令如下: ``` sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak ``` 然后,你可以通过编辑/etc/php-fpm.d/www.conf文件来配置PHP。 下面是一个简单的PHP配置示例,可以将它添加到/etc/php-fpm.d/www.conf文件中: ``` [www] user = nginx group = nginx listen = /var/run/php-fpm/php-fpm.sock listen.owner = nginx listen.group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 ``` 在这个示例配置中,我们将PHP-FPM的用户和组设置为nginx,并将PHP-FPM的监听套接字设置为/var/run/php-fpm/php-fpm.sock。 4.重启服务 完成配置后,你需要重启nginx和php-fpm服务,以使配置生效。重启命令如下: ``` sudo systemctl restart nginx sudo systemctl restart php-fpm ``` 到这里,就完成了nginx+PHP的简单部署。同时,为了进一步提升性能,你可以使用Nginx+PHP+FastCGI加速模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值