Installed and activated php-fpm and mpm_event, but still starting prefork automatically
Hi there,
I confess I am really not a experienced guy on this matter, and would love to count with your help please, if possible.
I followed a guide in order to install PHP-FPM with Apache on Ubuntu 22.04.
But some problems happened, and for some reason mpm_prefork still being started automatically as soon as I enable php8.1. See below, please:
sudo a2enmod php8.1
Considering dependency mpm_prefork for php8.1:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.1:
Module php8.1 already enabled
I get to disable php8.1 followed by mpm_prefork successfully. Now I try to restart Apache, but an error happens:
Job for apache2.service failed because the control process exited with error code.
After that I try again to enable php8.1, and for some reason it enables automatically mpm_prefork, when I needed to use mpm_event.
Someone has some idea about how to solve this, please?
Thanks again!
Obs: if I use a phpinfo.php file, it displays "FPM/FastCGI" for the server API line.
8 Replies
✓ Best Answer
mod-php8.1 requires mpm-prefork because it's not thread safe. You have to DISABLE it, REMOVE the package and install the php-fpm package and configure/start php81-fpm.
php81-fpm is configured additionally from php.
After that, you need to install some server glue to make apache2 to talk to php81-fpm. This is typically located in /etc/apache2/conf-available and is typically called php-fpm.conf (or something close to that).
Here's the contents of mine:
# Comments: Redirect to local php-fpm.
#
# Depends: module/mod_proxy module/mod_proxy_fcgi
#
<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:/var/run/php-fpm.sock|fcgi://localhost"
</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>
Local variations will apply. See here (for Ubuntu…but should be pretty universal to most common Linuxen): https://unixcop.com/install-apache-and-php-ubuntu-22-04/
-- sw
It looks like you're trying to enable the php 8.1 module, which you don't need with php-fpm. I found this guide which gives some commands. Specifically, a command to specifically enable the configuration for php-fpm.
sudo a2enconf php8.1-fpm
Hopefully this guide will help you or give you some ideas.
Good luck.
Blake
The link referred to above only shows how to install plain old PHP, not FPM.
This is a pretty good guide… but there are many others:
https://www.cloudbooklet.com/how-to-install-php-fpm-with-apache-on-ubuntu-22-04/
Thank you all, guys!
Thanks! That guide really helped me!
@stevewi,
Great! Thank you.
So, I need to disable and remove the php8.1
, right? Because I have not found a mod-php8.1
. Is that right?
Regarding php-fpm
, I have installed it and configured it (I think). Well, it seems it is working, I can see the status of it (through systemctl status php8.1-fpm
), etc.
About the server glue you've mentioned, following that guide, I have done the following:
I have edited my /etc/apache2/sites-available/domain.conf
and now it`s as follows:
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias www.domain.com
Protocols h2 http/1.1
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/domain.com/public_html
<Directory /var/www/html/domain.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/www/html/domain.com/logs/error.log
CustomLog /var/www/html/domain.com/logs/access.log combined
<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from localhost
</Location>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
<Directory /var/www/html/domain.com/public_html/>
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
Do you know if the above configuration is OK?
Thank you!
P.S.: now I have mpm_event running instead mpm_prefork. But I had to comment the below lines in my /etc/apache2/apache2.conf
file, due to errors regarding these "directives" when trying to start Apache:
<IfModule mpm_event_module>
StartServers 4
MaxSpareServers 25
MaxRequestWorkers 50
MaxConnectionsPerChild 2000
</IfModule>
Do the above lines really need to be removed from here?
So, I need to disable and remove the
php8.1
, right? Because I have not found amod-php8.1
. Is that right?
I think it's called libapache-php something or other…
P.S.: now I have mpm_event running instead mpm_prefork. But I had to comment the below lines in my /etc/apache2/apache2.conf file, due to errors regarding these "directives" when trying to start Apache:
<IfModule mpm_event_module>
StartServers 4
MaxSpareServers 25
MaxRequestWorkers 50
MaxConnectionsPerChild 2000
</IfModule>
Do the above lines really need to be removed from here?
These are incorrect. mpm_event uses threads instead of "servers" (mpm_prefork uses processes…hence the "server" nomenclature…a server in apache2 is a process). You want it to look something like this:
# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
ThreadLimit 512
ServerLimit 32
StartServers 25
MaxRequestWorkers 256
MinSpareThreads 32
MaxSpareThreads 64
ThreadsPerChild 128
MaxConnectionsPerChild 64
MaxRequestsPerChild 64
</IfModule>
Adjust the numbers to suit your situation…although, these would be pretty suitable for a fairly small number of user requests per hour.
-- sw
Thank you very much. I think I've found the necessary package to remove, and have already removed it (libapache2-mod-php
).
Now I checked the status of php8.1-fpm
and it returns the following:
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-02-14 18:06:56 -03; 20h ago
Docs: man:php-fpm8.1(8)
Main PID: 49343 (php-fpm8.1)
Status: "Processes active: 0, idle: 3, Requests: 2368, slow: 0, Traffic: 0req/sec"
Tasks: 4 (limit: 1029)
Memory: 194.0M
CPU: 2min 52.469s
CGroup: /system.slice/php8.1-fpm.service
├─49343 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
├─49538 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
├─53146 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
└─54754 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
I think everything is fine now, right.
And I finally edited my /etc/apache2/apache2.conf
and added the lines you suggested. Everything seems to be working fine now.
But I'll research more about every one of that directives. :)
Also, do you know if the presence of the following line on /etc/apache2/sites-available/domain.conf
is correct?
Protocols h2 http/1.1
Is that I really don't understood that in the guide I've followed, despite everything being running well now.
Thanks again, and please sorry for so many questions.
I think everything is fine now, right.
Looks like it… all the pairs of "" is just systemd being stupid (it's natural state IMHO)…
Also, do you know if the presence of the following line on /etc/apache2/sites-available/domain.conf is correct?
Protocols h2 http/1.1
The definitive documentation on Apache 2.4 can be found here: https://httpd.apache.org/docs/2.4/
The definitive documentation on Apache 2.4 directives can be found here: https://httpd.apache.org/docs/2.4/mod/directives.html
If you click on Protocols, you will find:
For example, if you want to support HTTP/2 for a server with TLS, specify:
Protocols h2 http/1.1
Valid protocols are http/1.1 for http and https connections, h2 on https connections and h2c for http connections. Modules may enable more protocols.
This is the answer you are seeking. Note h2/h2c are up and coming. Neither are standardized yet.
-- sw
Understood. Thank you very much for your help. I'll also take a look on that Apache documentation.