PHP 代码无法在 Web 浏览器上正确呈现

PHP 代码无法在 Web 浏览器上正确呈现

我刚刚根据以下教程安装了带有 PHP-FPM 的 Apache

如何在 Ubuntu 20.04 上使用 PHP-FPM 设置 Apache

wolf@linux:~$ dpkg -l apache2 libapache2-mod-fcgid software-properties-common php7.4 php7.4-fpm
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.29-1ubuntu4.14  amd64               Apache HTTP Server
ii  libapache2-mod-fcgid          1:2.3.9-1           amd64               FastCGI interface module for Apache 2
ii  php7.4                        7.4.12-3+ubuntu18.0 all                 server-side, HTML-embedded scripting language (metapackage)
ii  php7.4-fpm                    7.4.12-3+ubuntu18.0 amd64               server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  software-properties-common    0.96.24.32.14       all                 manage the repositories that you install software from (common)
wolf@linux:~$ 

phpinfo()使用函数测试 PHP

wolf@linux:/var/www/html$ cat info.php 
<?php phpinfo(); ?>
wolf@linux:/var/www/html$ 

目前唯一的问题是 PHP 文件似乎在 CLI 中可以正确处理,但不能通过 Web 浏览器正确处理。

wolf@linux:/var/www/html$ php info.php | head
phpinfo()
PHP Version => 7.4.12

System => Linux 4.15.0-122-generic #124-Ubuntu SMP Thu Oct 15 13:03:05 UTC 2020 x86_64
Build Date => Oct 31 2020 17:04:09
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.4/cli
Loaded Configuration File => /etc/php/7.4/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.4/cli/conf.d
wolf@linux:/var/www/html$ 

不幸的是,它无法在网络浏览器上运行

在此处输入图片描述

php7.4-fpm 服务已启动

wolf@linux:~$ systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-11-02 07:12:15 ; 1min 36s ago
     Docs: man:php-fpm7.4(8)
  Process: 739 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /e
 Main PID: 695 (php-fpm7.4)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 1129)
   CGroup: /system.slice/php7.4-fpm.service
           ├─695 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
           ├─737 php-fpm: pool www
           └─738 php-fpm: pool www

Nov 02 07:12:14 linux systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Nov 02 07:12:15 linux systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

这是 access.log 上唯一出现的内容

wolf@linux:~$ tail -F /var/log/apache2/access.log

127.0.0.1 - - [02/Nov/2020:07:23:50 ] "GET /info.php HTTP/1.1" 304 180 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"

没有新的日志在/var/log/apache2/error.log/var/log/apache2/other_vhosts_access.log

在这种情况下出现了什么问题以及我应该怎么做才能解决它?

答案1

Apache 还需要配置支持 PHP-FPM(apache2 + php-fpm)

/etc/apache2/conf-available/php7.4-fpm.conf安装 PHP-FPM 后会自动创建Apache 配置文件。

如果不存在,您也可以手动创建此文件。

/etc/apache2/conf-available/php7.4-fpm.conf

# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost" # PHP version (php -v command)
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        # Deny access to raw php sources by default
        # To re-enable it's recommended to enable access to the files
        # only in specific virtual host or directory
        Require all denied
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

然后,激活该proxy_fcgi模块:

sudo a2enmod proxy_fcgi

另外,激活配置文件:

sudo a2enconf php7.4-fpm

参考: https://develike.com/en/articles/configuring-apache-to-support-php-fpm-on-debian-ubuntu

答案2

就您显示的内容而言,处理程序尚未执行。因此,它将 PHP 文件显示为文本文件。

您是否创建了虚拟主机条目?

我检查了您链接的文档,发现缺少了一些内容。

他们在下面创建了网站的配置: /etc/apache2/sites-available/000-default.conf

但应在以下位置创建此文件的符号链接:/etc/apache2/sites-enabled/

请执行以下操作:

ls -al /etc/apache2/sites-available/
ls -al /etc/apache2/sites-enabled/
cat /etc/apache2/sites-available/000-default.conf

相关内容