CoachingUgosay

How to Install LAMP Stack on Ubuntu with Virtual Hosts

Abinesh S

Abinesh S

Senior WebCoder

Video Thumbnail

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

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.

Apache info

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:

Index page

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.

PHP info

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 line
  • php-curl โ†’ API requests
  • php-mbstring โ†’ UTF-8 support
  • php-xml โ†’ XML handling
  • php-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.

More articles

HTML Attributes and Techniques for Web Performance

Speed up your site using HTML attributes like preload, preconnect, prefetch, async, defer, fetchpriority, and lazy loading to boost UX and SEO

Read more

WordPress Database Structure and Tables

Explore WordPress database structure, default tables, and storage of content, users, settings, and plugins to manage and troubleshoot sites.

Read more

Connect with Us

Got questions or need help with your project? Fill out the form, and our team will get back to you soon. Weโ€™re here for inquiries, collaborations, or anything else you need.

Address
12, Sri Vigneshwara Nagar, Amman Kovil
Saravanampatti, coimbatore, TN, India - 641035