How to Setup Apache with PHP-FPM on Ubuntu 16.04
Posted on July 17th, 2019
It is a very easy task to set up a basic LAMP (Linux, Apache, MySQL, and PHP) stack on your server. But the problem with the basic stack using PHP module is, it is very slow. At some point in time, you will have to set up PHP-FPM with Apache to make your server handle more traffic efficiently.
There are two issues with the basic LAMP stack. The first one is, It will use Apache’s mpm_perfork module which is slower than event-based modules like mpm_event or mpm_worker. The other problem is, Apache’s PHP module is slower too!
The basic LAMP stack will start facing issues with a lower number of visitors too! Maybe that’s the same reason why you have decided to follow this guide. Let’s first understand the stack we are going to set up.
We will set up Apache with its FastCGI module and we will connect our Apache webserver with PHP-FPM. FPM stands for Fast Process Manager. With all these powerful tools and modules in place, our server will handle the traffic more efficiently and fast!
So, what you will need to set up the stack? A clean Ubuntu 16.04 server and root access to the server. We need root access because we are going to install and configure Apache with its modules, PHP and PHP-FPM. So, Let’s get started by installing Apache on the server.
Install Apache with FastCGI
It’s always very easy to install a web server on the machine. It just takes a few commands and we are good to go. However, In this case, We are also going to install the FastCGI module along with Apache. Execute the following command to install Apache with FastCGI module in your Ubuntu 16.04 machine.
$ sudo apt-get install apache2 libapache2-mod-fastcgi -y
flag in our command will suppress the confirmation message by automatically answering yes. It will take 20-30 seconds to install Apache with FastCGI module depending on the computing power available on your machine.
Once done, we have to disable mpm_prefork module and enable the mpm_event module. Installation of the FastCGI module will automatically perform this action for you. However, Just in case it’s not done alrready, execute the following commands in series.
$ sudo a2dismod mpm_prefork $ sudo a2enmod mpm_event
The reason why we have to execute the above given commands in series is, we cannot keep two MPMs running at the same time. It means that, to enable the new MPM, we have to first disable the MPM we are using right now. We also have to enable the actions
module of Apache with this configuration. Execute the following command to enable the actions module.
$ sudo a2enmod actions
Once done, restart the Apache server using the following command to apply the current configuration successfully.
$ sudo service apache2 restart
Currently, all our Apache and FastCGI configuration are done. Now, we have two steps left in the process. It’s time to install PHP on our server, Let’s do it.
Install PHP-FPM (Fast process manager)
It’s very easy to install PHP-FPM too! In this tutorial, I am going to install PHP-FPM with one or two extra PHP extensions. But you can install as many PHP extensions as you need, based on your requirements.
Execute the following command to install php-fpm on your Ubuntu 16.04 server.
$ sudo apt-get install php7.0-fpm php7.0-mysql php7.0-mbstring -y
It might take a full minute to install PHP-FPM and other extensions. Once done, verify your PHP installation by executing the following command.
$ sudo php -v
You should see PHP version information if PHP is correctly installed. Also, check if php7.0-fpm
service is running or not. Execute the following command to check the status of the service.
$ sudo service php7.0-fpm status
The output should show you active (running) as a status. If the output is the same as expected, Congratulations! We can now finally configure Apache with PHP-FPM using FastCGI.
Configure Apache with PHP-FPM
If you already have Apache with FastCGI and PHP with FPM installed, You should start with this step. If you started following this guide from this step, please make sure to disable mpm_prefork and enable mpm_event.
Once done, execute the following command to create a new configuration file in Apache. This configuration file will use fastCGI module to pass scripts to PHP-FPM for processing via socket.
$ sudo nano /etc/apache2/conf-available/php-fpm.conf
Now, Paste the following content in the file.
<IfModule mod_fastcgi.c> AddHandler php7-fcgi .php Action php7-fcgi /php7-fcgi Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization Require all granted </Directory> </IfModule>
Once done, press CTRL+X followed by Y followed by the Enter key to save the file. The file is saved and ready to use. But we still have to enable the configuration file in order to make apache include this configuration while execution.
Execute the following command to enable the configuration file we just created.
$ sudo a2enconf php-fpm
It will show you the message “Enabling conf php-fpm” as it will enable the configuration file. However, to apply the changes, we have to restart our apache server. It’s very easy to do, execute the following command to restart Apache webserver.
$ sudo service apache2 restart
If you didn’t face any error message while restarting the Apache server this time, Congratulations! We have finally configured Apache with FastCGI and PHP-FPM. You will surely see the improvement in the performance of your application.
To test if your stack is properly configured, you can create a test.php file in your DocumentRoot with content <?php phpinfo(); ?>
and access it via your web browser. You will be able to see FPM/FastCGI as Server API in PHP configuration, just like the following image.
Conclusion: It’s a bit hard to set up Apache with FastCGI and PHP-FPM but the performance it provides is not even comparable with the basic LAMP setup. It might take a few tries to set up this stack for the first time as you will stumble upon many unexpected server-level issues but once you understand how it actually works, It becomes very easy to set up high-performance stacks on your servers.
Let us know if you need some help setting up Apache with PHP-FPM in the comment section given below. We will respond with the solution or answer as soon as possible.