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 16.04 system. In this tutorial, we are using PHP 7.2 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.
Apache Installation
Install Apache web server from the official repository. Launch terminal on your system or login with ssh to remote systems. Execute the following commands to install the latest available version of Apache web server and FastCGI module.
sudo apt update sudo apt install apache2 libapache2-mod-fastcgi
PHP Installation
For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system.
sudo apt install python-software-properties sudo add-apt-repository ppa:ondrej/php
And install PHP 7.2 the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP and PHP-FPM packages.
apt update sudo apt install php7.2 php7.2-fpm
After installing above packages php7.2-fpm service will automatically be started. You can make sure by typing below command on terminal.
sudo systemctl status php7.2-fpm ● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabl Active: active (running) since Wed 2018-02-21 10:13:28 UTC; 17s ago Docs: man:php-fpm7.2(8) Main PID: 27506 (php-fpm7.2) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" CGroup: /system.slice/php7.2-fpm.service ├─27506 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf) ├─27509 php-fpm: pool www └─27510 php-fpm: pool www Feb 21 10:13:28 tecadmin systemd[1]: Starting The PHP 7.2 FastCGI Process Manager... Feb 21 10:13:28 tecadmin systemd[1]: Started The PHP 7.2 FastCGI Process Manager.
Apache Configuration
Now enable few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with Apache server.
a2enmod actions fastcgi alias proxy_fcgi
Let’s configure the Apache VirtualHost to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in text editor. You can create new VirtualHost as per your requirements, so make sure to enable any new VirtualHost.
sudo vim /etc/apache2/sites-available/000-default.conf
Update the configuration as followings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/html <Directory /var/www/html> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> <FilesMatch \.php$> # 2.4.10+ can proxy to unix socket SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" # Else we can just use a tcp socket: #SetHandler "proxy:fcgi://127.0.0.1:9000" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
Save your changes to the configuration file and restart Apache to reload the changes.
sudo systemctl restart apache2
Test Setup
Create a PHP script with phpinfo() function and place it to your server document root. Use below command to do it.
echo " <?php phpinfo(); ?> " > /var/www/html/info.php
Open a web browser access the info.php using server IP address (for default VirtualHost) for your configured domain in Apache VirtualHost.
5 Comments
Thks bro it works well for me
Smell something funny with this line:
SetHandler “proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/”
`php5.6-fpm.sock`???
Is this copied from an old example or something?
Thanks JKK, I have corrected the issue.
How can I install php 5.4 on my Ubuntu 16.04 system with Apache?
apt install php