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.