- How to Install Apache with PHP-FPM on Ubuntu 20.04
- Step 1 – Installing Apache
- Step 2 – Install PHP with FPM
- Step 3 – Apache Configuration
- RatserX / debian-ubuntu-apache-fastcgi-php.md
- Install and configure Apache and php with mod fastcgi on Ubuntu/Debian
- Install the packages
- Configure fastcgi
- Wrapper script
- Resource
- 4 Comments
How to Install Apache with PHP-FPM on Ubuntu 20.04
PHP FPM (FastCGI Process Manager) is an alternative implementation of PHP FastCGI. It provides some additional features like Adaptive process spawning which is useful for sites. This tutorial will help you to install Apache with PHP-FPM/FastCGI on Ubuntu 20.04 system. In this tutorial, we are using PHP 7.4 and configure with Apache using PHP-FPM and FastCGI.
You can also visit the previous tutorial to configure Apache with multiple PHP versions using PHP-FPM/FastCGI on Ubuntu systems.
Step 1 – Installing Apache
Apache web server debian packages are available under the default repositories. Login to your Ubuntu system with sudo privileges account. Open a terminal and execute the following commands:
sudo apt update sudo apt install apache2 libapache2-mod-fcgid
The above commands will install Apache and FastCGI module to your server.
Step 2 – Install PHP with FPM
Next, install PHP and PHP-FPM on your Ubuntu system. For this tutorial, we choose PHP 7.4 to install using
For the PHP installation we recommend to use ppa:ondrej/php PPA. Execute below couple of commands to add the PPA to your system.
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php
Then install PHP 7.4 (or required version) the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP and PHP-FPM packages.
sudo apt update sudo apt install php7.4 php7.4-fpm
Note:- When you are using PHP-FPM. All the PHP modules configurations are residing under /etc/php/7.4/fpm directory. You can read more about enable/disable PHP modules.
After installing the packages php7.4-fpm service will automatically be started. To make sure, type:
sudo systemctl status php7.4-fpm ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-05-09 04:41:44 UTC; 19s ago Docs: man:php-fpm7.4(8) Process: 375077 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited> Main PID: 375073 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 2283) Memory: 9.3M CGroup: /system.slice/php7.4-fpm.service ├─375073 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─375075 php-fpm: pool www └─375076 php-fpm: pool www May 09 04:41:43 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager. May 09 04:41:44 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
Step 3 – Apache Configuration
Now, You need to enable some of the Apache modules required for the FastCGI confiugration. You can enable the requird module by running command:
sudo a2enmod actions fcgid alias proxy_fcgi
Then configure Apache Virtual Host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in a text editor. You can also create a new configuration as per your choice.
sudo vim /etc/apache2/sites-available/000-default.conf
Update the configuration as followings.
RatserX / debian-ubuntu-apache-fastcgi-php.md
Two handlers will be created, one for each PHP version installed.
# PHP 5.6 vim /var/www/cgi-bin/php56.fastcgi chmod +x /var/www/cgi-bin/php56.fastcgi chown www-data:www-data /var/www/cgi-bin/php56.fastcgi # PHP 7.0 vim /var/www/cgi-bin/php70.fastcgi chmod +x /var/www/cgi-bin/php70.fastcgi chown www-data:www-data /var/www/cgi-bin/php70.fastcgi
#!/bin/bash PHPRC="/etc/php/5.6/cgi/php.ini" PHP_FCGI_CHILDREN=4 PHP_FCGI_MAX_REQUESTS=1000 export PHPRC export PHP_FCGI_CHILDREN export PHP_FCGI_MAX_REQUESTS exec /usr/lib/cgi-bin/php5.6
#!/bin/bash PHPRC="/etc/php/7.0/cgi/php.ini" PHP_FCGI_CHILDREN=4 PHP_FCGI_MAX_REQUESTS=1000 export PHPRC export PHP_FCGI_CHILDREN export PHP_FCGI_MAX_REQUESTS exec /usr/lib/cgi-bin/php7.0
Step 5 — Configure the sites
Change directory to the default web directory.
# Ubuntu 16.04 cd /var/www # Ubuntu 18.04 cd /var/www/html
Create the folders each site.
mkdir php56-example php70-example
Create the index files for each site.
vim php56-example/index.php vim php70-example/index.php
Step 6 — Add the listen ports
Open the configuration file on /etc/apache2/ports.conf and add the following ports.
Step 7 — Setup the virtual hosts
Two vhosts will be created, one for each site, on ports 81 and 82 respectively.
cd /etc/apache2/sites-available vim php56-example.conf vim php70-example.conf
ServerName php56-example ServerAdmin php56-example@example.com DocumentRoot /var/www/html/php56-example ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" Options +Indexes +FollowSymLinks +ExecCGI AddHandler fcgid-script .php FCGIWrapper /var/www/cgi-bin/php56.fastcgi .php AllowOverride All Order allow,deny Allow from All
ServerName php70-example ServerAdmin php70-example@example.com DocumentRoot /var/www/html/php70-example ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" Options +Indexes +FollowSymLinks +ExecCGI AddHandler fcgid-script .php FCGIWrapper /var/www/cgi-bin/php70.fastcgi .php AllowOverride All Order allow,deny Allow from All
Disable the default PHP modules.
Enable the necessary modules.
a2enmod actions alias fcgid proxy_fcgi
Enable the corresponding sites.
a2ensite php56-example a2ensite php70-example
Step 10 — Setup the firewall
sudo ufw allow 'Apache' sudo ufw allow 80/tcp sudo ufw allow 81/tcp sudo ufw disable sudo ufw enable
- Typical log locations
- /var/log/httpd/error_log
- Description: Usually this is caused by incorrect line endings on the .fastcgi files. Change the end of line sequence from CRLF to LF or recreate the file using vim.
- References: 1
Install and configure Apache and php with mod fastcgi on Ubuntu/Debian
Mod_fastcgi is an apache module that enables apache to talk to fastcgi enabled applications. It can be used to run php code over fastcgi through the php-cgi binary which is fastcgi enabled.
Fastcgi has many improvements over the traditional cgi model of executing external programs inside a webserver. With fastcgi multiple processes are kept alive and each process is reused to serve multiple requests one after another. There are settings to control the maximum number of requests a process can serve, after which the process is terminated and a new one is started.
Install the packages
The first thing to do is install the necessary packages from synaptic. We need to install the apache server, mod_fastcgi, mpm worker and php along with the cgi binary. Note that if you already have php installed with mpm prefork and mod-php then it would be removed upon installing these packages
$ sudo apt-get install apache2 libapache2-mod-fastcgi apache2-mpm-worker php5 php5-cgi
On ubuntu the apache configuration file located at
/etc/apache2/sites-enabled/000-default
The php cgi binary is located at /usr/bin/php-cgi.
Configure fastcgi
After installing the packages, its time to configure apache to use mod_fastcgi to run php scripts. Like cgi, fastcgi will also run php processes using the php-cgi binary.
First enable the fastcgi module with a2enmod command
The a2enmod command copies the configuration file of the module from /etc/apache2/mods-available to the directory /etc/apache2/mods-enabled. The configuration file in this case is fastcgi.conf. It looks like this
AddHandler fastcgi-script .fcgi FastCgiIpcDir /var/lib/apache2/fastcgi Mod_fastcgi registers a handler called fastcgi-script with apache. This can be used to specify which programs to execute through mod_fastcgi.
Ok, lets move on. Next thing is to configure the relevant vhost to run php using the fastcgi handler. Put the following configuration inside the desired vhost block in the apache configuration file.
FastCgiServer /usr/local/bin/php-fastcgi-wrapper -processes 10 -restart-delay 1 -init-start-delay 1 -pass-header HTTP_AUTHORIZATION Alias /binary /usr/local/bin
Options ExecCGI SetHandler fastcgi-script AddHandler php-fastcgi .php Action php-fastcgi /binary/php-fastcgi-wrapperThe FastCgiServer registers /usr/local/bin/php-fastcgi-wrapper as the fastcgi application with various options.
Note that this is a shell script that will launch the php-cgi binary with various settings. Note that we asked the FastCgiServer to create and manage 10 processes.The AddHandler line declares a new handler for «.php» files called php-fastcgi. This can named to anything you like.
The Action line tells apache to handle php-fastcgi file using the cgi program /cgi-bin/php-fastcgi-wrapper.Now the location /cgi-bin/php-fastcgi-wrapper is not a real one. To make it point to the real location of wrapper script, the Alias directive is used. It points /cgi-bin to /usr/local/bin.
Wrapper script
Now comes the wrapper script that will be used by fastcgi to run php.
#!/bin/sh # Set desired PHP_FCGI_* environment variables. # Example: # PHP FastCGI processes exit after 500 requests by default. PHP_FCGI_MAX_REQUESTS=10000 export PHP_FCGI_MAX_REQUESTS PHP_FCGI_CHILDREN=5 export PHP_FCGI_CHILDREN # Replace with the path to your FastCGI-enabled PHP executable exec /usr/bin/php-cgi
Note the PHP_FCGI_CHILDREN setting. It specifies that each php process should further fork and manage 5 more child php processes. So earlier fastcgi was told to create 10 processes. The total is 10*5 = 50 processes. There are 2 levels of process management going on. Mod_fastcgi manages 10 php processes and each php process further manages 5 process each.
This file is stored at the following path
/usr/local/bin/php-fastcgi-wrapper
Make sure that the file is executable. Do a chmod on it
/usr/local/bin# chmod +x php-fastcgi-wrapper
The location of the file does not matter. The wrapper script is necessary so that various options like PHP_FCGI_MAX_REQUESTS can be passed on to the php process.
Now restart apache and test the setup by opening a php script in browser. View the contents of the $_SERVER variable in php. It should contain [FCGI_ROLE] => RESPONDER. This indicates that fastcgi is in action.
Also check the process table using htop or System Monitor. You should see 50 php-cgi processes running.
Resource
A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .
4 Comments
- tylercollier July 25, 2014 at 11:11 pm My previous comment about “client denied by server configuration: /usr/local/bin/php-fastcgi-wrapper” has not yet been approved, so I can’t see, edit, or reply to it. But I wanted to post my fix. From: http://stackoverflow.com/a/13923526/135101. Add [Directory /usr/local/bin]
Require all granted
[/Directory] to your vhost config. EDIT: I guess I can’t use markdown here and Disqus is munging my “html” of the directory element, so I replaced angle brackets with brackets.
- tylercollier July 25, 2014 at 11:11 pm I had to enable the multiverse source, but only from Ubuntu 14.04 (see instructions here: http://serversforhackers.com/articles/2014/05/05/apache-proxy-fcgi/. I just needed the section labeled Ubuntu 14.04). I didn’t on 12.04. I also had to run `sudo a2enmod actions`.