How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04

Linux VPS

Introduction

A "LAMP" stack is a collection of open-source software that is often deployed together to allow a server to host dynamic websites and PHP-based web apps. This word is an abbreviation for the Linux operating system and the Apache web server. The site's data is kept in a MySQL database, while PHP handles dynamic content.

In this article, you will install a LAMP stack on an Ubuntu 20.04 server.

Prerequisites

You will need an Ubuntu 20.04 server with a non-root sudo-enabled user account and a basic firewall; this can be configured using our Initial server setup guide for the Ubuntu 20.04 tutorial.

Step 1 - Installing Apache and Updating the Firewall

The Apache web server is one of the most widely used web servers. It is highly documented, has an active user community, and has been widely used for much of the history of the web, making it an excellent choice for hosting a website.

Begin by refreshing the package manager cache. If this is your first time using sudo in this session, you must provide your user's password to confirm you have the necessary permissions to manage system packages using apt.

$ sudo apt update
Then, install Apache as follows:
$ sudo apt install apache2

You will also be asked to confirm Apache's installation by pressing Y and then ENTER.

After the installation is complete, you must configure your firewall to enable HTTP traffic. UFW includes a variety of application profiles that you can use to accomplish this. To see a list of all currently accessible UFW application profiles, type:

$ sudo ufw app list
Output:

Here are the definitions for each of these profiles:

  • Apache: This profile only allows access to port 80 (unencrypted web traffic).
  • Apache Full: This profile allows access to port 80 (standard, unencrypted web traffic) and port 443.
  • Apache Secure: This profile only allows access to port 443 (TLS/SSL encrypted traffic).

Because this is a new Apache installation and you don't yet have a TLS/SSL certificate installed to allow HTTPS traffic on your server, it's advisable to limit connections to port 80 for the time being.

Use the Apache profile to restrict traffic to port 80 only:

$ sudo ufw allow in "Apache"

Output:
Rule added
Rule added (v6)

You may confirm the modification by using -

$ sudo ufw status
Output:

Traffic on port 80 is now permitted via the firewall.

You may immediately verify that everything happened as expected by visiting your server's public IP address in your web browser.

http://your_server_ip

The default Ubuntu 20.04 Apache web page will be displayed for information and testing; this is what it should look like:

If you view this page, your web server has been correctly installed and is now available through your firewall.

Step 2 - Installing MySQL

Now that your web server is up and running, you must install the database system to store and manage data for your website. MySQL is a widely used database management system in PHP settings.

Once again, use apt to get and install this software:

$ sudo apt install mysql-server

When prompted, type Y and then ENTER to confirm the installation.

When the installation is complete, run the security script that comes with MySQL. This script will disable access to your database system and delete several insecure default settings. Begin the interactive script by typing:

$ sudo mysql_secure_installation

This prompts you to configure the VALIDATE PASSWORD PLUGIN.
Answer Y for yes or any other letter to proceed without enabling.

Output:

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
s and allows the users to set only those passwords that are
secure enough. Would you like to set a VALIDATE PASSWORD component?

Press Y for Yes, any other key for No: Y

If you answer "yes," you will be prompted to choose a degree of password validation. Remember that if you input 2 for the strongest level, you will receive errors when attempting to set any password that does not include digits, upper and lowercase letters, and special characters or is based on common dictionary phrases.

Regardless of whether you enabled the VALIDATE PASSWORD PLUGIN, your server will prompt you to enter and confirm a password for the MySQL root user. This is not the same as the system root. The database root user is an administrative user with complete control over the database system. Even though the default authentication mechanism for the MySQL root user does not need a password, if one is set, you should define a strong password here as an extra precaution.

If you enable password validation, your server will display the password strength for the root password you just supplied and ask if you wish to proceed.

For the remaining questions, press Y and then ENTER at each prompt; this will remove some anonymous users and the test database, deactivate remote root logins, and load these new rules so MySQL recognizes the changes.

When it is complete, see if you can access the MySQL console by typing:

$ sudo mysql

This command will connect to the MySQL server as the administrator database user root, as implied by using sudo; you should get something like this:

Output:

Enter the following command to exit the MySQL console:

$ mysql > exit

Your MySQL server is now up and running and secure. The final component of the LAMP stack will be installed next.

Step 3 - Installing PHP

You have Apache set up to serve content and MySQL set up to store and manage data. PHP is the component of our system that will process code to display dynamic material to the end user. You will also require php-mysql, a PHP module that allows PHP to interface with MySQL-based databases. To enable Apache to support PHP files, you'll also need libapache2-mod-php. As dependencies, core PHP packages will be installed automatically.

Run the following command to install these packages:

$ sudo apt install php libapache2-mod-php php-mysql
Once the installation is complete, execute the following command to verify your PHP version:
$ php -v

Output:

PHP 7.4.3-4ubuntu2.19 (cli) (built: Jun 27, 2023, 15:49:59) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3-4ubuntu2.19, Copyright (c), by Zend Technologies

Step 4 - Creating a Virtual Host for your Website

You can use the Apache web server to construct virtual hosts to encapsulate configuration data and host several domains from a single server. We'll set up a domain - your_domain, but you should change it with your domain name.

On Ubuntu 20.04, Apache is enabled by default and set to serve documents from the /var/www/html directory. While this is fine for a single site, it might become cumbersome if you host numerous sites. Rather than changing /var/www/html, we'll establish a directory structure within /var/www for the your_domain site, leaving /var/www/html as the default directory to be provided if a client request does not match any other sites.

Make the following directory for your_domain:

$ sudo mkdir /var/www/your_domain
 
Next, use the $USER environment variable to assign ownership of the directory to your current system user:
$ sudo chown -R $USER:$USER /var/www/your_domain
Then, using your preferred command-line editor, create a new configuration file in Apache's sites-available directory. We'll use nano in this case:
$ sudo nano /etc/apache2/sites-available/your_domain.conf
This will generate a new empty file. Copy and paste the following bare-bones configuration:
<VirtualHost *:80>
ServerName your_domain
ServerAlias www.your_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When finished, save and close the file. You can do this in nano by typing CTRL+X, Y, and ENTER.

With this VirtualHost setup, we tell Apache to serve your_domain from the web root directory /var/www/your_domain. If you want to test Apache without a domain name, remove or comment out the ServerName and ServerAlias options by adding a # character to the beginning of each line.

You can now enable the new virtual host using a2ensite:

$ sudo a2ensite your_domain

You might wish to turn off the default website that comes with Apache. If you don't use a custom domain name; this is needed because Apache's default configuration will replace your virtual host. Type to disable Apache's default website -

$ sudo a2dissite 000-default
 

To ensure that your configuration file is free of syntax problems, run:

$ sudo apache2ctl configtest
Output;
Syntax OK

Finally, reload Apache to make these modifications effective:

$ sudo systemctl reload apache2

Although your new website is operational, the web root /var/www/your_domain remains empty. Create an index.html file in that place so that we can verify that the virtual host is functioning -

$ nano /var/www/your_domain/index.html

Include the following information in this document:

<html>
<head>
<title>your_domain website</title>
</head>
<body>
<h1>Hello! Welcome to AccuWebHosting</h1>

<p>This is the landing page of <strong>your_domain</strong>.</p>
</body>
</html>

Now, open your browser and type in your server's domain name or IP address:

http://server_domain_or_IP

Output:

Hello, Welcome to HostBuddy.com Hosting.

If you view this page, it signifies your Apache virtual host is functioning normally.

This file can be left as a temporary landing page for your application until you create an index.php file to replace it. Remember to remove or rename the index.html file from your document root, as it will take precedence over an index.php file by default.

Step 5 - Testing PHP Processing on your Web Server

Now that you have a unique place to host your website's files and folders, we'll write a PHP test script to ensure that Apache can handle and execute PHP file requests.

Inside your custom web root folder, create a new file called info.php:

$ nano /var/www/your_domain/info.php

This will create a new blank file. Inside the file, insert the following valid PHP code:

<?php
phpinfo();
?>

Save and close the file when finished.

To test this script, open a web browser and type in your server's domain name or IP address, followed by the script name; in this example, it is info.php -

http://server_domain_or_IP/info.php

Output:

 
 

This page contains PHP-specific information about your server. It is helpful for troubleshooting and ensuring that your settings are correctly applied.

If you can view this page in your browser, your PHP installation is functioning normally.

Conclusion

In this article, we’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as a web server and MySQL as a database system.