对于 Ubuntu 20-22 用户:

对于 Ubuntu 20-22 用户:

我安装了阿帕奇/2.4.54PHP72-FPMPHP74-FPM以及 Arch Linux 上其他版本的 PHP-FPM。显然存在一些配置错误httpd配置文件,PHP 无法解释任何内容并且浏览器会注释掉代码。

我尝试过的:

httpd.conf:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

httpd-vhosts.conf:

<VirtualHost *:80>
    LoadModule php7_module modules/libphp72.so
    AddHandler php72-script php
    Include conf/extra/php72-module.conf
    ...

php72-模块.conf:

<IfModule dir_module>
    <IfModule php_module7>
        <FilesMatch "\.php$">
            SetHandler application/x-httpd-php
            SetHandler "proxy:unix:/run/php72-fpm/php-fpm.sock|fcgi://localhost/"
            ...

php72-fpm is active.

php -v返回:PHP 8.1.12 (cli)


更新:

我搬家了:

<FilesMatch "\.php$">
    SetHandler "proxy:unix:/run/php72-fpm/php-fpm.sock|fcgi://localhost/"

<VirtualHost>并得到[503 Unavailable]

错误日志:

[proxy:error] FCGI: attempt to connect to Unix domain socket /run/php-fpm72/php-fpm.sock (*:80) failed
[proxy_fcgi:error] failed to make connection to backend: httpd-UDS

答案1

我相信您在配置中混合了 php-fpm 和 modphp。您只需要执行以下步骤:

sudo a2dismod php7.x          # disable modphp (optional)
sudo a2dismod mpm_prefork     # to use php-fpm, you need to switch to mpm_event
sudo a2enmod mpm_event        # enable mpm_event
sudo a2enmod proxy proxy_fcgi # enable proxy_fcgi, required for php-fpm
sudo a2enconf php7.2-fpm      # enable php7.2-fpm

无需更新您的 vhosts 文件,除非 Archlinux 没有附带 apache 助手a2*

更新后更新

确保在该特定位置有一个 unix 套接字。您可能应该在/etc/php/fpm/pool.d/xxx.conf配置中验证套接字的确切位置,然后重新启动 php-fpm。

答案2

对于 Ubuntu 20-22 用户:

目的:

使用套接字 + fcgid 模块安装 fpm + 根据每个域的请求应用用户权限。

步骤 1:安装

sudo apt-get update
sudo systemctl stop apache2
sudo apt-get install libapache2-mod-fcgid
sudo apt-get install php8.1-fpm

# ubuntu 20 missing libs?!
sudo apt-get install php-mbstring
sudo apt-get install php-gd

步骤 2:禁用/启用模块

sudo a2dismod php* mpm_prerfork cgi cgid
sudo a2enmod mpm_event fcgid proxy_fcgi headers rewrite
# Ubuntu 20
sudo a2enconf php7.4-fpm
# Ubuntu 22
sudo a2enconf php8.1-fpm

步骤3:添加配置命令

用户:“约翰”

域名:“mydomain.com”

(一)Apache FPM 配置

sudo nano /etc/apache2/mods-available/fcgid.conf

类型:

<IfModule mod_fcgid.c>
  FcgidConnectTimeout 20
  AddType   application/x-httpd-php .php
  AddHandler    application/x-httpd-php .php
  <IfModule mod_mime.c>
    AddHandler fcgid-script .fcgi
  </IfModule>
</IfModule>

(b).FPM 配置

# Ubuntu 20
sudo cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/mydomain.com.conf
# Ubuntu 22
sudo cp /etc/php/8.1/fpm/pool.d/www.conf /etc/php/8.1/fpm/pool.d/mydomain.com.conf

改变:

[www] -> [mydomain.com]
user = www-data -> user = john
group = www-data -> group = john

添加或留空:

;;;;;;;;;;
; Memory ;
;;;;;;;;;;
php_admin_value[memory_limit] = 12M
php_admin_value[post_max_size] = 12M
php_admin_value[upload_max_filesize] = 12M
;;;;;;;;;;;;
; Settings ;
;;;;;;;;;;;;
php_value[user_ini.filename] = ".user.ini"
php_value[default_charset] = "UTF-8"
php_flag[short_open_tag] = Off
php_flag[display_errors] = On
php_flag[display_startup_errors] = On
php_flag[log_errors] = On
php_value[log_errors_max_len] = 1024
php_flag[report_memleaks] = On
php_flag[html_errors] = On
php_value[error_reporting] = E_ALL & ~E_DEPRECATED & ~E_STRICT
php_flag[file_uploads] = On
php_value[max_file_uploads] = 20
php_flag[allow_url_fopen] = On
php_flag[allow_url_include] = Off
php_value[session.save_handler] = files
php_flag[session.use_strict_mode] = 1
php_flag[session.use_cookies] = 1
php_flag[session.use_only_cookies] = 1
; temporary disabled fro STEP 5 system('whoami') to run!
;php_admin_value[disable_functions] = exec,passthru,shell_exec,system
;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
php_admin_value[error_log] = "/var/www/mydomain.com/log/php_errors.log"
php_admin_value[open_basedir] = "/var/www/mydomain.com/"
php_admin_value[doc_root] = "/var/www/mydomain.com"
php_admin_value[sys_temp_dir] = "/var/www/mydomain.com/tmp"
php_admin_value[upload_tmp_dir] = "/var/www/mydomain.com/tmp"
php_admin_value[session.save_path] = "/var/www/mydomain.com/session"
php_admin_value[soap.wsdl_cache_dir] = "/var/www/mydomain.com/tmp"
php_admin_value[curl.cainfo] = "/var/www/mydomain.com/server/registry/cacert.pem"

php_admin_value, php_admin_flag:用户无法更改

php_value, php_flag: 可由用户更改

(c). 创建虚拟主机

sudo nano /etc/apache2/sites-available/mydomain.com.conf

类型:

<VirtualHost *:80>
ServerName mydomain.com.localhost
ServerAlias mydomain.com.localhost
ServerAdmin [email protected]

DocumentRoot /var/www/mydomain.com

Header set Access-Control-Allow-Origin "*"

<IfModule mod_fcgid.c>
    FcgidConnectTimeout 20
    AddType   application/x-httpd-php .php
    AddHandler    application/x-httpd-php .php

    # Ubuntu 20
    ProxyPassMatch " ^/(.*\.php(/.*)?)$" "unix:/run/php/php7.4-fpm.mydomain.com.sock|fcgi://localhost/var/www/mydomain.com/"
    # Ubuntu 22
    ProxyPassMatch " ^/(.*\.php(/.*)?)$" "unix:/run/php/php8.1-fpm.mydomain.com.sock|fcgi://localhost/var/www/mydomain.com/"

    <Directory /var/www/mydomain.com/>
        Options +ExecCGI
        Options -Indexes
        AllowOverride None
        Require all granted
#        FRONT CONTROLLER PATTERN
#        RewriteEngine On
#        RewriteCond "%{REQUEST_URI}" "!=/public_html/index.php"
#        RewriteRule "^(.*)$" "/public_html/index.php?$1" [NC,NE,L,PT,QSA]
    </Directory>
</IfModule>

# CPU usage limits 5s 10s
RLimitCPU 5 10
# memory limits to 10M 20M
RLimitMEM 10000000 20000000
# limit of forked processes 20 30
RLimitNPROC 20 30
LogLevel warn
ErrorLogFormat connection "[%t] New connection: [%{c}L] [ip: %a]"
ErrorLogFormat request "[%t] [%{c}L] New request: [%L] [pid %P] %F: %E"
ErrorLogFormat "[%t] [%{c}L] [%L] [%l] [pid %P] %F: %E: %M"
ErrorLog /var/www/mydomain.com/log/apache_error.log
CustomLog /var/www/mydomain.com/log/apache_access.log combined
ServerSignature Off
</VirtualHost>

(d)网络

sudo nano /etc/hosts

类型(*):

127.0.0.1   localhost
127.0.0.1   mydomain.com
127.0.1.1   aspire1.machine.com aspire1

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

创建或编辑文件:

sudo nano /etc/localhost

在此处输入(*):

aspire1

(*)aspire1用您喜欢的任何简称替换。

步骤 4:加载虚拟主机

sudo a2ensite mydomain.conf

步骤 5:创建域文件+权限

sudo mkdir -p  /var/www/mydomain.com/public_html
sudo mkdir /var/www/mydomain.com/log/
sudo touch /var/www/mydomain.com/public_html/info.php
sudo nano /var/www/mydomain.com/public_html/info.php

类型info.php

<?php

echo '<b>';
echo php_sapi_name();
echo '</b><br>';

printf("%s<br>", 'DOCUMENT_ROOT=<b>'.$_SERVER['DOCUMENT_ROOT'].'</b>');

printf("%s", 'User=<b>');
system('whoami');
echo '</b><br>';

phpinfo();

分配权限:

sudo chown -R  john:john /var/www/mydomain.com
sudo find /var/www/mydomain.com -type d -name '*' -exec chmod 700 {} \;
sudo find /var/www/mydomain.com -type f -name '*' -exec chmod 600 {} \;
# open execute
sudo chmod 711 /var/www/mydomain.com
sudo chmod 711 /var/www/mydomain.com/public_html

第 6 步:点火!

# Ubuntu 20
sudo service php7.4-fpm reload
sudo service php7.4-fpm restart
# Ubuntu 22
sudo service php8.1-fpm reload
sudo service php8.1-fpm restart
sudo systemctl restart apache2

第七步:驾驶舱!

假设您尚未启用rewritevhost 文件,请打开浏览器并输入:

mydomain.localhost/public_html/info.php

步骤 8:切换 Apache 配置

让我们从 FPM 切换到 modphp:

# Ubuntu 20
sudo a2disconf php7.4-fpm
sudo a2enmod php7.4
# Ubuntu 22
sudo a2disconf php8.1-fpm
sudo a2enmod php8.1
######
sudo a2dismod mpm_event
sudo a2dismod fcgid
sudo a2dismod proxy_fcgi
sudo a2enmod mpm_prefork
sudo systemctl restart apache2

让我们从 modphp 切换到 FPM:

# Ubuntu 20
sudo a2enconf php7.4-fpm
sudo a2dismod php7.4
# Ubuntu 22
sudo a2enconf php8.1-fpm
sudo a2dismod php8.1
######
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo a2enmod fcgid
sudo a2enmod proxy_fcgi
# Ubuntu 20
sudo service php7.4-fpm reload
# Ubuntu 22
sudo service php8.1-fpm reload
######
sudo systemctl restart apache2

祝你好运!

參考文獻:

  1. https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-ubuntu-18-04/
  2. https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-debian-10/
  3. https://askubuntu.com/questions/1029564/php-7-2-fastcgi-doesnt-work-on-ubuntu-18-04-server/1458664#1458664

相关内容