? Article 5: How to Install LAMP Stack on Ubuntu 22.04 LTS

LAMP = Linux + Apache + MySQL + PHP
Used for hosting websites, applications, and testing environments.

 


 

???? Part A: Preparation

  1. Laptop/Desktop with Ubuntu 22.04 LTS installed.

  2. Open a Terminal (shortcut: Ctrl + Alt + T).

  3. Run all commands with sudo (superuser).

???? Hint: If system asks for password → type your login password → press Enter.

 


 

???? Part B: Update System

Always update first:

sudo apt update && sudo apt upgrade -y

 

 


 

???? Part C: Install Apache (Web Server)

  1. Install Apache:

sudo apt install apache2 -y

 

  1. Check service is running:

sudo systemctl status apache2

 

(Press q to exit status view.)

  1. Test in browser → open:

http://localhost

 

✅ You should see “Apache2 Ubuntu Default Page”.

???? Hint: If page not loading → check firewall:

sudo ufw allow in "Apache Full"

 

 


 

???? Part D: Install MySQL (Database)

  1. Install MySQL:

sudo apt install mysql-server -y

 

  1. Secure MySQL:

sudo mysql_secure_installation

 

Follow prompts:

  • Validate password plugin? → N (optional, can say Y if you want strong policy)

  • Set root password? → Y → enter password

  • Remove anonymous users? → Y

  • Disallow root remote login? → Y

  • Remove test DB? → Y

  • Reload privileges? → Y

  1. Test MySQL login:

sudo mysql -u root -p

 

(Enter password you just set → should see mysql> prompt.)
Type exit; to leave.

 


 

???? Part E: Install PHP

  1. Install PHP + common modules:

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

 

  1. Verify PHP version:

php -v

 

✅ Should show PHP 8.1 or later (default in Ubuntu 22.04).

 


 

???? Part F: Test PHP with Apache

  1. Create a test PHP file:

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

 

  1. Add:

<?php

phpinfo();

?>

 

  1. Save (Ctrl+O → Enter → Ctrl+X).

  2. Open in browser:

http://localhost/info.php

 

✅ Should show a purple PHP information page.

???? Remove this file later for security:

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

 

 


 

???? Part G: Manage Services

Check status / start / stop:

sudo systemctl status apache2

sudo systemctl status mysql

 

Restart if needed:

sudo systemctl restart apache2

sudo systemctl restart mysql

 

 


 

???? Part H: Permissions (For Web Development)

  1. Default website folder: /var/www/html

  2. Give ownership to your user (replace username):

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

 

  1. Now you can copy/paste website files directly.

 


 

???? Part I: Optional Add-Ons

  • phpMyAdmin (GUI for MySQL):

sudo apt install phpmyadmin -y

 

When asked:

  • Select apache2

  • Configure db for phpMyAdmin → Y

  • Set phpMyAdmin password

Access at:

http://localhost/phpmyadmin

 

  • Additional PHP modules (if apps need them):

sudo apt install php-cli php-curl php-xml php-gd php-mbstring unzip -y

 

 


 

General Hints ????

  • Use systemctl to manage services (Apache/MySQL).

  • If PHP page downloads instead of running → check Apache has php module enabled:

sudo a2enmod php8.1

sudo systemctl restart apache2

 

  • For remote servers → open firewall:

sudo ufw allow 'Apache Full'

 

  • To change MySQL root password later:

sudo mysql

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPass123!';

FLUSH PRIVILEGES;

EXIT;

 

 


 

✅ LAMP stack is now ready!
You can deploy websites in /var/www/html/ or configure virtual hosts for multiple sites.