Atlantic.Net Blog

How to Add PHP-FPM Support on Apache and Nginx Web Server on Ubuntu 18.04

Hitesh Jethva
by Atlantic.Net (76posts) under VPS Hosting
0 Comments

Apache and Nginx are free, open-source and popular – the most widely used web servers around the world. Apache and Nginx both run on all Unix-based operating systems. Apache is known for its power, while Nginx is known for its speed. Nginx also used as a reverse proxy for the HTTP, HTTPS, IMAP, SMTP, POP3 and as a load balancer.

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation specially designed for high-loaded websites. PHP-FPM allows you to run multiple versions of PHP at a time. PHP-FPM can be run in a different way than mod_PHP on a webserver. If you are looking to host your web application with optimal performance, then PHP-FPM is the best choice for you.

In this tutorial, we will explain how to enable PHP-FPM support on Apache and Nginx webserver on an Ubuntu 18.04 server.

Prerequisites

  • A fresh Ubuntu 18.04 VPS on the Atlantic.Net Cloud Platform.
  • A valid domain name pointed at your VPS IP address.

Step 1- Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 18.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged into your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Enable PHP-FPM Support on Apache Web Server

In this section, we will learn how to install and enable PHP-FPM support on the Apache webserver.

Install Apache and PHP-FPM

First, install the Apache and PHP-FPM by running the following command:

apt-get install apache2 libapache2-mod-php libapache2-mod-fcgid php php-fpm php-cli -y

Once all the packages are installed, start Apache and PHP-FPM service with the following command:

systemctl start apache2
systemctl start php7.2-fpm

Configure Apache with PHP-FPM Support

Next, you will need to configure Apache webserver with PHP-FPM support. To do so, create a new Apache virtual host configuration file:

nano /etc/apache2/sites-available/example.com.conf

Add the following lines:

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/
     DirectoryIndex info.php
     ServerName example.com

     <Directory /var/www/html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
         SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
     CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Save and close the file. Then, enable the virtual host configuration file with the following command:

a2ensite example.com

Next, you will need to enable a few modules for apache2 to work with PHP-FPM:

a2enmod actions fcgid alias proxy_fcgi

Next, restart the Apache service using the following command:

systemctl restart apache2

Step 3 – Test Apache Web Server

Apache webserver is now configured with PHP-FPM support. It’s time to test whether PHP-FPM is loaded with Apache webserver or not.

To test it, create a sample info.php file inside Apache document root directory:

nano /var/www/html/info.php

Add the following lines:

<?php
    phpinfo();
?>

Save and close the file then change the ownership of info.php file to www-data:

chown www-data:www-data /var/www/html/info.php

Next, open your web browser and type the URL http://example.com. You should see the following page:

The above page indicates that PHP-FPM is loaded with the Apache webserver.

Note: Don’t forget to remove info.php file after testing.

Step 4 – Enable PHP-FPM Support on Nginx Web Server

In this section, we will learn how to install and enable PHP-FPM support on the Nginx webserver.

Step 5 – Install Nginx and PHP-FPM

First, install Nginx and PHP-FPM by running the following command:

apt-get install nginx php php-fpm php-cli -y

Once all the packages are installed, start Nginx and PHP-FPM service with the following command:

systemctl start nginx
systemctl start php7.2-fpm

Step 6 – Configure Nginx with PHP-FPM Support

Next, you will need to configure Nginx webserver with PHP-FPM support. To do so, create a new Nginx virtual host configuration file:

nano /etc/nginx/sites-available/example.com.conf

Add the following lines:

server {
        listen 80;
        root /var/www/html/;
        index info.php;
        server_name example.com;

        location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_intercept_errors off;
          fastcgi_buffer_size 16k;
          fastcgi_buffers 4 16k;
          fastcgi_connect_timeout 600;
          fastcgi_send_timeout 600;
          fastcgi_read_timeout 600;
        }

   location / {
       try_files $uri $uri/ =404;
   }

}

Save and close the file. Then, enable the Nginx virtual host with the following command:
ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

Next, restart Nginx and PHP-FPM service to apply the configuration changes:

systemctl restart nginx
systemctl restart php7.2-fpm

Step 7 – Test Nginx Web Server

Nginx webserver is now configured with PHP-FPM support. It’s time to test whether PHP-FPM is loaded with Nginx webserver or not.

To test it, create a sample info.php file in Nginx document root directory:

nano /var/www/html/info.php

Add the following lines:

<?php
    phpinfo();
?>

Save and close the file then change the ownership of info.php file to www-data:

chown www-data:www-data /var/www/html/info.php

Next, open your web browser and type the URL http://example.com. You should see the following page:

The above page indicates that PHP-FPM is loaded with the Nginx webserver.

Note: Don’t forget to remove info.php file after testing.

Conclusion

Congratulations! You have successfully configured Nginx and Apache web server with PHP-FPM support. I hope you have now enough knowledge to use PHP-FPM to run multiple versions of PHP at a time. To get started with PHP-FPM on Apache and Nginx, sign up for a VPS Hosting plan with Atlantic.Net today.

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources