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.