In this tutorial, you will learn how to install PHP-FPM with Apache2 on EC2 Ubuntu 18.04 instance.
PHP-FPM (FastCGI Process Manager) is an alternative FastCGI implementation for PHP that allows a website/application to handle high workload requests. PHP-FPM uses pool workers that can respond to PHP requests. It provides some additional features, for more information see the link https://www.php.net/manual/en/install.fpm.php.
Use PHP-FPM if you need to run different types of PHP versions on your server or computer. This guide will help you to manage your websites running in different versions of PHP.
In my previous tutorial, you will learn how to install PHP on different versions.
What will you do
- Installing Apache2 web server
- Installing PHP and PHP-FPM
- PHP-FPM Configuration
- Apache2 Configuration
- Different PHP version with PHP-FPM configuration
Requirements
- AWS Account. Create your own AWS Account
- Amazon EC2 Ubuntu 18.04. Learn how to deploy Ubuntu 18.04 on AWS
- A user with sudo privilege command.
To get started, this guide will show you through step process on how to install PHP-FPM with Apache2 on EC2 Ubuntu 18.04 instance.
Step 1. Installing Apache2
Open your terminal on your system and connect to your Ubuntu EC2 instance using SSH command. Install Apache web server from the official repository using:
sudo add-apt-repository ppa:ondrej/apache2 sudo apt update && sudo apt-get install apache2 -y
Step 2. Installing PHP and PHP-FPM
Install PHP from PPA software repositories. Use the following command below to add PPA software to your system.
sudo add-apt-repository ppa:ondrej/php sudo apt-get update
Next, you can install any PHP version as you want as per your requirements. At this time, you are going to install PHP 7.2 with PHP-FPM modules and other additional PHP packages to make sure your websites are running properly.
sudo apt install php7.2 php7.2-fpm libapache2-mod-php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-xml php7.2-mysql php7.2-cli php7.2-zip php7.2-curl
Note: If there are any additional PHP modules missing on your site you can continue with the installation later.
After installing the above packages, the php7.2-fpm service will be started automatically. To check the service, type command:
sudo service php7.2-fpm status
Output something like this:
Step 3. PHP-FPM Configuration
To configure the PHP-FPM, open the default configuration file residing at /etc/php/7.2/fpm/pool.d/www.conf
. Now open the file by using command Vim editor:
sudo vim /etc/php/7.2/fpm/pool.d/www.conf
And find the section line listen = /run/php/php7.2-fpm.sock
and replace with listen = 127.0.0.1:8990
.
Note: You can choose any port number as you want.
Save and close the file.
Next, run daemon-reload.
sudo systemctl daemon-reload
And then restart the PHP-FPM service to apply the changes.
sudo service php7.2-fpm restart
Now if everything is fine, continue on next step.
Step 4. Apache2 Configuration
There are 2 types of Apache MPM provides:
- Prefork MPM uses multiple child processes with one thread each and each process handles one connection at a time.
- Worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time.
By default, MPM is Prefork which is thread safe.
However, PHP-FPM does not work with the combination of the default Prefork MPM module. In that case, you need to switch the MPM module into the Worker MPM module for it to work.
Now to switch the MPM module type command a2dismod mpm_prefork
to disable the default MPM module:
sudo a2dismod mpm_prefork
And enable the Worker MPM module using:
sudo a2enmod mpm_worker
And then reload the Apache2 service to apply the changes.
sudo service apache2 reload
Next, enable the additional modules required for configuring multiple versions of PHP with Apache2. These modules are necessary for Apache virtual host configuration to establish an authentication method between PHP-FPM and FastCGI with the Apache2 web server.
sudo a2enmod actions fastcgi alias proxy_fcgi
Now configure the Apache2 virtual host file residing at /etc/apache2/site-available
directory to run your FPM/FastCGI configuration. You can create a new virtual host file and make sure to enable the file. In my case, I’m using the existing default virtual host file:
sudo vim /etc/apache2/sites-available/000-default.conf
And add the entry below under the <VirtualHost> section.
# PHP-FPM <FilesMatch "\.php$"> SetHandler "proxy:fcgi://127.0.0.1:8990/" </FilesMatch>
Example of 000-default.conf
virtual host configuration:
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
# Connect PHP-FPM
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:8990/"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file.
Create an phpinfo.php
file from the web root directory, type command:
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
Now run apache2ctl configtest
to verify any issue on the Apache configuration.
sudo apache2ctl configtest
If everything is fine.
Restart the Apache2 service to apply the changes:
sudo service apache2 restart
Next, open your browser and enter your server public IP address http://52.72.184.56/
.
Output something like this:
Step 5. Different PHP version with PHP-FPM configuration
You can install a different PHP version using PHP-FPM modules using the following commands:
sudo apt-get install php5.6-fpm sudo apt-get install php7.0-fpm sudo apt-get install php7.1-fpm
Then configure each PHP-FPM module using the following:
# PHP-FPM5.6
sudo vim /etc/php/5.6/fpm/pool.d/www.conf
;listen = /run/php/php5.6-fpm.sock //comment this section
listen = 127.0.0.1:8997 //add this line
sudo systemctl enable php5.6-fpm.service
sudo systemctl daemon-reload
sudo service php5.6-fpm restart
# PHP-FPM7.0
sudo vim /etc/php/7.0/fpm/pool.d/www.conf
;listen = /run/php/php7.0-fpm.sock //comment this section
listen = 127.0.0.1:8998 //add this line
sudo systemctl enable php7.0-fpm.service
sudo systemctl daemon-reload
sudo service php7.0-fpm restart
# PHP-FPM7.1
sudo vim /etc/php/7.1/fpm/pool.d/www.conf
;listen = /run/php/php7.1-fpm.sock //comment this section
listen = 127.0.0.1:8999 //add this line
sudo systemctl enable php7.1-fpm.service
sudo systemctl daemon-reload
sudo service php7.1-fpm restart
And perform the following example of Apache2 virtual host below to set up your PHP-FPM setting.
# WEB SERVER
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/www.example.com
<Directory "/var/www/www.example.com/">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
# PHP 5.6
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:8997/"
</FilesMatch>
</VirtualHost>
# API SERVER
<VirtualHost *:80>
ServerName api.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/api.example.com
<Directory "/var/www/api.example.com/">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
# PHP 7.0
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:8998/"
</FilesMatch>
</VirtualHost>
# IMAGE SERVER
<VirtualHost *:80>
ServerName images.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/images.example.com
<Directory "/var/www/images.example.com/">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
# PHP 7.1
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:8999/"
</FilesMatch>
</VirtualHost>
That’s all.
I hope this guide helped you and feel free to comment section below for more suggestions.
Tags: apache2, aws, ec2, linux, php, php-fpm, ubuntu, ubuntu 18.04