php8.1 无法与 php 处理程序 php8.1-fpm 配合使用:浏览器显示 php 源代码

php8.1 无法与 php 处理程序 php8.1-fpm 配合使用:浏览器显示 php 源代码

我在 Ubuntu 22.04.1 LTS 上安装了以下 Ubuntu 存储库包:

dpkg -l apache2 php php-fpm libapache2-mod-php libapache2-mod-fcgid
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                 Version           Architecture Description
+++-====================-=================-============-=========================================================================
ii  apache2              2.4.52-1ubuntu4.1 amd64        Apache HTTP Server
ii  libapache2-mod-fcgid 1:2.3.9-4         amd64        FastCGI interface module for Apache 2
ii  libapache2-mod-php   2:8.1+92ubuntu1   all          server-side, HTML-embedded scripting language (Apache 2 module) (default)
ii  php                  2:8.1+92ubuntu1   all          server-side, HTML-embedded scripting language (default)
ii  php-fpm              2:8.1+92ubuntu1   all          server-side, HTML-embedded scripting language (FPM-CGI binary) (default)
$ apache2 -v
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2022-06-14T12:30:21
$ php -v
PHP 8.1.2-1ubuntu2.6 (cli) (built: Sep 15 2022 11:30:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.6, Copyright (c), by Zend Technologies

但是php8.1不使用 php 处理程序时php8.1-fpm,浏览器会显示 PHP 源代码。

我的 VirtualHost 中有以下配置:

$ grep -1 php/ /etc/apache2/sites-enabled/helpdesk.media-participations.com-ssl.conf
        <FilesMatch \.php$>
                SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost/"
        </FilesMatch>

状态如下:

$ \ls -l /run/php/php8.1-fpm.*
-rw-r--r-- 1 root     root     5 Nov  4 15:10 /run/php/php8.1-fpm.pid
srw-rw---- 1 www-data www-data 0 Nov  4 15:10 /run/php/php8.1-fpm.sock
$ a2query -m | grep php
php8.1 (enabled by site administrator)
$ sudo a2dissite 000-default
Site 000-default disabled.
To activate the new configuration, you need to run:
  systemctl reload apache2
$ sudo systemctl reload apache2
$ systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-11-04 16:22:18 CET; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 76811 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 76816 (apache2)
      Tasks: 7 (limit: 9365)
     Memory: 17.7M
        CPU: 102ms
     CGroup: /system.slice/apache2.service
             ├─76816 /usr/sbin/apache2 -k start
             ├─76817 /usr/sbin/apache2 -k start
             ├─76818 /usr/sbin/apache2 -k start
             ├─76819 /usr/sbin/apache2 -k start
             ├─76820 /usr/sbin/apache2 -k start
             ├─76821 /usr/sbin/apache2 -k start
             └─76822 /usr/sbin/apache2 -k start

Nov 04 16:22:18 my-server systemd[1]: Starting The Apache HTTP Server...
Nov 04 16:22:18 my-server apachectl[76814]: AH00558: apache2: Could not reliably determine the server_s fully qualified domain name, using 127.0.1.1. Set the 'ServerName'>
Nov 04 16:22:18 my-server systemd[1]: Started The Apache HTTP Server.
$ apachectl configtest
AH00558: apache2: Could not reliably determine the server_s fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
$ systemctl status php8.1-fpm.service
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset:>
     Active: active (running) since Fri 2022-11-04 15:10:01 CET; 1h 22min ago
       Docs: man:php-fpm8.1(8)
    Process: 76090 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php>
   Main PID: 76087 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 9365)
     Memory: 11.1M
        CPU: 471ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─76087 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "">
             ├─76088 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
             └─76089 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">

Nov 04 15:10:01 my-server systemd[1]: Starting The PHP 8.1 FastCGI Process Manager>
Nov 04 15:10:01 my-server systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
$ ps -fC php-fpm8.1
UID          PID    PPID  C STIME TTY          TIME CMD
root       76087       1  0 15:10 ?        00:00:00 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
www-data   76088   76087  0 15:10 ?        00:00:00 php-fpm: pool www
www-data   76089   76087  0 15:10 ?        00:00:00 php-fpm: pool www
$ sudo cat /var/log/php8.1-fpm.log
[04-Nov-2022 14:15:53] NOTICE: fpm is running, pid 68585
[04-Nov-2022 14:15:53] NOTICE: ready to handle connections
[04-Nov-2022 14:15:53] NOTICE: systemd monitor interval set to 10000ms
[04-Nov-2022 14:15:54] NOTICE: Terminating ...
[04-Nov-2022 14:15:54] NOTICE: exiting, bye-bye!
[04-Nov-2022 14:15:54] NOTICE: fpm is running, pid 68708
[04-Nov-2022 14:15:54] NOTICE: ready to handle connections
[04-Nov-2022 14:15:54] NOTICE: systemd monitor interval set to 10000ms
[04-Nov-2022 14:50:38] NOTICE: Terminating ...
[04-Nov-2022 14:50:38] NOTICE: exiting, bye-bye!
[04-Nov-2022 15:10:01] NOTICE: fpm is running, pid 76067
[04-Nov-2022 15:10:01] NOTICE: ready to handle connections
[04-Nov-2022 15:10:01] NOTICE: systemd monitor interval set to 10000ms
[04-Nov-2022 15:10:01] NOTICE: Terminating ...
[04-Nov-2022 15:10:01] NOTICE: exiting, bye-bye!
[04-Nov-2022 15:10:01] NOTICE: fpm is running, pid 76087
[04-Nov-2022 15:10:01] NOTICE: ready to handle connections
[04-Nov-2022 15:10:01] NOTICE: systemd monitor interval set to 10000ms

以下是我得到的结果:

$ curl -k https://localhost/info.php
<?php
phpinfo();
phpinfo(INFO_MODULES);
?>

看起来 PHP 代码没有被服务器解释。

我怎样才能解决这个问题 ?

答案1

Apache 无法解释 PHP 的原因可能有很多。根据您分享的内容,启用proxy_fcgi模块应该可以解决问题。运行sudo a2enmod proxy_fcgi并重新启动 Apache 应该就足够了。

但是,在下面的代码中,我添加了一些额外的命令(在您的特定情况下可能不需要),但如果这不能解决问题,请运行它们。

sudo a2enmod proxy
sudo a2enmod proxy_fcgi
sudo a2enmod setenvif
sudo a2enconf php8.1-fpm
sudo service apache2 restart

相关内容