7

I've just installed LAMP on a Ubuntu 18.04 server and I can't get PHP fastcgi to work.

Here are my settings:

My fpm PHP file: /etc/php/7.2/fpm/pool.d/sites.mydomain.conf:

[sites.mydomain]
 ...
user = sites
group = sites
 ...
listen = /run/php/php7.2-fpm.sites.mydomain.sock
 ...
listen.owner = www-data
listen.group = www-data

Then:

sudo service php7.2-fpm reload
sudo service php7.2-fpm restart

My virtual host file /etc/apache2/sites-available/sites.mydomain.conf:

    ServerName sites.mydomain
    ServerAdmin webmaster@localhost
    DocumentRoot /srv/www/sites/html
<IfModule mod_fastcgi.c>
  AddHandler php7-fcgi-sites .php
  Action php7-fcgi-sites /php7-fcgi-sites
  Alias /php7-fcgi-sites /usr/lib/cgi-bin/php7-fcgi-sites
 FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi-sites -idle-timeout 60 -socket /var/run/php/php7.2-fpm.sites.mydomain.sock -pass-header Authorization

  <Directory /usr/lib/cgi-bin>
    Require all granted
  </Directory>
</IfModule>

<Directory /srv/www/sites/html>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Require all granted
</Directory>

Then:

sudo a2enmod actions
sudo service apache2 restart

But PHP is not interpreted when running scripts, pages are displayed as plain text.

Can someone tells me what's wrong in my settings?

Note: I set it exactly the same way on my Ubuntu 16.04 server and it works fine.

sotirov
  • 4,379
Duddy67
  • 535

6 Answers6

8

I unfortunately took the advice of the answers here, which either incorrect, or a tangental.

After a little digging, i realised that the module in question (proxy_fcgi) is actually part of the bionic apache2-bin package

so all that was needed was:

a2enmod proxy_fcgi
Rowy
  • 81
4

Finally there is no need to install the libapache2-mod-fastcgi package.
The trick takes place in the etc/apache2/sites-available/sites.mydomain.conf file (see my example above).
Replace the <IfModule mod_fastcgi.c> part with

<FilesMatch "\.php$">
   SetHandler "proxy:unix:///var/run/php/php7.2-fpm.sites.mydomain.sock|fcgi://sites/"
</FilesMatch>

and you're done.
Hope it helps.

Duddy67
  • 535
3

So... I did some digging on this.

First, it seems that mod_fcgid is the free version of mod_fastcgi, but sends fewer requests per stream, so is slower.

But both seem to be fairly outdated, and it seems the libapache2-mod-fastcgi package was pulled from Debian for various reasons: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=835374

However, as of apache 2.4.10, it is best to use the mod_proxy_fcgi module with php fpm. https://serverfault.com/questions/783173/differences-between-mod-fastcgi-and-mod-proxy-fcgi

Hope this helps clear things up. Most php-fpm guides around don't seem to be updated for mod_proxy_fcgi use yet, so might be causing confusion.

az4th
  • 31
0

From what I gather, it could be that libapache2-mod-fastcgi package is not available for bionic yet. As such, there is no fastcgi module available for Apache2.

Tommy
  • 101
0

I upgraded from 17.10 to 18.04 and pages were just loading with the contents of the php file rather than rendering the page. All I had to do was update my /etc/apache2/mods-enabled/fastcgi.conf file with the following:

<IfModule mod_fastcgi.c>
  AddHandler php7-fcgi-sites .php
  Action php7-fcgi-sites /php7-fcgi-sites
  Alias /php7-fcgi-sites /usr/lib/cgi-bin/php7-fcgi-sites
 FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi-sites -idle-timeout 60 -socket /var/run/php/php7.2-fpm.sock -pass-header Authorization

  <Directory /usr/lib/cgi-bin>
    Require all granted
  </Directory>
</IfModule>
0

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 for 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 admin@mydomain.com

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 &quot; ^/(.*\.php(/.*)?)$&quot; &quot;unix:/run/php/php7.4-fpm.mydomain.com.sock|fcgi://localhost/var/www/mydomain.com/&quot;
# Ubuntu 22
ProxyPassMatch &quot; ^/(.*\.php(/.*)?)$&quot; &quot;unix:/run/php/php8.1-fpm.mydomain.com.sock|fcgi://localhost/var/www/mydomain.com/&quot;

&lt;Directory /var/www/mydomain.com/&gt;
    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]

&lt;/Directory&gt;

</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/
centurian
  • 131