How to set up Apache with PHP-FPM on Ubuntu 18.04

Ubuntu 18.04 is the latest stable Ubuntu version that we can use on our server. Ubuntu is one of the most used operating systems on the servers that are based on Linux. The reason why it is so popular is, it is very easy to get started with. It is a well documented and well maintained Linux based operating system for servers. In this guide, I will show you how to set up Apache with PHP FPM on Ubuntu 18.04.

Apache FastCGI with PHP-FPM is one of the best stacks to host PHP applications. This stack is not as easy as setting up a basic LAMP stack, but it is far better than the basic LAMP stack when it comes to performance. The key ingredient in this stack is FPM which stands for Fast Process Manager. In this stack, Apache will send all the PHP files for processing to FPM. It will then return the rendered output to the browser.

So, This is how our stack will work. One tip, If you are going to host a WordPress website on your server after setting up this stack, use any one of the top WordPress caching plugins to supercharge your WordPress site!

Finally, we can start the actual step-by-step tutorial. However, there are some prerequisites to follow this guide. Make sure you have:

  1. A Clean Ubuntu VPS (No stacks pre-configured).
  2. Root access to the server OR if you do not have root access, make sure you have sudo privileges.

We need root access to the server because we have to install Apache, PHP-FPM, FastCGI, and MySQL on our server. We also have to create some configuration files to support our stack.

Once you are clear on prerequisites, continue with the guide!

Set up Apache with PHP-FPM on Ubuntu 18.04

I have divided this guide into multiple parts so that you can understand how exactly the stack works. The first thing we are going to install on our server is, obviously, Apache and Apache’s FastCGI module.

Install Apache with FastCGI on Ubuntu 18.04

Execute the following commands on your server to install Apache and FastCGI.

$ sudo apt-get update
$ sudo apt-get install apache2 -y
$ wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb
$ sudo dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb
$ sudo a2enmod actions
$ sudo service apache2 restart

It might take some time to install Apache and FastCGI on your server. Once the process is complete, verify the Apache installation by visiting your server’s public IP address on your browser.

If you see the default Apache page like the following image, you have successfully installed and configured Apache on your server.

Apache default page

Now, we can move on to install PHP on our server. Installing PHP is the easiest task from all the tasks we have to perform to set up this stack. So, Let’s do it.

Install PHP FPM on Ubuntu 18.04

In this guide or demonstration, we will install only a handful of PHP extensions along with PHP-FPM. But you can install as many extensions as you want as per your requirements. We will go with the PHP version 7.2 in this guide, again, you can install the PHP version that works best for you.

Execute the following commands to install PHP-FPM with other PHP extensions on your server.

$ sudo apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-curl php7.2-dom -y

This process will take approximately one minute to complete. Once the process is complete, PHP is ready to use on our server. If you want to install the different version of PHP, just replace 7.2 with 7.X . For example, replacing 7.2 with 7.1 will install PHP7.1 on your server.

Verify the installation by executing the command given below.

$ php -v

You should see the PHP version along with some other information regarding your PHP installation as an output of the command. Once the PHP is ready, we can now configure our Apache web server to pass PHP requests to FPM.

Configure Apache with PHP-FPM on Ubuntu 18.04

Right now, Our Apache web server and PHP-FPM are configured and can work individually. But we still have to tell our Apache web server to pass PHP requests to PHP fast process manager.

To enable this configuration globally, we just have to create a configuration file in our Apache installation. The configuration file will contain information about our PHP Fast Process Manager that Apache can use to pass requests.

Execute the following command to create an Apache configuration file.

$ sudo nano /etc/apache2/conf-available/php-fpm.conf

Now, paste the following content in the file while your nano editor is open in edit mode.

<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 /run/php/php7.2-fpm.sock -pass-header Authorization -idle-timeout 60
        <Directory /usr/lib/cgi-bin>
                Require all granted
        </Directory>
</IfModule>

Do not forget to replace the PHP version number with the version you have installed on your server. Once done, press CTRL+X followed by Y followed by the Enter key to save our configuration file.

Now, we also have to enable this configuration file so that Apache can pass PHP files for processing to Fast Process Manager. Execute the following command to enable the configuration file.

$ sudo a2enconf php-fpm
$ sudo service apache2 reload

If you do not see any issue while reloading the Apache configuration, Congratulations! The Apache installation can now work with Fast Process Manager to handle the PHP files.

Test the Configuration

Now, It’s time to test our configuration. We have to make sure that our PHP files are sent to FPM for interpretation. To test the configuration, we can create a test.php file in our default Apache DocumentRoot. Then, we can check if the PHP code is processed or not by accessing the file in the browser.

To create a test PHP file, execute the following command.

$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php

Now, access the following URL in your web browser. Do not forget to replace the SERVER_IP placeholder with the actual IP address of your server.

http://SERVER_IP/test.php

If you can see a page just like the following image, you have successfully configurated Apache with PHP-FPM on your Ubuntu 18.04 server.

Apache with PHP FPM and FastCGI

If you can see the page just like the above-given image, you can move on to the next and optional step in the process.

Install and configure MySQL on Ubuntu 18.04

This step is optional if you want to use some other database management system on your server. If you want to host a WordPress site, I recommend you to complete this task too!

To install MySQL on your server, execute the following command.

$ sudo apt-get install mysql-server mysql-client -y

Then, execute the following command to secure your MySQL server.

$ sudo mysql_secure_installation

It will ask you some questions. The first thing it will ask you is if you want to enable the Validate Password plugin. We recommend you to enable this plugin as it will set the standard for setting the strong password. Then, It will ask you to set the strong password for the root user.

After that, it will ask you four more questions. Answer them according to your requirements. If you do not know what to do, answer Y on all of them.

Once done, Our MySQL server is successfully installed and configured on our server. There is nothing left to do on our server. You can now install WordPress on your server and it should work smooth!

 

Conclusion: Apache with FastCGI works best with PHP-FPM. This combination is the best you can get if you want to host PHP applications on your server. As an extra note, we recommend you to secure your Ubuntu server with UFW firewall.

If you are using Ubuntu 16.04, you can follow our guide to set up Apache with PHP-FPM on Ubuntu 16.04 operating system.Let us know if you need help setting up this stack on your server. You can comment down your query in the comment section given below. Or, if you are an InterServer customer, you can also contact our support team for help!

Leave A Comment