Install LAMP with PHP-FPM on Ubuntu 20

Step 1 – Installing Apache

Apache web server debian packages are available under the default repositories. Login to your Ubuntu system with sudo privileges account. Open a terminal and execute the following commands:

sudo apt update 
sudo apt install apache2 libapache2-mod-fcgid

The above commands will install Apache and FastCGI module to your server.

Step 2 – Install PHP with FPM

Next, install PHP and PHP-FPM on your Ubuntu system. For this tutorial, we choose PHP 7.4 to install using

For the PHP installation we recommend to use ppa:ondrej/php PPA. Execute below couple of commands to add the PPA to your system.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then install PHP 7.4 (or required version) the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP and PHP-FPM packages.

sudo apt update
sudo apt install php7.4 php7.4-fpm

For additional PHP funcitons to be installed you may continue as below-

sudo apt install php7.4-common php7.4-intl php7.4-xsl php7.4-gd php7.4-mysql php7.4-pdo php7.4-curl php7.4-redis php7.4-zip php7.4-mbstring

Note:- When you are using PHP-FPM. All the PHP modules configurations are residing under /etc/php/7.4/fpm directory. You can read more about enable/disable PHP modules.
After installing the packages php7.4-fpm service will automatically be started. To make sure, type:

sudo systemctl status php7.4-fpm
php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-05-09 04:41:44 UTC; 19s ago
Docs: man:php-fpm7.4(8)
Process: 375077 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited>
Main PID: 375073 (php-fpm7.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 2283)
Memory: 9.3M
CGroup: /system.slice/php7.4-fpm.service
├─375073 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
├─375075 php-fpm: pool www
└─375076 php-fpm: pool www

May 09 04:41:43 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
May 09 04:41:44 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Step 3 – Apache Configuration

Now, You need to enable some of the Apache modules required for the FastCGI confiugration. You can enable the requird module by running command:

sudo a2enmod actions fcgid alias proxy_fcgi

Then configure Apache Virtual Host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in a text editor. You can also create a new configuration as per your choice.

sudo vim /etc/apache2/sites-available/000-default.conf

Update the configuration as followings.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.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>

Save your changes to the configuration file and restart Apache to reload the changes.

sudo systemctl restart apache2

Step 4 – Verify Setup

Apache with PHP-FPM configuration has been done. Now all the PHP files will be run with the FPM.

To verify these settigns, create a PHP script with phpinfo() function and place it to your server document root. Use below command to create file:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Then access the info.php using server IP address (for default VirtualHost) or your configured domain in Apache VirtualHost.

Step5- Installing phpmyadmin

To install PhpMyAdmin, run the following command.

sudo apt install phpmyadmin

During the package installation, you will be prompted to choose the web server that should be automatically configured to run PhpMyAdmin. Click enter to use Apache, the default option.

Also, PhpMyAdmin must have a database installed and configured before you can start using it. To configure a database for PhpMyAdmin with the dbconfig-common package, select yes in the next prompt.

Next, create a password for PhpMyAdmin to register with the MariaDB database server.

Create a Password for PhpMyAdmin

Once the installation process is complete, the configuration files for phpMyAdmin are located in /etc/phpmyadmin and its main configuration file is /etc/phpmyadmin/config.inc.php. Another important configuration file is /etc/phpmyadmin/apache.conf, used to configure Apache2 to work with PhpMyAdmin.

(Optional Step) you need to configure Apache2 to serve the phpMyAdmin site. Run the following command to symlink the file /etc/phpmyadmin/apache.conf to /etc/apache2/conf-available/phpmyadmin.conf. Then enable the phpmyadmin.conf configuration files for Apache2 and restart the Apache2 service to apply the recent changes.

$ sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
$ sudo a2enconf phpmyadmin.conf
$ sudo systemctl reload apache2.service

Enable PhpMyAdmin for Apache2

In a browser go to http://SERVER_IP/phpmyadmin, replacing SERVER_IP with the server’s actual IP address.

http://SERVER_IP/phpmyadmin

Once the PhpMyAdmin login page loads, enter root for the username and its password, or another MariaDB user, if you have any setup, and enter the user’s password. If you disabled remote root user login, you can use the phpmyadmin user and password to log in.

Src: 

How to Install Apache with PHP-FPM on Ubuntu 20.04

How to Install LAMP Stack with PhpMyAdmin in Ubuntu 20.04

Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.