? 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
-
Laptop/Desktop with Ubuntu 22.04 LTS installed.
-
Open a Terminal (shortcut: Ctrl + Alt + T).
-
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)
-
Install Apache:
sudo apt install apache2 -y
-
Check service is running:
sudo systemctl status apache2
(Press q to exit status view.)
-
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)
-
Install MySQL:
sudo apt install mysql-server -y
-
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
-
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
-
Install PHP + common modules:
sudo apt install php libapache2-mod-php php-mysql -y
-
Verify PHP version:
php -v
✅ Should show PHP 8.1 or later (default in Ubuntu 22.04).
???? Part F: Test PHP with Apache
-
Create a test PHP file:
sudo nano /var/www/html/info.php
-
Add:
<?php
phpinfo();
?>
-
Save (Ctrl+O → Enter → Ctrl+X).
-
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)
-
Default website folder: /var/www/html
-
Give ownership to your user (replace username):
sudo chown -R username:username /var/www/html
-
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.