Introduction
Apache virtual hosts allow hosting multiple websites on a single server by creating individual configuration files for each site and pointing them to specific directories. This setup saves costs by eliminating the need for separate servers, efficiently utilizes server resources, and simplifies management tasks. By centralizing updates, file management, and performance monitoring, Apache Virtual Hosts provide a cost-effective, efficient, and manageable solution for running multiple websites.
This guide shows you how to host multiple websites with Apache on Ubuntu 24.04.
Prerequisites
- Ubuntu 24.04 server. We recommend a Digital Ocean VPS server.
- A non-root user with sudo privileges
- Apache webserver. Follow our guide on How to Install Apache On Ubuntu 24.04.
Set Up Directory Structure for Websites
For each website you intend to host on your Apache web server, you should create the correct directories and set up the appropriate permissions. This guide intends to run the following websites:
example.com
example.info
-
example.net
-
Create three directories in the root directory of your webserver.
CONSOLE$ sudo mkdir -p /var/www/example.com/public_html $ sudo mkdir -p /var/www/example.info/public_html $ sudo mkdir -p /var/www/example.net/public_html
-
Take ownership of the new directories.
CONSOLE$ sudo chown -R $USER:$USER /var/www/example.com/public_html $ sudo chown -R $USER:$USER /var/www/example.info/public_html $ sudo chown -R $USER:$USER /var/www/example.net/public_html
-
Set the appropriate permissions for the directories.
CONSOLE$ sudo chmod -R 755 /var/www/example.com/public_html $ sudo chmod -R 755 /var/www/example.info/public_html $ sudo chmod -R 755 /var/www/example.net/public_html
-
Ensure any files or new directories that you create inside the above base directories inherit the permissions.
CONSOLE$ sudo find /var/www/example.com/public_html -type d -exec chmod g+s {} \; $ sudo find /var/www/example.info/public_html -type d -exec chmod g+s {} \; $ sudo find /var/www/example.net/public_html -type d -exec chmod g+s {} \;
Set Up Virtual Host Files for Websites
For each website that you intend to run, you must set up a virtual host file.
-
example.com
:-
Create a new
/etc/apache2/sites-available/example.com.conf
fileCONSOLE$ sudo nano /etc/apache2/sites-available/example.com.conf
-
Enter the following information.
ApacheConf<VirtualHost *:80> ServerAdmin admin@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/example.com/public_html> Options -Indexes +FollowSymLinks -MultiViews AllowOverride All Require all granted </Directory> </VirtualHost>
-
-
example.info
:-
Create a new
/etc/apache2/sites-available/example.info.conf
fileCONSOLE$ sudo nano /etc/apache2/sites-available/example.info.conf
-
Enter the following information.
ApacheConf<VirtualHost *:80> ServerAdmin admin@example.info ServerName example.info ServerAlias www.example.info DocumentRoot /var/www/example.info/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/example.info/public_html> Options -Indexes +FollowSymLinks -MultiViews AllowOverride All Require all granted </Directory> </VirtualHost>
-
-
example.net
:-
Create a new
/etc/apache2/sites-available/example.net.conf
fileCONSOLE$ sudo nano /etc/apache2/sites-available/example.net.conf
-
Enter the following information.
ApacheConf<VirtualHost *:80> ServerAdmin admin@example.net ServerName example.net ServerAlias www.example.net DocumentRoot /var/www/example.net/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/example.net/public_html> Options -Indexes +FollowSymLinks -MultiViews AllowOverride All Require all granted </Directory> </VirtualHost>
-
-
Enable the virtual host configurations for each website.
CONSOLE$ sudo a2ensite example.com.conf $ sudo a2ensite example.info.conf $ sudo a2ensite example.net.conf
-
Disable the default virtual host.
CONSOLE$ sudo a2dissite 000-default.conf
-
Restart Apache to load the new changes.
CONSOLE$ sudo systemctl restart apache2
Create Sample Home Pages for Websites
Create a sample home page for each website you intend to run with the Apache server.
-
example.com
:-
Create a new
/var/www/example.com/public_html/index.html
file.CONSOLE* Paste the information below in the file.$ sudo nano /var/www/example.com/public_html/index.html
HTML<html> <head> <title>example.com</title> </head> <body> <h1>This is a sample page for example.com website</h1> </body> </html>
-
-
example.info
:-
Create a new
/var/www/example.info/public_html/index.html
file.CONSOLE$ sudo nano /var/www/example.info/public_html/index.html
-
Paste the information below in the file.
HTML<html> <head> <title>example.info/title> </head> <body> <h1>This is a sample page for example.info website</h1> </body> </html>
-
-
example.net
:-
Create a new
/var/www/example.net/public_html/index.html
file.CONSOLE$ sudo nano /var/www/example.net/public_html/index.html
-
Paste the information below in the file.
HTML<html> <head> <title>example.net/title> </head> <body> <h1>This is a sample page for example.net website</h1> </body> </html>
-
Edit your Local Computer Hosts File
On your local computer, add the example.com
, example.net
, and example.info
sample domain names to your hosts
file and point the domain names to your server's public IP address.
If you are running Windows, follow the steps below.
-
Edit the
c:\Windows\System32\Drivers\etc\hosts
file and remember to replace192.88.99.1
with the public IP address associated with your Ubuntu server.INI# Copyright (c) 1993-2009 Microsoft Corp. ... # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 192.88.99.1 example.com 192.88.99.1 example.info 192.88.99.1 example.net ...
-
Save the file and visit each domain name on your browsers, you should see different web content for each domain.
-
example.com
:http://example.com
-
example.info
:http://example.info
-
example.net
:http://example.net
-
Conclusion
In this guide, you've configured multiple websites using Apache Virtual Hosts. You have set up different domain names, created separate configuration files for each site, and pointed them to their respective directories. To further enhance these websites, consider buying a domain name from a domain registrar and point the domain names A records to your server's public IP address.