How to Set Up Apache Virtual Hosts on Ubuntu 18.04

Introduction

Apache virtual hosts allow you to host multiple websites on a single server. This is a great way to save money and resources, as you can host multiple websites on a single server. In this tutorial, we will show you how to set up Apache virtual hosts on Ubuntu 18.04. We will cover how to create virtual hosts, configure Apache to serve the virtual hosts, and test the virtual hosts. By the end of this tutorial, you will have a working Apache virtual host setup on Ubuntu 18.04.

How to Set Up Apache Virtual Hosts on Ubuntu 18.04

1. Update your system:

sudo apt update
sudo apt upgrade

2. Install Apache:

sudo apt install apache2

3. Enable Apache modules:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

4. Create the virtual host configuration file:

sudo nano /etc/apache2/sites-available/example.com.conf

5. Add the following content to the file:


ServerName example.com
ServerAlias www.example.com
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

6. Enable the virtual host:

sudo a2ensite example.com.conf

7. Restart Apache:

sudo systemctl restart apache2

What is Apache Virtual Host?

Virtual Host allows you to run multiple websites from a single physical server or Virtual Private Server. There are two types of virtual hosts on Apache:

  • IP-Based Virtual Hosting – every individual website on the Apache Server uses a different, unique IP address.
  • Name-Based Virtual Hosts – enables you to add multiple domains using a single IP address.

This tutorial shows you how to set up Apache Virtual Hosts on an Ubuntu 18.04 system.

How to set up Apache virtual host on Ubuntu.

Prerequisites

Set Up Multiple Domains on a Single IP

Apache Virtual Host allows you to maximize your resources when setting up a website. With this powerful software, can use a single server and a single IP address to host a number of different domains.

Before you can configure Apache Virtual Hosts, you need to install the Apache webserver. To do so, run the command:

sudo apt-get install apache2

If you have any problems setting up the web server, refer to our an in depth tutorial on installing Apache on Ubuntu.

How to Set Up a Name-Based Virtual Host

Name-based virtual hosting allows the client to report the hostname to the server, as an element of the HTTP header. This feature means that one machine can host multiple websites sharing the same IP address.

Visual of name based virtual hosting

Step 1: Create a Directory Structure

Each virtual host needs to have a directory for storing virtual host data. Create directories and a directory structure at the following location /var/www. In our example, we’ve created phxnap1.com and phxnap2.com directories, one for each domain name.

1. Enter the following command and replace the example domain with your domain names:

sudo mkdir -p /var/www/phxnap1.com/public_html
sudo mkdir -p /var/www/phxnap2.com/public_html

Within the directories, we also created public_html. These directories are going to store website files for the domains.

2. Next, create a sample index.html page for each domain, using nano or your favorite text editor. Start with the first domain:

nano /var/www/phxnap1.com/public_html/index.html

3. Add the following sample HTML:

<html>
<head>
<title>Welcome to phoenixNAP 1!</title>
</head>
<body>
<h1>Well done! Everything seems to be working on your first domain!</h1>
</body>
</html>
A sample HTML for setting up multiple domains on Apache virtual host.

4. Save and exit the file.

5. Next, create a sample page for the second domain:

nano /var/www/phxnap2.com/public_html/index.html

6. Add the following lines to the file:

<html>
<head>
<title>Welcome to phoenixNAP 2!</title>
</head>
<body>
<h1>Well done! Everything seems to be working on your second domain!</h1>
</body>
</html>
Sample HTML file for setting up multiple domains on Apache virtual host.

7. Save and exit the second HTML file.

8. To prevent any permission issues modify the ownership of your document’s root directory to www-data. The chown command is useful in this instance:

sudo chown –R www-data:www-data /var/www/phxnap1.com
sudo chown –R www-data:www-data /var/www/phxnap2.com

You have now successfully changed the directory ownership to the Apache User.

Step 2: Create a Virtual Host Configuration File

Apache Virtual Host configuration files are stored in the /etc/apache2/sites-available directory.

1. To create a basic configuration file for your first domain, enter the domain information in the command:

sudo nano /etc/apache2/sites-available/phxnap1.com.conf

2. Add the following configuration block to create a basic configuration file. This example uses the first test domain, phxnap1.com. Make sure to enter the correct domain for your website:

<VirtualHost *:80>

ServerAdmin [email protected]
ServerName phxnap1.com
ServerAlias www.phxnap1.com
DocumentRoot /var/www/phxnap1.com/public_html

ErrorLog ${APACHE_LOG_DIR}/phxnap1.com-error.log
CustomLog ${APACHE_LOG_DIR}/phxnap1.com-access.log combined

</VirtualHost>
Apache congif file showing server settings
  • ServerName – represents the domain
  • ServerAlias – represents all other domains such as subdomains
  • DocumentRoot – the directory used by Apache to serve domain files
  • ErrorLog, CustomLog – designates the log files location

There is no established format. However, naming your configuration files based on the domain name is a “best practice”.

4. Once you have edited the config file for the first domain, repeat the process for the rest. In our case, we will run:

sudo nano /etc/apache2/sites-available/phxnap2.com.conf

5. Then, add the configuration block as in the example above, making sure to change the values for phxnap2.com:

<VirtualHost *:80>

ServerAdmin [email protected]
ServerName phxnap2.com
ServerAlias www.phxnap2.com
DocumentRoot /var/www/phxnap2.com/public_html

ErrorLog ${APACHE_LOG_DIR}/phxnap2.com-error.log
CustomLog ${APACHE_LOG_DIR}/phxnap2.com-access.log combined

</VirtualHost>
Apache virtual host configuration file for one of multiple domain names.

Step 3: Enable Virtual Host Configuration Files

To enable the virtual host file, create a symbolic link from the virtual host file to the sites-enabled directory. Apache2 reads this file when staring.

1. Use the a2ensite helper to enable the virtual host file with the command:

sudo a2ensite phxnap1.com

The output will appear as:

Use a2ensite helper to enable virtual host. Command line image.

2. Repeat the process for the second domain by typing:

sudo a2ensite phxnap2.com
Enable virtual host configuration files for multiple domains.

3. Next, verify the configuration file syntax is correct using the command:

sudo apachectl configtest

The message in the terminal will confirm that the syntax is correct: “Syntax OK”

4. Restart Apache2 for the changes to be applied:

sudo systemctl restart apache2

5. The only thing left to do is to go to a web browser and access your websites. Based on the index.html file we created earlier, the appropriate message should appear for each domain.

phxnap1.com
Web page displaying virtual host sucessfully created
phxnap2.com
Virtual host successfully created for multiple domains.

Step 4: Configure Firewall (Optional)

Modify your firewall settings to improve security by creating a rule to enable SSH on Ubuntu:

sudo ufw allow OpenSSH

Add the rules to allow access to Apache.

sudo ufw allow in “Apache Full”

Next, enable the firewall.

sudo ufw enable

If you see a message saying “Command may disrupt existing SSH connections.”  Press y. If the firewall is working properly, then you should see “Firewall is active and enabled on system startup.”

Conclusion

By following this tutorial, you have successfully created and configured an Apache Virtual Host on Ubuntu. You are now able to create multiple name-based virtual hosts, as well as run/host a large number of domains from a single server by using one IP address.

How to Set Up Apache Virtual Hosts on Ubuntu 18.04

Apache virtual hosts allow you to host multiple websites on a single server. This can be useful if you want to host multiple websites on a single server, or if you want to host a website in multiple languages. In this tutorial, we will show you how to set up Apache virtual hosts on Ubuntu 18.04.

Prerequisites

Before you begin this guide you’ll need the following:

  • A server running Ubuntu 18.04.
  • A non-root user with sudo privileges.
  • Apache installed and configured.

Step 1 — Creating the Directory Structure

We will create a directory structure for our virtual hosts. This will make it easier to manage our virtual hosts. We will create a directory for each virtual host in the /var/www/ directory. We will also create a directory for our logs.

First, create the directory for our logs:

sudo mkdir -p /var/www/logs

Next, create the directory for our first virtual host:

sudo mkdir -p /var/www/example.com/public_html

Repeat this step for each virtual host you want to create.

Step 2 — Setting Up the Document Root

We will now set up the document root for our virtual hosts. This is the directory that Apache will serve when a user visits our website.

First, create a file for our first virtual host:

sudo nano /var/www/example.com/public_html/index.html

Add the following content to the file:

<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>
    <h1>Success! The example.com virtual host is working!</h1>
  </body>
</html>

Save and close the file when you are finished.

Repeat this step for each virtual host you want to create.

Step 3 — Creating the Virtual Host Files

We will now create the virtual host files for our websites. These files tell Apache how to serve our websites.

First, create a file for our first virtual host:

sudo nano /etc/apache2/sites-available/example.com.conf

Add the following content to the file:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog /var/www/logs/example.com-error.log
    CustomLog /var/www/logs/example.com-access.log combined
</VirtualHost>

Save and close the file when you are finished.

Repeat this step for each virtual host you want to create.

Step 4 — Enabling the Virtual Hosts

We will now enable our virtual hosts. This will tell Apache to serve our websites when a user visits our domain.

First, enable the first virtual host:

sudo a2ensite example.com.conf

Repeat this step for each virtual host you want to enable.

Step 5 — Disabling the Default Virtual Host

We will now disable the default virtual host. This will prevent Apache from serving the default website when a user visits our domain.

Disable the default virtual host:

sudo a2dissite 000-default.conf

Step 6 — Restarting Apache

We will now restart Apache to apply our changes.

Restart Apache:

sudo systemctl restart apache2

Conclusion

In this tutorial, we showed you how to set up Apache virtual hosts on Ubuntu 18.04. You should now be able to host multiple websites on a single server.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?