Linux Tactic

Mastering Multiple Websites on Apache: Step-by-Step Virtual Host Setup

Setting Up Multiple Websites on Apache with Virtual Hosts

If you’re a website owner or a developer with multiple websites that require hosting, you may find yourself in a situation where you need to configure Apache to serve different websites from the same server. That’s where virtual hosts come in.

In this article, we’ll go through the process of setting up Apache and configuring virtual hosts, step-by-step, to enable hosting of multiple websites on the same server.

Part 1 – Installing Apache

Before we can get started, we need to ensure that Apache is installed on our server. In most cases, Apache ships with most Linux distributions, but it might not be enabled by default.

To confirm whether Apache is installed, open up your terminal and run the following command:

sudo systemctl status apache2

If Apache is running, you’ll see output that looks something like:


apache2.service – The Apache HTTP Server

Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)

Active: active (running) since Fri 2022-01-14 18:08:21 UTC; 3h 56min ago


Main PID: 11007 (apache2)

Tasks: 8 (limit: 512)

Memory: 5.2M

CGroup: /system.slice/apache2.service

11007 /usr/sbin/apache2 -k start

11008 /usr/sbin/apache2 -k start

11009 /usr/sbin/apache2 -k start

11010 /usr/sbin/apache2 -k start

11011 /usr/sbin/apache2 -k start

11012 /usr/sbin/apache2 -k start

11013 /usr/sbin/apache2 -k start

11014 /usr/sbin/apache2 -k start


If not, you can run the following command to install it:

sudo apt install apache2

Part 2 – Setting up a Virtual Host

2.1 -to Virtual Host

A virtual host is an Apache configuration directive that enables multiple websites to be served from a single server. With virtual hosts, we can use one Apache server to host many different websites.

Each virtual host runs independently of the other virtual hosts, with separate configuration settings and website files. 2.2 – Creating Document Root Directory

Before we can create a virtual host, we need to create a directory to store the website files.

We’ll do this for each website we want to host. Using the following command, let’s create a directory for our new website:

sudo mkdir -p /var/www/

The “-p” flag with the mkdir command tells it to create any missing parent directories.

In this case, we’re creating a directory called “” inside the “html” folder of the /var/www directory. 2.3 – Changing Ownership of Directory

Once we have created the directory, we need to change its ownership to the Apache user so that it can serve files from it.

Use the following command to change the ownership of the directory:

sudo chown -R www-data:www-data /var/www/

The “-R” flag tells the chown command to apply the change recursively, meaning that it applies the change to all files and directories within the “html” folder. 2.4 – Creating Virtual Host Configuration

Next, we need to create a virtual host configuration file for our website.

The Apache default virtual host configuration file is called “000-default.conf” and is located in the /etc/apache2/sites-available directory. We could edit the default file directly but it’s recommended we create a separate file for each website we host, so we’ll create a new file called “”.

We’ll use a text editor to create the new file:

sudo nano /etc/apache2/sites-available/

In this file, we’ll add the following configuration:




DocumentRoot /var/www/

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined


The above configuration sets up a virtual host that listens to incoming requests on the standard HTTP port 80 (*:80). In addition, it specifies the server name as “” and creates an alias for it as “”.

The DocumentRoot points to where we have saved the website files (/var/www/ Finally, it specifies the log files for the site, which includes any error messages and access logs.

2.5 – Activating Virtual Host Configuration

After we have created the virtual host configuration file, we need to activate it. We can use the following command:

sudo a2ensite

This creates a symbolic link to the newly created file in the /etc/apache2/sites-enabled directory, effectively activating the virtual host.

2.6 – Testing Configuration

Before we restart Apache, we need to make sure that the configuration file we created is free of syntax errors. We can do this using the following command:

sudo apachectl configtest

If everything is okay, we’ll see:


Syntax OK


If there are any problems with the syntax of the configuration file we created, we’ll need to go back to step 2.4 and re-edit the file. 2.7 – Verifying Virtual Host

After we’ve verified the configuration of our virtual host, we need to restart Apache to apply the changes:

sudo systemctl restart apache2

We can now test to confirm that everything is working by opening a web browser and visiting the URL “”. If everything is set up correctly, we should see a test web page that we can use to confirm that the website is up and running.


In this article, we have gone through the process of installing Apache and setting up virtual hosts to host multiple websites on the same server. We’ve covered everything from creating the document root directory and setting permissions to configuring virtual hosts, creating the configuration file, and verifying it.

By following these simple steps, you can easily configure Apache and virtual hosts to host multiple websites on the same server. In this expanded article, we’ll explore several additional topics to help you further understand the process of installing and configuring Apache as a web server and a proxy server.

We’ll also touch on the steps involved in deploying applications on Apache and how to incorporate feedback/comments on your website.

Part 3 – Install and Configure Apache as a Proxy Server

Apache can also function as a proxy server, allowing it to forward requests from clients to other servers. This can be done for caching or load balancing purposes.

To set up Apache as a proxy server, follow these steps:

1. Enable the proxy module:

sudo a2enmod proxy

2. Enable the proxy_http module:

sudo a2enmod proxy_http

3. Create a configuration file for the proxy:

sudo nano /etc/apache2/sites-available/proxy.conf

Add the following configuration:


ProxyPreserveHost On

ProxyPass / http://localhost:3000/

ProxyPassReverse / http://localhost:3000/




Enable the new configuration:

sudo a2ensite proxy.conf

5. Restart Apache:

sudo systemctl restart apache2

Now, when requests come in for “”, Apache will forward them to http://localhost:3000/.

Part 4 – Deploying Applications on Apache

Once you have Apache set up and running, you can deploy applications using different methods. One popular method is to use the Apache module called mod_wsgi.

It allows for Python web applications to run natively on Apache. Follow these steps to set up mod_wsgi:


Install mod_wsgi:

sudo apt-get install libapache2-mod-wsgi-py3

2. Create a Python application:

sudo mkdir /var/www/myapp

sudo nano /var/www/myapp/

Add the following code:


def application(environ, start_response):

status = ‘200 OK’

output = b’Hello World!’

response_headers = [(‘Content-type’, ‘text/plain’), (‘Content-Length’, str(len(output)))]

start_response(status, response_headers)

return [output]



Create a WSGI file for the application:

sudo nano /var/www/myapp/myapp.wsgi

Add the following code:


#!/usr/bin/env python3

import sys

sys.path.insert(0, ‘/var/www/myapp’)

from myapp import application as app

if __name__ == ‘__main__’:

from wsgiref.simple_server import make_server

server = make_server(”, 8080, app)



4. Edit the Apache configuration to enable mod_wsgi:

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following code:


WSGIScriptAlias /myapp /var/www/myapp/myapp.wsgi



Restart Apache:

sudo systemctl restart apache2

Now, when you visit, you should see “Hello World!”. Part 5 – Incorporating Feedback/Comments

One way to improve the user experience of your website is to incorporate feedback and comments from users.

We can use a variety of tools to achieve this, such as Disqus, Facebook Comments, or our own custom solution. For a custom solution, we can use a simple PHP script to allow users to leave comments on our website.

Here’s how:

1. Create a new directory for your PHP script:

sudo mkdir /var/www/comments


Create a new PHP file:

sudo nano /var/www/comments/index.php

Add the following code:


$commentsFile = __DIR__ . ‘/comments.txt’;

if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && isset($_POST[‘comment’])) {

$comment = htmlentities($_POST[‘comment’]);

if (file_put_contents($commentsFile, $comment .

PHP_EOL, FILE_APPEND) === false) {

exit(‘Failed to write comment to file’);



$comments = file($commentsFile, FILE_IGNORE_NEW_LINES);





The PHP script saves comments to a file called “comments.txt” and displays them on the web page. 3.

Edit the Apache configuration to serve the new PHP script:

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following code:


Alias /comments /var/www/comments

Options Indexes FollowSymLinks MultiViews

AllowOverride All

Order allow,deny

allow from all


4. Restart Apache:

sudo systemctl restart apache2

Now, when you visit, you should see a form to leave comments, and any comments that have been submitted will also be displayed.


In this expanded article, we’ve covered several additional topics related to setting up Apache as a web server and a proxy server, deploying applications on Apache, and incorporating feedback/comments on your website. By following these steps, you can set up a robust and functional web server capable of serving multiple websites and applications.

In conclusion, this article has covered the important steps necessary to install, configure, and utilize Apache as a web server and a proxy server. The article has presented instructions for creating virtual hosts, deploying web applications, and incorporating feedback/comments on your website.

Following the steps outlined in this article will allow users to serve multiple websites and applications on a single server while also providing an improved user experience through feedback and comments. Whether you are an experienced developer or just starting, the knowledge conveyed in this article will contribute to your appreciation of the powerful capabilities of Apache.

The key takeaway is that by understanding Apache, you can deliver a website with reliable performance and a great user experience.

Popular Posts