How to Install LAMP Stack on Ubuntu with Virtual Hosts

Senior WebCoder

The LAMP stack is one of the most popular ways to run websites and applications. It combines:
- Linux → Operating system
- Apache → Web server
- MySQL → Database
- PHP → Server-side language
In this guide, you’ll learn how to install LAMP stack on Ubuntu step by step. We’ll cover everything from installing Apache and setting up a firewall, to configuring a Virtual Host, testing PHP, and linking PHP with MySQL. By the end, your server will be ready to run modern PHP applications.
Table of Contents
- Step 1 — Install Apache and Update Firewall
- Step 2 — Install MySQL Database
- Step 3 — Install PHP
- Step 4 — Configure Apache Virtual Host
- Step 5 — Test PHP Processing
- Step 6 — Test Database Connection
- Conclusion
Step 1 — Install Apache and Update Firewall
Apache is the web server that delivers content to users.
Update your system and install Apache:
sudo apt update
sudo apt install apache2
Check status:
systemctl status apache2
If you see active (running), Apache is working.
Configure Firewall
List profiles:
sudo ufw app list
Allow HTTP traffic:
sudo ufw allow "Apache"
Now visit:
http://your_server_ip
If you see the Apache default page, your server is live.

Step 2 — Install MySQL Database
Next, install MySQL to manage website data.
sudo apt install mysql-server
Secure MySQL
sudo mysql_secure_installation
Set a root password, remove test databases, and block remote root access.
Test MySQL
sudo mysql -u root -p
If you see the MySQL prompt, installation is successful.
Step 3 — Install PHP
PHP connects Apache with MySQL.
Install PHP with common extensions:
sudo apt install php libapache2-mod-php php-mysql
Check PHP version:
php -v
Step 4 — Configure Apache Virtual Host
Set up a custom Virtual Host instead of relying on the default web root.
Create directory:
sudo mkdir /var/www/your-project-name
sudo chown -R $USER:$USER /var/www/your-project-name
Create config file:
sudo nano /etc/apache2/sites-available/your-project-name.conf
Add:
<VirtualHost *:80>
ServerName your-project-name.com
ServerAlias www.your-project-name.com
DocumentRoot /var/www/your-project-name
ErrorLog ${APACHE_LOG_DIR}/your_project_name_error.log
CustomLog ${APACHE_LOG_DIR}/your_project_name_access.log combined
</VirtualHost>
Enable the site and reload:
sudo a2ensite your-project-name
sudo systemctl reload apache2
3) Add Test Page
nano /var/www/your-project-name/index.html
Paste:
<h1>Welcome from My Project!</h1>
Visit in browser: http://your_server_ip
You should see:

Step 5 — Test PHP Processing
Create test file:
nano /var/www/your-project-name/info.php
Add:
<?php
phpinfo();
?>
Visit:
http://your_server_ip/info.php
You should see PHP details.

Step 6 — Test Database Connection
Create Database
sudo mysql
CREATE DATABASE sampledb;
CREATE USER 'sampleuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON sampledb.* TO 'sampleuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Create PHP DB Test Script
nano /var/www/your-project-name/dbtest.php
<?php
$mysqli = new mysqli("localhost", "sampleuser", "password", "sampledb");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Database connection successful!";
$mysqli->close();
?>
Visit:
http://your_server_ip/dbtest.php
You should see: Database connection successful!
Step 7 — Install Extra PHP Extensions (Optional)
sudo apt install php-cli php-curl php-mbstring php-xml php-zip
php-cli→ Run PHP from command linephp-curl→ API requestsphp-mbstring→ UTF-8 supportphp-xml→ XML handlingphp-zip→ File compression
Conclusion
You’ve successfully set up the LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu and configured a Virtual Host.
With this foundation, you can now:
- 🚀 Launch WordPress or other CMS
- 🛠️ Develop with Laravel, Symfony, or custom PHP apps
- 🔒 Prepare for production with SSL (Let’s Encrypt), secure MySQL users, and Apache hardening
👉 Next steps: 📈 To boost your online presence, follow a SEO Action Plan - 2025 to climb the rankings and attract unstoppable organic traffic.

Abinesh S
Senior WebCoder
Senior WebCoder at FUEiNT, specializing in advanced frontend architecture, Next.js, and performance optimization. Passionate about determining the best tools for the job.
