How to install LAMP stack in Ubuntu?

Complete guide on how to install Apache, Mysql, and PHP (LAMP Stack) in Ubuntu.

What is LAMP Stack?

LAMP Stack is a set of technologies used to serve a web application. It uses tech stacks like Linux, Apache, MySql, and PHP.

  • Linux:  an open-source Operating System Kernel that manages different hardware resources. So it becomes the base for many operating systems and server os. Here we will use Ubuntu, an OS based on Linux Kernel as a host machine. On the top, we will  install Apache2, MySql, and PHP
  • Apache: It is a powerful and efficient web server that can manage huge traffic for our web app
  • MySql: MySql is a popular and stable relational database. 
  • PHP: It's a programming language that was primarily designed to make dynamic websites. 

We can use this tech stack to host PHP web apps on the cloud. 

Installing Linux (Ubuntu Server 22.04/ 24.04 Prefer the LTS Version)

We don't need to install Linux/Ubuntu by ourselves, we can get this with a Cloud/VPS server.

* You can check cloud services like AWS, GCP Cloud, Digital Ocean, or Hostinger and choose the best cloud server your needs. While choosing the OS, go for Ubuntu 22.04 or Ubuntu 24.04 LTS version. 

It will take some time and give you a public IPv4 address with the Linux os installed on your server.

Login to the Linux VPS using SSH

Now you can use the terminal in Linux and Mac or Putty in Windows to access your cloud server from your local PC.

ssh username@server_ip
Replace the username and server_ip with your server's username and IP address then connect to the VPS. Then follow the next steps for installing Apache, MySql, and PHP in the Linux VPS.

Install Apache2 in Ubuntu VPS

Use these commands to update and upgrade the server before installing the Apache web server

Update the system 

sudo apt update

Upgrade the system

sudo apt upgrade

Install Apache2

sudo apt install apache2

This will install apache2 in the Ubuntu VPS. Now try to open the URL https://<your_server_ip> it will show the Apache2 default page, like this


Configure ufw firewall to allow Apache Web Server

Here we need to configure the ufw firewall, it works as a security layer for incoming and outgoing connections. Apache servers at port 80 for http and port 443 for https. 

Check ufw status

sudo ufw status

Allow OpenSSH and Apache Full

sudo ufw allow OpenSSH
sudo ufw allow "Apache Full"

We need to connect to the VPS using port 22, and Apache Full allows ports 80 and 443. Once we enable the ufw then we can only connect to the allowed port.

Enable ufw Firewall

sudo ufw enable
sudo ufw app list

Apache2 commands

These are a few apache2 commands 

sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo systemctl status apache2
sudo systemctl enable apache2
sudo systemctl disable apache2 
sudo apache2ctl configtest # everything is ok 
sudo tail -f /var/log/apache2/error.log #logs apache errors 

Install MySQL

MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) for accessing and managing databases. Follow these commands to install MySQL on the Ubuntu server.

Update the System

sudo apt update

Install MySQL

sudo apt install mysql-server

Start MySQL Server

 sudo systemctl start mysql.service

Access MySQL Console

sudo mysql

Exit from the MySQL Console

exit

Secure the MySQL Database

We have just installed the MySQL database, but need to secure the database root user with a password. 

sudo mysql

Set the root user password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; 

* replace password with a strong password and then run

Access MySQL database with root user and password

Now if you try to access MySQL with the sudo mysql command it will give you an error, so use this command instead.

sudo mysql -u root -p

Then enter the password and continue

Create a MySQL user and give access to a specific database

Create a MySQL Database

CREATE DATABASE database_name;

List  all databases

SHOW DATABASES;

Now if you want to create a new user for MySQL use this command.

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Authorize the user to access the DB
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

Install PHP

PHP is a server-side scripting language designed primarily for web development. It stands for "Hypertext Preprocessor,". PHP can be embedded with HTML to serve dynamic webpages, it connects seamlessly with MySQL to make highly scalable web apps. Install PHP by the following commands

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

Configure Apache to server PHP files with high-priority

Once you have installed the PHP, configure the Apache server to serve PHP files with higher priority than HTML 

sudo nano /etc/apache2/mods-enabled/dir.conf

It will open the nano editor, and the code will be like this

<IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Here add PHP, first to the list like this
<IfModule mod_dir.c>
        DirectoryIndex index.php index.cgi index.pl index.html index.xhtml index.htm
</IfModule>
Now press ctrl + x and then y + enter to exit the nano editor. The LAMP stack is installed successfully in the Ubuntu VPS.

Test PHP on a Web Server

To test the LAMP stack installation, create an index.php page inside the /var/www/html (default public folder for apache2)

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

It will open the nano editor, then add these lines

<?php
phpinfo ();
?>

Now press ctrl + x and then y + enter to exit the nano editor and try to open the URL https://<your_server_ip> it will show the PHP info page like this

That's it LAMP stack is installed ;)