4

I followed directions from here to switch the server API entry will be FPM/FastCGI.from Server API Apache 2.0 Handler. When mpm_event is enabled and a2dismod php7.4 the phpinfo.php shows only:

<?php phpinfo(); ?>

Apache status:

Apache Server Status for localhost (via 127.0.0.1)

Server Version: Apache/2.4.46 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1f

Server MPM: event

Server Built: 2020-11-13T01:36:38

 ----------------------------------------------------------------------

Current Time: Saturday, 27-Feb-2021 13:08:50 EST

Restart Time: Saturday, 27-Feb-2021 13:05:50 EST

Parent Server Config. Generation: 1

Parent Server MPM Generation: 0

Server uptime: 3 minutes

Server load: 0.80 0.95 1.07

Total accesses: 14 - Total Traffic: 190 kB - Total Duration: 10069

CPU Usage: u0 s.05 cu0 cs0 - .0278% CPU load

.0778 requests/sec - 1080 B/second - 13.6 kB/request - 719.214 ms/request

1 requests currently being processed, 49 idle workers

+------------------------------------------------------------------------+ | | | |Connections |Threads |Async connections | |Slot|PID |Stopping|---------------+---------+--------------------------| | | | |total|accepting|busy|idle|writing|keep-alive|closing| |----+-----+--------+-----+---------+----+----+-------+----------+-------| |0 |46738|no |0 |yes |1 |24 |0 |0 |0 | |----+-----+--------+-----+---------+----+----+-------+----------+-------| |1 |46739|no |0 |yes |0 |25 |0 |0 |0 | |----+-----+--------+-----+---------+----+----+-------+----------+-------| |Sum |2 |0 |0 | |1 |49 |0 |0 |0 | +------------------------------------------------------------------------+

_______________W__________________________________.............. ................................................................ ......................

Scoreboard Key: "_" Waiting for Connection, "S" Starting up, "R" Reading Request, "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup, "C" Closing connection, "L" Logging, "G" Gracefully finishing, "I" Idle cleanup of worker, "." Open slot with no current process

If I enable php7.4 and switch back on mpm_prefork, it will restore to Apache 2.0 Handler. Is something not right what I am doing?

Latest note: it partially works, but PHP pages still not shown correctly.

The PHP pages are shown correctly now and the MySQL connection test works. After I entered a2enmod php7.4 which should have switched to Server API Apache 2.0 Handler get the error below but switched to FPM/FastCGI.

Besides is capture here for future references it was 3 nights of work: enable/disable modules, dependencies conflicts, updates, configurations and logging. Copy&Paste from other posts always sucks. Thanks!

SOLVED

a2enmod php7.4
    Considering dependency mpm_prefork for php7.4:
    Considering conflict mpm_event for mpm_prefork:
    ERROR: Module mpm_event is enabled - cannot proceed due to conflicts. It needs to be disabled first!
    Considering conflict mpm_worker for mpm_prefork:
    ERROR: Could not enable dependency mpm_prefork for php7.4, aborting

enter image description here

sotirov
  • 4,379

2 Answers2

6

Setting up PHP-FPM with Apache is much more complicated than simply installing FPM and enabling a few things. It requires additiona modules (FastCGI) to be installed among other things enabled and disabled.

Funnily enough, the Digital Ocean guide for using MPM Event and PHP-FPM via FastCGI on Apache with Ubuntu 18.04 is still a relatively good jump-off point for configurations of this setup, we just have to tweak some things and adjust bits for some changes between 18.04 and 20.04. The below instructions are based off the DO guide but with the requisite tweaks to make FPM work in 20.04, adapting PHP versions, etc. and giving you just the commands.

Step 1: Changing the Multi-Processing Module

The php7.4 module in Apache is the inbuilt one - unfortunately it doesn't work for PHP-FPM. So we need to do some work to make this work.

First, stop Apache while we make tweaks to the configs

sudo systemctl stop apache2

Then, disable the in-built prefork driven PHP 7.4 module

sudo a2dismod php7.2

Disable the prefork module.

sudo a2dismod mpm_prefork

Enable the event mpm module

sudo a2enmod mpm_event

Now we configure the PHP components, which is fairly straightforward...

Step 2: Configure Apache's FastCGI manager with PHP

If you haven't done this already, install PHP FPM

sudo apt install php-fpm

If that command doesn't work, use this one:

sudp apt install php7.4-fpm

Install the FCGI module for Apache

sudo apt install libapache2-mod-fcgid

Enable requisite libraries and modules

a2enmod proxy_fcgi setenvif

Enable the PHP-FPM module's configuration (which will already be present)

sudo a2enconf php7.4-fpm

Now we need to make sure the syntax passes in your config.

Step 3: Make sure the configuration passes the tests.

Run this command:

sudo apachectl configtest

This command may spit out more information such as this, but as long as "Syntax OK" is the output you're good (run from a test container/environment, ignore the AH00558 error if it shows up and worry only about the 'Syntax OK' output):

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.73.252.124. Set the 'ServerName' directive globally to suppress this message
Syntax OK

Now that config test has passed...

Step 4: Re-enable Apache service

sudo systemctl restart apache2

You should now get PHP working again, except with the FPM service. And PHP files should work again.

Nuke your browser cache before testing, though - web browsers are NOTORIOUS for caching error messages.


If you've done everything here, you should see PHP working as expected - this is phpinfo on my test env running 20.04:

enter image description here

Thomas Ward
  • 78,878
0

To add a bit to Thomas' excellent answer, here's an easy way to tell when you're running FPM vs mod-php

The default way PHP is loaded is as an Apache module. You can tell if you're using this by checking the Apache config list. Navigate to /etc/apache2/mods-enabled. If you're using mod-php, you'll have two files symlinked (where X.X is the version of PHP)

  • phpX.X.conf
  • phpX.X.load

The first file is telling Apache to use PHP for files that end in .php and the second is loading the PHP mod-php module.

Manual process

If you're looking to do this manually, the config for FPM will be in /etc/apache2/conf-enabled and will look like this

# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php8.c>
<IfModule proxy_fcgi_module>
    ProxyTimeout 90
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>
# Using (?:pattern) instead of (pattern) is a small optimization that
# avoid capturing the matching pattern (as $1) which isn't used here
&lt;FilesMatch &quot;.+\.ph(?:ar|p|tml)$&quot;&gt;
    SetHandler &quot;proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost&quot;
&lt;/FilesMatch&gt;

The default configuration works for most of the installation, however it could

be improved in various ways. One simple improvement is to not pass files that

doesn't exist to the handler as shown below, for more configuration examples

see https://wiki.apache.org/httpd/PHP-FPM

<FilesMatch ".+.ph(?:ar|p|tml)$">

<If "-f %{REQUEST_FILENAME}">

SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"

</If>

</FilesMatch>

&lt;FilesMatch &quot;.+\.phps$&quot;&gt;
    # 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
&lt;/FilesMatch&gt;
# Deny access to files without filename (e.g. '.php')
&lt;FilesMatch &quot;^\.ph(?:ar|p|ps|tml)$&quot;&gt;
    Require all denied
&lt;/FilesMatch&gt;

</IfModule> </IfModule>

The key line is this (PHP 8.1 shown)

SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"

When you have FPM installed and running (systemctl status phpX.X-fpm) you should have a socket file in /run/php That config line should point to the current socket file.

Upgrading

The main reason to do this manually is that once you have FPM installed, upgrading PHP versions can be a pain. Your fresh install of PHP will come with its own default configuration (i.e. you should really edit /etc/php/X.X/fpm/pool.d/www.conf file to match your previous config). Then you can stop the old FPM server, start the new one, and point Apache over there with minimal effort.

Machavity
  • 187