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.:
- https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-ubuntu-18-04/
- https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-debian-10/
- https://askubuntu.com/questions/1029564/php-7-2-fastcgi-doesnt-work-on-ubuntu-18-04-server/1458664#1458664