Php Code Is Not Rendered Properly on Web Browser

Php Code Is Not Rendered Properly on Web Browser

I've just installed Apache with PHP-FPM based on the following tutorial

How to Setup Apache with PHP-FPM on Ubuntu 20.04

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:~$ 

Test PHP with phpinfo() function

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

The only issue right now is PHP file seems to be processed properly in CLI, but not via web browser.

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$ 

Unfortunately, it's not working on web browser

php7.4-fpm service is already started

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: /
           ├─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.

This is the only things appear on 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"

There is no new log at /var/log/apache2/error.log and /var/log/apache2/other_vhosts_access.log

What wrong in this case and what should I do to troubleshoot it?

2 Answers

For what you show the handler is not being executed, so it is displaying the PHP file as if it was a text file.

Did you create the virtual host entry?.

I checked the document you link and something is missing.

They create the config for the site on:

/etc/apache2/sites-available/000-default.conf

But a symbolic link to this file should be created in: /etc/apache2/sites-enabled/

Please do:

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

so I can review what's wrong.

Cheers, Carles

Apache also needs to be configured to support PHP-FPM (apache2 + php-fpm)

The Apache configuration file /etc/apache2/conf-available/php7.4-fpm.conf is created automatically after installing PHP-FPM.

If it's not there, you can also create this file manually.

/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>

Then, activate the proxy_fcgi module:

sudo a2enmod proxy_fcgi

Also, activate the configuration file:

sudo a2enconf php7.4-fpm

Reference:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.