## Tutorial ##

How To Install LAMP Stack on Ubuntu 22.04

Published on May 17 2023 · Updated on May 17 2023

In this article, we'll explain how to install LAMP Stack on Ubuntu 22.04. We will install Apache, MariaDB, and PHP and deploy a test website page also we will install SSL certificate using Certbot.

Apache, MariaDB, and PHP are composed of packages. It is known as LAMP and installs on the Linux system environment.

Prerequisite:

  • Ubuntu 22.04 dedicated server or KVM VPS.
  • A root user access or normal user with sudo privileges.

1. Keep the server up-to-date:

sudo apt update && sudo apt upgrade -y

2. Install Apache webserver

sudo apt install apache2 -y


In case, you enabled UFW firewall, configure the ports in the firewall.

sudo ufw allow 80/tcp

sudo ufw allow 443/tcp


Now, let’s verify the Apache installation. Open browser and test default page.

http://[SERVER IP]

3. Install MariaDB

sudo apt install mariadb-server -y


The default configuration of the MariaDB will not be secured. Let’s secured the installation using the following command:

sudo mysql_secure_installation


Once the script gets executed, it will ask multiple questions. You can enter "y" or "n" as per your requirements.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] y

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

4. Install PHP

Here we are installing the default PHP version 8.1. This package is a dependency package, which depends on latest stable. We will install some common packages for web deployments using the following command:

sudo apt install php php-common php-mysql php-gd php-cli -y


Once PHP installed, for testing purpose, create a simple info.php page using following command (Not recommended in production environment):

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


Now, let’s access it from the web browser:

http://SERVER_IP/info.php

5. Deploy website

We have installed all the necessary packages to deploy the website om the Apache. Now, let's deploy the website.

first create a website root directory.

sudo mkdir -p /var/www/website_name


Note:
Replace the website_name to you website name.

Now, change the working directory to newly created website directory.

cd /var/www/website_name


Create a index.html file using following command:

sudo nano index.html


Copy and paste following conents:

<!DOCTYPE html>
<html>
   <head>
   </head>
   <body>
    <h1>It Worked!</h1>
 </body>
</html>


It's a basic HTML code, you can replace it what ever you want.

Save and close

Change the ownership of the directory and files.

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


Now, let's create a virtual host for our website. Create a file in /etc/apache2/sites-available/ using following command:

sudo nano /etc/apache2/sites-available/website_name.com.conf


Note: replace website_name.com to your website name.

Add following lines:

<VirtualHost *:80>
    ServerName <domain name or server IP>
    DocumentRoot /var/www/website_name
   <Directory /var/www/website_name>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
   </Directory>
</VirtualHost>


Note: 

  • Replace <domain name or server IP> with you domain name or server IP.
  • Replace website_name with your domain name or directory name.

Save and exit

Create a symbolic link of newly created conf file.

sudo ln -s /etc/apache2/sites-available/website_name.com.conf /etc/apache2/sites-enabled/website_name.com.conf


Restart the Apache web server to load the new virtual host file.

sudo systemctl restart apache2

6. Install Certbot

Finally, let's install SSL certificate using Certbot.

Add the Certbot repository:

sudo add-apt-repository ppa:certbot/certbot


You’ll need to press ENTER to accept.

Install Certbot’s Apache package with apt:

sudo apt install python3-certbot-apache -y


Obtaining an SSL Certificate

Run this command to get a certificate. Certbot provides a variety of ways to obtain SSL certificates through plugins. The Apache plugin will take care of reconfiguring Apache and reloading the config whenever necessary. To use this plugin, type the following:

sudo certbot --apache -d <your_domain>


Check automatic renewal

Once the certificate installed, test the renewal process, you can do a dry run with Certbot:

The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire.

sudo certbot renew --dry-run


That's it, navigate to your browser and enter your domain name or server IP. 

We've have seen how to install LAMP stack on Ubuntu 22.04.

Related Tutorials

How To Install Maven on Ubuntu 22.04 - HostnExtra

We'll explain how to install Maven on Ubuntu 22.04. For this demonstration purpose, we're installing current LTS version JDK 17.

How To Install Elasticsearch on Rocky Linux 9 - HostnExtra

We'll explain you how to install Elasticsearch on Rocky Linux 9. We'll perform CRUD operation using RESTful API. It's a distributed, RESTful and analytics.

How To Install Elasticsearch on AlmaLinux 9 - HostnExtra

We'll explain you how to install Elasticsearch on AlmaLinux 9. We'll perform CRUD operation using RESTful API. It's a distributed, RESTful search and analytics.