Score:1

Configuring PHP-FPM in Apache

lb flag

I installed Apache/2.4.54, PHP72-FPM, PHP74-FPM and some other versions of PHP-FPM on Arch Linux. Apparently there is some misconfiguration in httpd.conf, PHP can`t interpret anything and browser comments out the code.

What I tried:

httpd.conf:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

httpd-vhosts.conf:

<VirtualHost *:80>
    LoadModule php7_module modules/libphp72.so
    AddHandler php72-script php
    Include conf/extra/php72-module.conf
    ...

php72-module.conf:

<IfModule dir_module>
    <IfModule php_module7>
        <FilesMatch "\.php$">
            SetHandler application/x-httpd-php
            SetHandler "proxy:unix:/run/php72-fpm/php-fpm.sock|fcgi://localhost/"
            ...

php72-fpm is active.

and

php -v returns: PHP 8.1.12 (cli)


Updated:

I moved:

<FilesMatch "\.php$">
    SetHandler "proxy:unix:/run/php72-fpm/php-fpm.sock|fcgi://localhost/"

to <VirtualHost> and got [503 Unavailable],

Error log:

[proxy:error] FCGI: attempt to connect to Unix domain socket /run/php-fpm72/php-fpm.sock (*:80) failed
[proxy_fcgi:error] failed to make connection to backend: httpd-UDS
Score:2
in flag

I believe you are mixing php-fpm with modphp in your configuration. The only steps you should have to do are these:

sudo a2dismod php7.x          # disable modphp (optional)
sudo a2dismod mpm_prefork     # to use php-fpm, you need to switch to mpm_event
sudo a2enmod mpm_event        # enable mpm_event
sudo a2enmod proxy proxy_fcgi # enable proxy_fcgi, required for php-fpm
sudo a2enconf php7.2-fpm      # enable php7.2-fpm

There is no need to update your vhosts file, unless Archlinux doesn't come with the apache helpers a2*.

Update following your update

Make sure you have a unix socket at that specific location. You should probably verify in your /etc/php/fpm/pool.d/xxx.conf configuation the exact location of your socket and then restart php-fpm.

Phree avatar
lb flag
I enabled `mpm_event` before I try it again, got: `Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.`
in flag
@Phree You need to disable modphp alltogether (as in the first of the list of commands) - are you sure that there is not one single (version of) modphp is running?
Phree avatar
lb flag
I dont see modphp in uncommented modules in `httpd.conf` and already have commented out these lines: `LoadModule php_module modules/libphp.so & AddHandler php-script php & Include conf/extra/php_module.conf` @zenlord
in flag
@Phree Can you try uninstalling all php***-apache packages to be sure you're not using modphp? Once modphp is disabled, you should be able to enable the mpm_event module. The Apache wiki shows how to set AddHandler to point to a unix socket: https://cwiki.apache.org/confluence/display/httpd/PHP-FPM (and if I'm not mistaken in your AddHandler line 'php-script' should be replaced with a suitable regular expression like `^/(.*\.php)$`
Phree avatar
lb flag
Thanks for your guides
Phree avatar
lb flag
Rewriting `\.php$` with another regex pattern worked with `php80-fpm`. BTW I came up with `Prefork MPM` and it worked.
Score:0
za flag

For Ubuntu 20-22 users:

Purpose:

Install fpm with sockets + fcgid module + apply user rights on requests per domain.

step 1: installation

sudo apt-get update
sudo systemctl stop apache2
sudo apt-get install libapache2-mod-fcgid
sudo apt-get install php8.1-fpm

# ubuntu 20 missing libs?!
sudo apt-get install php-mbstring
sudo apt-get install php-gd

step 2: disable/enable modules

sudo a2dismod php* mpm_prerfork cgi cgid
sudo a2enmod mpm_event fcgid proxy_fcgi headers rewrite
# Ubuntu 20
sudo a2enconf php7.4-fpm
# Ubuntu 22
sudo a2enconf php8.1-fpm

step 3: add configuration commands

User: "john"

Domain: "mydomain.com"

(a). Apache FPM configuration

sudo nano /etc/apache2/mods-available/fcgid.conf

type:

<IfModule mod_fcgid.c>
  FcgidConnectTimeout 20
  AddType   application/x-httpd-php .php
  AddHandler    application/x-httpd-php .php
  <IfModule mod_mime.c>
    AddHandler fcgid-script .fcgi
  </IfModule>
</IfModule>

(b). FPM configuration

# Ubuntu 20
sudo cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/mydomain.com.conf
# Ubuntu 22
sudo cp /etc/php/8.1/fpm/pool.d/www.conf /etc/php/8.1/fpm/pool.d/mydomain.com.conf

change:

[www] -> [mydomain.com]
user = www-data -> user = john
group = www-data -> group = john

add or leave empty:

;;;;;;;;;;
; Memory ;
;;;;;;;;;;
php_admin_value[memory_limit] = 12M
php_admin_value[post_max_size] = 12M
php_admin_value[upload_max_filesize] = 12M
;;;;;;;;;;;;
; Settings ;
;;;;;;;;;;;;
php_value[user_ini.filename] = ".user.ini"
php_value[default_charset] = "UTF-8"
php_flag[short_open_tag] = Off
php_flag[display_errors] = On
php_flag[display_startup_errors] = On
php_flag[log_errors] = On
php_value[log_errors_max_len] = 1024
php_flag[report_memleaks] = On
php_flag[html_errors] = On
php_value[error_reporting] = E_ALL & ~E_DEPRECATED & ~E_STRICT
php_flag[file_uploads] = On
php_value[max_file_uploads] = 20
php_flag[allow_url_fopen] = On
php_flag[allow_url_include] = Off
php_value[session.save_handler] = files
php_flag[session.use_strict_mode] = 1
php_flag[session.use_cookies] = 1
php_flag[session.use_only_cookies] = 1
; temporary disabled fro STEP 5 system('whoami') to run!
;php_admin_value[disable_functions] = exec,passthru,shell_exec,system
;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
php_admin_value[error_log] = "/var/www/mydomain.com/log/php_errors.log"
php_admin_value[open_basedir] = "/var/www/mydomain.com/"
php_admin_value[doc_root] = "/var/www/mydomain.com"
php_admin_value[sys_temp_dir] = "/var/www/mydomain.com/tmp"
php_admin_value[upload_tmp_dir] = "/var/www/mydomain.com/tmp"
php_admin_value[session.save_path] = "/var/www/mydomain.com/session"
php_admin_value[soap.wsdl_cache_dir] = "/var/www/mydomain.com/tmp"
php_admin_value[curl.cainfo] = "/var/www/mydomain.com/server/registry/cacert.pem"

php_admin_value, php_admin_flag: can't be changed by user

php_value, php_flag: can be changed by user

(c). Create vhost

sudo nano /etc/apache2/sites-available/mydomain.com.conf

type:

<VirtualHost *:80>
ServerName mydomain.com.localhost
ServerAlias mydomain.com.localhost
ServerAdmin [email protected]

DocumentRoot /var/www/mydomain.com

Header set Access-Control-Allow-Origin "*"

<IfModule mod_fcgid.c>
    FcgidConnectTimeout 20
    AddType   application/x-httpd-php .php
    AddHandler    application/x-httpd-php .php

    # Ubuntu 20
    ProxyPassMatch " ^/(.*\.php(/.*)?)$" "unix:/run/php/php7.4-fpm.mydomain.com.sock|fcgi://localhost/var/www/mydomain.com/"
    # Ubuntu 22
    ProxyPassMatch " ^/(.*\.php(/.*)?)$" "unix:/run/php/php8.1-fpm.mydomain.com.sock|fcgi://localhost/var/www/mydomain.com/"

    <Directory /var/www/mydomain.com/>
        Options +ExecCGI
        Options -Indexes
        AllowOverride None
        Require all granted
#        FRONT CONTROLLER PATTERN
#        RewriteEngine On
#        RewriteCond "%{REQUEST_URI}" "!=/public_html/index.php"
#        RewriteRule "^(.*)$" "/public_html/index.php?$1" [NC,NE,L,PT,QSA]
    </Directory>
</IfModule>

# CPU usage limits 5s 10s
RLimitCPU 5 10
# memory limits to 10M 20M
RLimitMEM 10000000 20000000
# limit of forked processes 20 30
RLimitNPROC 20 30
LogLevel warn
ErrorLogFormat connection "[%t] New connection: [%{c}L] [ip: %a]"
ErrorLogFormat request "[%t] [%{c}L] New request: [%L] [pid %P] %F: %E"
ErrorLogFormat "[%t] [%{c}L] [%L] [%l] [pid %P] %F: %E: %M"
ErrorLog /var/www/mydomain.com/log/apache_error.log
CustomLog /var/www/mydomain.com/log/apache_access.log combined
ServerSignature Off
</VirtualHost>

(d). Network

sudo nano /etc/hosts

type(*):

127.0.0.1   localhost
127.0.0.1   mydomain.com
127.0.1.1   aspire1.machine.com aspire1

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

create or edit file:

sudo nano /etc/localhost

type there(*):

aspire1

(*)replace aspire1 with any short name you like.

step 4: load vhost

sudo a2ensite mydomain.conf

step 5: create domain files + permissions

sudo mkdir -p  /var/www/mydomain.com/public_html
sudo mkdir /var/www/mydomain.com/log/
sudo touch /var/www/mydomain.com/public_html/info.php
sudo nano /var/www/mydomain.com/public_html/info.php

at info.php type:

<?php

echo '<b>';
echo php_sapi_name();
echo '</b><br>';

printf("%s<br>", 'DOCUMENT_ROOT=<b>'.$_SERVER['DOCUMENT_ROOT'].'</b>');

printf("%s", 'User=<b>');
system('whoami');
echo '</b><br>';

phpinfo();

Assign permissions:

sudo chown -R  john:john /var/www/mydomain.com
sudo find /var/www/mydomain.com -type d -name '*' -exec chmod 700 {} \;
sudo find /var/www/mydomain.com -type f -name '*' -exec chmod 600 {} \;
# open execute
sudo chmod 711 /var/www/mydomain.com
sudo chmod 711 /var/www/mydomain.com/public_html

step 6: Ignition!

# Ubuntu 20
sudo service php7.4-fpm reload
sudo service php7.4-fpm restart
# Ubuntu 22
sudo service php8.1-fpm reload
sudo service php8.1-fpm restart
sudo systemctl restart apache2

step 7: cockpit!

Supposing you haven't enabled rewrite at the vhost file, open browser and type:

mydomain.localhost/public_html/info.php

step 8: switch Apache configuration

Let's switch from FPM to modphp:

# Ubuntu 20
sudo a2disconf php7.4-fpm
sudo a2enmod php7.4
# Ubuntu 22
sudo a2disconf php8.1-fpm
sudo a2enmod php8.1
######
sudo a2dismod mpm_event
sudo a2dismod fcgid
sudo a2dismod proxy_fcgi
sudo a2enmod mpm_prefork
sudo systemctl restart apache2

Let's switch from modphp to FPM:

# Ubuntu 20
sudo a2enconf php7.4-fpm
sudo a2dismod php7.4
# Ubuntu 22
sudo a2enconf php8.1-fpm
sudo a2dismod php8.1
######
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo a2enmod fcgid
sudo a2enmod proxy_fcgi
# Ubuntu 20
sudo service php7.4-fpm reload
# Ubuntu 22
sudo service php8.1-fpm reload
######
sudo systemctl restart apache2

Good luck!

Ref.:

  1. https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-ubuntu-18-04/
  2. https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-debian-10/
  3. https://askubuntu.com/questions/1029564/php-7-2-fastcgi-doesnt-work-on-ubuntu-18-04-server/1458664#1458664
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.