View Categories

Install Linux Apache MySQL PHP on Ubuntu 20.04 LTS (LAMP)

4 min read

The following is based on Ubuntu 20.04.02 LTS using Kernel 5.4.0-28-generic

Installing the LAMP stack #

LAMP is a common acronym used to describe the combined installation on (L)inux of (A)pache web server, (M)ySQL Database server and, (P)HP Scripting language support. LAMP.

Note: In this tutorial, we will use MariaDB, which has full compatibility with MySQL

The LAMP stack is the basis for many online platforms and is a baseline for getting your web applications and sites up and running.

To start with, let’s install Apache:

apt install -y apache2 apache2-utils

Start the apache2 webserver service:

systemctl start apache2

At the time of writing, the version is 2.4.41 built: 2021-06-17; you can find this out with:

apache2 -v

You should now be able to browse to your server’s IP address, e.g. https://1.2.3.4, and see the Apache2 Ubuntu default page, or if you have already pointed an A record to your server’s IP, you can use that address instead.

It is a good idea to open up the ports needed at this stage. Usually, 80 and 443m in iptables and or ufw, depending on your Operating system configuration. You can do this as follows:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT

ufw commands:

ufw allow http
ufw allow https

Next, we need to make sure that www-data, which is the apache username owns the web/document root as by default it is owned by the root user, which will cause all sorts of problems with permissions later:

chown www-data:www-data /var/www/html/ -R

Set the server name #

Next, let’s set the server name using the domain name you plan to point at the server:

echo "mydomainname.com" > /etc/apache2/conf-available/servername.conf

We now need to tell Apache to reach the servername.conf file and restart Apache for the changes to take effect:

a2enconf servername.conf
systemctl reload apache2

 

Install MariaDB #

Next, we need to install MariaDB

apt install mariadb-server mariadb-client

Start the service:

systemctl start mariadb

Make sure it starts when the server boots:

systemctl enable mariadb

 

Now we need to take initial security measures to remove the generic items which are inherently insecure on a default installation:

mysql_secure_installation

 

You will be asked to set a MariaDB root password then answer yes to the remaining questions you will be asked related to removing the anonymous user, the test database, disable remote root access, and then finally reload the privilege tables.

While you can now use the database as root from the command line without having to enter a password as Ubuntu uses the unix_socket to authenticate the user it is wise to check everything was done correctly so lets try to enter the database with the following:

mariadb -u root -p

The -p will force you to use the password you just set. Assuming all went to plan, you should now be logged into the MariaDB server as root. You can exit with the command ‘exit;’

When writing the MariaDB, Version is 10.3.29-MariaDB-0ubuntu0.20.04.1 you will see this when logging in.

 

Install PHP #

Next, we need to install PHP apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline

Now enable the php7.4 module in the Apache webserver:

a2enmod php7.4

Restart Apache, so the changes take effect:

systemctl restart apache2

Let’s check that PHP is running with Apache by generating a PHP info page:

echo "" > /var/www/html/info.php

In some instances, you might want to run PHP-FPM. The PHP module in Apache will handle things fine 9/10 however there is a performance case to be made for using PHP-FPM instead. The following steps are optional:

 

Disable the PHP #

Disable the PHP7.4 Module in Apache:

a2dismod php7.4

Install php7.4-fpm:

apt install php7.4-fpm

Enable the proxy fcgi and setenvif modules in Apache to allow FPM to run:

a2enmod proxy_fcgi setenvif

 

Enable the PHP #

Enable the php 7.4-fpm: configuration file (/etc/apache2/conf-available/php7.4-fpm.conf) which is created when php7.4-fpm is installed, this is also the file used to edit fpm parameters:

a2enconf php7.4-fpm

Finally, restart Apache:

systemctl restart apache2

If you browse the info.php page you created earlier, you should not see that the Server API has: FPM/FastCGI.

 

Congratulations, you now have a functional LAMP stack on your server!