How to install LAMP stack on Ubuntu 22.04

Posted 
Comments 0

Today I’ll show you how to install a “LAMP” stack on Ubuntu 22.04 server. LAMP is an acronym for Linux, Apache, MySQL, and PHP, which are a set of open-source software for hosting websites written in HTML, SHTML and PHP. LAMP stacks are the most common software combination for hosting websites. There are alternatives like LEMP, which replaces Apache with Ngnix web server.

Some more information about each package:

This article is part of my series The Ultimate Web Server.

Contents

Prerequisites

You will need a cloud server (DigitalOcean, Vultr or DreamHost) with a non-root user and firewall configured. See my guide on Initial server set up.

Installing Apache and Configuring the Firewall

Before we install Apache web server, we need to make sure Ubuntu is up-to-date. Lets run apt to update the cache and upgrade any packages:

sudo apt update && sudo apt upgrade -y

Then reboot:

sudo reboot

Once your server reboots, login as your non-root user and install the Apache web server software using the apt package manager:

sudo apt install apache2 -y

Once Apache is installed, we need to allow it through the UFW firewall (Ports 80 and 443). Let’s see what options we have:

sudo ufw app list
Allow Apache through the UFW firewall

Each profile/option has a different meaning:

sudo ufw allow in "Apache Full"

We also need to enable OpenSSH so you can login after we enable the firewall:

sudo ufw allow OpenSSH

Now let’s enable the firewall:

sudo ufw enable

We then verify the changes:

sudo ufw status
Confirm the status of UWF firewall

We need to find your servers IP address (if you already know it, skip this section). To find your servers IP we use the ip command:

ip address
Find your servers IP address

Usually your IP address will be under the eth0 interface as highlighted in red. In my case it’s 68.183.229.66.

To check everything went well, enter the IP address of your server into your web browser address bar. You should see the default Apache “It Works!” page:

Enter your servers IP address. You should see the default It Works Ubuntu page.

Install MySQL

Now let’s install MySQL database software. We use the apt package manager to do this:

sudo apt install mysql-server -y

After MySQL has finished installing, we need to set the root password to avoid the following error during the mysql_secure_installation script:

Failed! Error: SET PASSWORD has no significance for user ‘root’@‘localhost’ as the authentication 
method used doesn’t store authentication data in the MySQL server. Please consider using 
ALTER USER instead if you want to change authentication parameters.

login to MySQL and use your Ubuntu sudo root password:

sudo mysql -uroot -p

Then we set the MySQL root password (replace mynewpassword with your chosen password):

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';

Then exit MySQL:

exit;

Now we can run the installation security script to secure MySQL with this command:

sudo mysql_secure_installation

You will presented with the Validate Password Component first. This component can enforce strong passwords for all MySQL users. I usually choose to disable this in testing mode, but if you’re planning on using your server for a production environment, definitely enable it. If you do enable it, you’ll need to choose from three different levels of password complexity: low, medium and strong.

Validate Password Component

I suggest answering the questions as follows:

Install PHP

Now let’s install the PHP package with only the required modules (I will give you some options for common packages further below). We install PHP using the apt package manager again:

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

Let’s make sure PHP was installed:

php -v

You should see something similar to this:

Check PHP version

Optional PHP Packages

There a hundreds of PHP packages available. These are packages I recommend you install:

sudo apt install -y php-{common,mail,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}

Check PHP Modules (Optional)

If you want to see what PHP modules and extensions are installed or enabled on your server, we use the phpinfo(); code inside a regular PHP page.

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

Then copy and paste this code inside the new PHP page:

<?php

phpinfo();

?>

	

Then save ctrl + o, ENTER and exit nano ctrl + x.

Now we need to change ownership of the new file:

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

Enter the IP address of your server in your web browser address bar with the new page (info.php) appended to it (in my case it’s: 68.183.229.66/info.php) and you should see the automatically generated PHP information page:

Create a phpinfo page to display modules and extensions

Make sure you delete the info.php file so nobody can see your servers sensitive information:

sudo rm -rf /var/www/html/info.php