This article explains how to run Apache with PHP-FPM on Ubuntu Linux.
PHP-FPM (FastCGI Process Manager) is an alternative to PHP FastCGI. It provides features like Adaptive process spawning, basic statistics, and advanced process management with graceful stop/start, which may be helpful in really busy websites.
Running Apache with PHP-FPM on Ubuntu Linux can provide additional benefits, such as adaptive process spawning, basic statistics, and advanced process management with graceful stop/start, which can be helpful for busy websites.
PHP-FPM can also improve website performance significantly, especially for big and busy websites built on PHP. Following the steps in the tutorial, you can enable PHP-FPM support for your Apache2 HTTP server and enjoy these benefits.
Install Apache2 HTTP Server
If you don’t already know, the Apache HTTP server is the most popular used today. However, with PHP-FPM support, you may be able to improve your website performance significantly, and it is especially useful for big and busy websites built on PHP.
To install Apache2 with PHP7.2 with FastCGI support, run the commands below.
sudo apt update sudo apt install apache2
After installing Apache2, the commands below can stop, start, and enable Apache2 always to start when the server boots.
sudo systemctl stop apache2.service sudo systemctl start apache2.service sudo systemctl enable apache2.service
To test the Apache2 setup, open your browser and browse to the server hostname or IP address. You should see the Apache2 default test page, as shown below. When you see that, then Apache2 is working as expected.
http://localhost
You may have to install the Apache2 Fast-CGI module manually. The commands below will cause an error that the package has no installation candidate.
sudo apt-get install libapache2-mod-fastcgi
You may get the error below after running the commands above.
Package libapache2-mod-fastcgi is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'libapache2-mod-fastcgi' has no installation candidate
To add that package, download it and install it manually.
cd /tmp && 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 apt install -f
Install PHP7.2 PHP-7.2-FPM
PHP 7.2 isn’t available on Ubuntu’s default repositories. To install it, you will have to get it from third-party repositories.
Run the commands below to add the below-party repository to upgrade to PHP 7.2
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php
Then update and upgrade to PHP 7.2
sudo apt update
Next, run the commands below to install PHP 7.2 and related modules.
sudo apt install php7.2 php7.2-fpm php7.2-common
After installing PHP 7.2, run the commands below to open the PHP default config file for Apache2.
sudo nano /etc/php/7.2/fpm/php.ini
Then, save the changes on the following lines below in the file. The value below is an ideal setting to apply in your environment.
file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 100M max_execution_time = 360 date.timezone = America/Chicago
After making the change above, please save the file and close it.
Configure Apache2 HTTP to use PHP7.2-FPM
Now that Apache2 and PHP7.2-FPM are installed run the commands below to enable the FastCGI module for Apache2.
sudo a2enmod actions fastcgi alias proxy_fcgi
Next, open the Apache2 default configuration file, add the highlighted lines, and save and close.
sudo nano /etc/apache2/sites-available/000-default.conf
Configure the file as shown below:
<VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/html ServerName example.com ServerAlias www.example.com <Directory /var/www/html/wordpress/> Options +FollowSymlinks AllowOverride All Require all granted </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Save and close.
Restart Apache2 and Test
After the configurations above, run the commands below to restart Apache2.
sudo systemctl restart apache2.service
To test PHP 7.2 settings with Apache2, create a phpinfo.php file in the Apache2 root directory by running the commands below
sudo nano /var/www/html/phpinfo.php
Then, type the content below and save the file.
<?php phpinfo( ); ?>
Save the file. Then browse to your server hostname followed by /phpinfo.php
http://example.com/phpinfo.php
You should see the PHP default test page…
That’s it! This is how you use Apache2 with PHP-FPM (FastCGI)
Enjoy!
Leave a Reply to bhavin Cancel reply