Back to Writing
PHP4 min readMarch 17, 2026

How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts

A complete step-by-step guide to building a reliable Ubuntu web server stack with Apache, MySQL, PHP, and virtual host configuration.

Ashim Rudra Paul

Ashim Rudra Paul

Software Engineer

How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts

How to Create a Robust Ubuntu Web Server Using Apache, MySQL, PHP, and Virtual Hosts

Importance of a Web Server

A web server is the backbone of any website, serving as the platform that delivers content to users across the globe. The efficiency and reliability of your web server are critical to the success of your online presence.

Overview of Ubuntu, Apache, MySQL, PHP, and Virtual Hosts

In this guide, we will set up a complete web server environment on Ubuntu using:

  • Apache as the web server
  • MySQL as the database server
  • PHP as the scripting language
  • Virtual Hosts to run multiple websites on one server

Purpose of This Guide

This article provides a detailed, step-by-step process for setting up a robust web server on Ubuntu, tailored for both beginners and advanced users.


Preliminary Setup

Choosing the Right Hardware

Before installing software, ensure your hardware is suitable for expected traffic and workload.

Consider:

  • CPU performance
  • RAM capacity
  • SSD/HDD storage size and speed

Installing Ubuntu Server

  1. Download the latest Ubuntu Server ISO from the official Ubuntu website.
  2. Create a bootable USB drive.
  3. Install Ubuntu Server using the on-screen installer.

Updating and Upgrading Ubuntu

After installation, update your packages:

bash
1sudo apt update
2sudo apt upgrade

Installing Apache

Understanding Apache Web Server

Apache is one of the most widely used web servers, known for robustness, flexibility, and extensive module support.

Install Apache

bash
1sudo apt install apache2

Start and Enable Apache

bash
1sudo systemctl start apache2
2sudo systemctl enable apache2

Verify Apache Installation

bash
1sudo systemctl status apache2

Installing MySQL

Understanding MySQL Database Server

MySQL is a powerful relational database management system used to store and manage data for websites and applications.

Install MySQL

bash
1sudo apt install mysql-server

Secure MySQL Installation

Run the built-in security script:

bash
1sudo mysql_secure_installation

Follow the prompts to:

  • Set root password
  • Remove anonymous users
  • Disable remote root login
  • Remove test database

Test MySQL Functionality

bash
1sudo mysql -u root -p

Installing PHP

Understanding PHP Scripting Language

PHP is a popular server-side language used for dynamic web applications and database integration.

Install PHP and Core Modules

bash
1sudo apt install php libapache2-mod-php php-mysql

Install Additional PHP Extensions

bash
1sudo apt install php-mbstring php-mysql php-curl php-cli php-dev php-imagick php-soap php-zip php-xml php-imap php-xmlrpc php-gd php-opcache php-intl

Integrate PHP with Apache

You may need to update Apache directory index priority:

bash
1sudo nano /etc/apache2/mods-enabled/dir.conf

Configuring Virtual Hosts

What Are Virtual Hosts?

Virtual hosts allow multiple websites/domains to run on one Apache server. Each site can have its own:

  • Document root
  • Logging configuration
  • Domain settings

Create Site Directory

bash
1sudo mkdir /var/www/demo

Set Permissions

bash
1sudo chown -R $USER:$USER /var/www/demo
2sudo chmod -R 755 /var/www/demo

Create Virtual Host Config File

bash
1sudo nano /etc/apache2/sites-available/demo.conf

Add:

apache
1<VirtualHost *:80>
2 ServerAdmin webmaster@localhost
3 ServerName demo
4 ServerAlias www.demo
5 DocumentRoot /var/www/demo
6 ErrorLog ${APACHE_LOG_DIR}/error.log
7 CustomLog ${APACHE_LOG_DIR}/access.log combined
8</VirtualHost>

Enable the Virtual Host and Validate Apache Config

bash
1sudo a2ensite demo.conf
2sudo apache2ctl configtest

Restart Apache

bash
1sudo systemctl restart apache2

Edit Hosts File for Local Testing

bash
1sudo nano /etc/hosts

Add this line:

text
1127.0.0.1 demo

Test Virtual Host

Open this in your browser:

text
1http://demo/

If configured correctly, your demo site should load from /var/www/demo.


Final Thoughts

With this setup, you now have a complete LAMP stack on Ubuntu:

  • Linux (Ubuntu)
  • Apache
  • MySQL
  • PHP

You can now host multiple websites efficiently using virtual hosts and scale your server setup as your projects grow.