Atlantic.Net Blog

How to Configure and Install LAMP Stack on Ubuntu 22.04

In this guide, we’re going to look at how to install and configure a LAMP Stack on Ubuntu 22.04.

Once you’re finished, you’ll be able to host dynamic websites and web apps written in PHP from your computer, or even remotely if you’re using VNC for Linux.

What Is a LAMP Stack?

A LAMP Stack includes an operating system, a web server, a database, and a programming language. The various components give the LAMP Stack its name; Linux is the operating system, Apache is the web server, MySQL database, and PHP is the programming language.

Together, these components can implement web applications and provide a modern computing environment. Its simplicity and ease of use have made it one of the top technologies when it comes to web development.

Each component has a specific role to play within the software stack:

  • Linux is a free, open-source operating system. There are many distinct distributions of Linux available, of which Ubuntu is far and away the most popular.

Image sourced from ubuntu.com

  • Apache is a common web server, developed and released as free, open-source software by The Apache Software Foundation. It’s well-documented and has an active user community.

All the components required to host a website are included in the standard version of Apache, but other modules can be added from the Apache Hadoop ecosystem to add features and functions such as programming language APIs and authentication.

  • MySQL is a free relational database management system (RDBMS) with a client-server model. It’s used to create and manage databases based on a relational model.
  • PHP is the server-side programming language used in the LAMP Stack. It is well integrated with the rest of the stack, as it’s a very commonly used language in web development. In fact, over 75% of websites using a server-side programming language use PHP.

Image sourced from researchgate.net

PHP commands can be implemented efficiently within an HTML page, from which the Ubuntu system interprets the code using a PHP processor. PHP software is also available at no cost.

Requirements

Before we begin, in order to implement this step-by-step guide, you’ll need an Ubuntu 22.04 OS running on your computer, or a remote server. Ideally, you should have a non-root, sudo-enabled user account, and a basic firewall.

Sudo enables users to access administrator privileges without logging in directly as root, giving them access to commands they would not normally be able to use.

Any commands that require elevated privileges will be prefixed with ‘sudo’ throughout this guide.

Installing and Configuring a LAMP Stack

First we’re going to take a look at how to install the LAMP Stack on Ubuntu 22.04 LTS.

1. Update

The first step is to update the repository and package manager cache using apt:

sudo apt update 
sudo apt upgrade

If it’s the first time you’ve used sudo in a session, you’ll be asked to provide a user password. This is to confirm that you’ve got the right privileges to manage system packages.

2. Install Apache

The next step is to install the Apache web server:

sudo apt install apache2

At this point, you’ll be prompted to confirm Apache’s installation, so press ‘Y’, then ‘Enter’.

Once it’s installed, Apache should start automatically. You can check its status using:

systemct1 status apache2

Next, you’ll need to adjust your firewall settings to ensure that HTTP traffic is allowed. Uncomplicated Firewall (UFW) is the default firewall configuration tool for Ubuntu, which contains various application profiles for you to use. To see a list of the currently available profiles, execute:

sudo ufw app list

This should return a list of applications that looks like this:

Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

At this point in the installation, it’s recommended you only open port 80, as you won’t yet have a TLS/SSL certificate configured on your server.

Use the ‘Apache’ profile to allow traffic only on port 80. You can do this by executing the command:

sudo ufw allow in ‘Apache’

Then verify that your changes have been configured correctly using:

sudo ufw status

You can check right away whether the installation has been successful by visiting your server’s public router IP address in a web browser. If it’s worked, you should see the default Ubuntu 22.04 Apache web page, with a message saying ‘It works!’ near the top.

Image sourced from linux.com

Viewing this page means that the web server is correctly installed and can be accessed through your firewall.

3. Install MySQL

The next step is to install your database system, which will allow you to store and manage data for your website.

(While we’re talking about data, if you’re worried about data loss, why not investigate the benefits of cloud backup)?

The database management system we’re going to use is MySQL.

To install MySQL, execute:

sudo apt install mysql-server

Again, confirm the installation with ‘Y’ then ‘Enter’.

MySQL includes a pre-installed security script that we recommend you run once installation has completed. This will lock down access to your database system, and remove some of the more insecure default settings.

To do this, first you’ll need to adjust how your root MySQL user authenticates. To do this, open the MySQL prompt:

sudo mysql

Then change the root user’s authentication method to one that requires a password using the following ‘alter user’ command:

mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’

Exit the MySQL prompt:

mysql> exit

Then start the interactive script by executing the command:

sudo mysql_secure_installation

You’ll be presented with a message asking if you wish to configure the validate password plugin. This is a plugin used to test passwords, improving your security by only allowing you to use passwords that are satisfactorily secure.

You can press ‘Y’ to say yes and use the validate password plugin or any other key to decline.

If you select ‘yes’ you’ll be asked to choose the level of password validation you wish to use:

You can select the level of password validation using numerical keys, 0 for ‘Low’, 1 for ‘Medium’, and 2 for ‘Strong’.

Next, you’ll be asked to select and confirm a password for the MySQL root user. This is the database root user that will have full privileges over the database system. Defining a strong password here is essential for system security.

If you’ve enabled password validation, you’ll receive a prompt telling you how secure your password is. If you’re satisfied, press ‘Y’ to continue.

You can move through the rest of the prompts by pressing ‘Y’ and then ‘Enter’. This will disable remote root logins, remove the test database, and remove some anonymous users.

4. Install PHP

Now it’s time to install PHP. This will ensure that the code you enter into your setup will be displayed as dynamic content to the end user.

You’ll need the ‘php’ package, and ‘php-mysql’, a PHP module that will allow PHP to communicate with MySQL databases.

You’ll also need ‘libapache2-mod-php’, which will enable Apache to handle PHP files.

Install these packages by running this command:

sudo apt install php libapache2-mod-php php-mysql

Once installation is complete, you can check the version of PHP you’re running with this command:

php -v

5. Create a Virtual Host for Your Website

The Apache web server enables you to create virtual hosts, which allows you to host more than one domain on a single server.

For our example, we’ll use a domain titled ‘example_domain’. This should be replaced with your domain name when creating your virtual host.

If you’re struggling to decide on a name for your domain, try running a domain name check to see what’s available and generate some ideas.

In Apache for Ubuntu 22.04, there is one virtual host enabled by default. This virtual host is configured to serve documents from ‘/var/www/html’, which can become complicated if you’re hosting more than one site.

We will leave this directory intact and create a directory structure within it for example_domain.

To do this, start by executing this command:

sudo mkdir  /var/www/example_domain

Then you can assign ownership of the directory to the current system user by executing:

Sudo chown -R $USER:$USER /var/www/example_domain

Next, open a new configuration file in Apache’s directory using a command-line editor such as nano:

sudo nano /etc/apache2/sites-available/example_domain.conf

You can then configure the blank file that’s created with your domain name, save and close the file.

6. Testing the LAMP Stack Installation

Finally, you’ll need to test whether Apache can effectively process requests for PHP files using a PHP test script.

Start by creating a file named ‘info.php’ inside your web root folder:

nano /var/www/example_domain/info.php

Add some valid PHP text to the blank file that opens:

<?php

phpinfo();

Then save and close the file. You can test the script by navigating to your server’s domain name, followed by the script name ‘info.php’.

You can use this page to check that your settings have been applied correctly, and for debugging purposes.

We Hope You’ve Found This LAMP Stack Tutorial Illuminating

If you’ve followed these steps correctly, you should now have a fully operational LAMP Stack on Ubuntu 22.04. Using the combination of Linux, Apache, MySQL, and PHP, you’ll be able to build a website or web app to serve whatever purpose you may require.

See our other guides for further information about the LAMP Stack, such as how to ensure it’s HIPAA compliant.

If you wish to use the LAMP Stack remotely, investigate how you can go about setting up VNC for true computing freedom.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year