Open In App

How to Install Apache with PHP-FPM on Ubuntu?

Improve
Improve
Like Article
Like
Save
Share
Report

The Apache HTTP Server is a free, open-source, cross-platform web server software. It is developed and maintained by Apache Software Foundation. Apache is the most widely used web server around the world. The vast majority of Apache HTTP server instances run on Linux distribution, but current versions also run on Microsoft Windows, OpenVMS, and a variety of UNIX-like systems. PHP-FPM  is known as FastCGI Process Manager. It is an alternative PHP FastCGI implementation with extra features that are useful for heavy-loaded sites. It allows us to run multiple versions of PHP simultaneously. In this article, we will discuss how to install PHP-FPM with Apache on Ubuntu. Here we will use Ubuntu version 20.04.03 on a virtual box. 

Installing PHP-FPM with Apache on Ubuntu

To install PHP-FPM with Apache on Ubuntu follow the following steps:

Step 1: Run the following command to basic update in the base system for the latest available packages.

# apt update -y

Updating-base-system

Step 2: Install apache2 and PHP-fpm with the required installation package.

# apt install apache2 libapache2-mod-fcgid php php-fpm php-cli libapache2-mod-php

 Installing-apache2-and-PHP-fpm

The above command will install PHP 7.4 version, but if you want to install a manual specific version follow like apt install php7.3.

Step 3: After installation of php-fpm, you have to enable a few modules to configure multiple versions of php fastCGI with apache2

# a2enmod actions fcgid alias proxy_fcgi 

Enabling-few-modules-to-configure

Step 4: Configuration Apache2 with php-fpm. Now we need to configure the apache2 with php-fpm. So, you can create a new configuration file or you can make changes on default files. In this case, I create a new gfg.conf configuration file

# nano /etc/apache2/sites-available/gfg.conf

Add command line as follows

<VirtualHost *:80>
    ServerAdmin admin@geekforgeek.com
    ServerName geekforgeek.com
    ServerAlias www.geekforgeek.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
 
    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
        SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
    </FilesMatch>
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Configuration-Apache2-with-php-fpm

Save the files and restart the apache2 to reload the changes.

# systemctl restart apache2

Step 5: Now we test the PHP setup. To create php script with phpinfo(); function and save on web server document root.

# nano /var/www/html/info.php

<?php

    phpinfo();

?>

So open web browser and go to http://localhost/info.php or you can also use system IP instead of localhost.

php-info

So, successfully install php-fpm with apache2 in ubuntu 20.03.04. The main use of php-fpm is to use multiple versions of PHP at a time. So you can run another version of PHP at the same time.

Let’s install and configure another version of php-fpm with apache2

Step 1: You need to install some repository.

# add-apt-repository ppa:ondrej/php

Install-some-repository

Step 2: Next we install php-fpm using the following command.

# apt install php8.0 php8.0-fpm

 installing-php-fpm

As you can see in the above image, at the ending of the php8.0 and php8.0-fpm installation system automatically says run that command to enable php8.0 as we run on step 3 of php7.4 installation. They both commands are the same. And also, two different PHP version sockets are shown in the image.

Step 3: Create a new configuration file as we created above.

# nano /etc/apache2/sites-available/gfg1.conf

<VirtualHost *:8888>
    ServerAdmin admin@geekforgeek.com
    ServerName geekforgeek.com
    ServerAlias www.geekforgeek.com
    DocumentRoot /var/www/gfg

    <Directory /var/www/gfg>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
 
    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
        SetHandler "proxy:unix:/var/run/php/php8.0-fpm.sock|fcgi://localhost"
    </FilesMatch>
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Create-a-new-configuration-file

You have to do some changes like port number, document root, and main PHP sock version.

Step 4: Add a new port in port.conf file

# nano /etc/apache2/ports.conf

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 8888                

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

Add-a-new-port

Step 5: To enable this tell apache a new configuration file and reload apache2.

# a2ensite gfg1.conf 

# systemctl reload apache2

Reloading-apache2

Step 6: Finally test PHP set up. So, first, create a new directory “gfg” in /var/www path as we mention DocumentRoot in the configuration file. And create a PHP test file.

# mkdir /var/www/gfg

# echo “<?php phpinfo(); ?>” > /var/www/gfg/index.php 

So, open a browser and go to http://localhost:8888 

Testing-PHP-set-up


Last Updated : 11 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads