Set up a website on an Ubuntu Server using Apache Virtual Hosts (Name-Bassed Hosting)
In this guide, we will show you how to configure a website using Apache virtual hosts. The configuration of Apache virtual hosts over the different operating system versions like CentOS, Ubuntu and Debian is much the same. This guide assumes that you have access to a managed DNS service. Most domain registrars provide a service where you can configure A records and DNS records when using their DNS servers. You can ask your registrar to add these A records for you or if you have access to the domain names dns zone file you can add or edit the A entry as required.
Our development server for this guide is 151.80.124.86 so our A records look like this


Update Server
To begin, if your server does not have Apache, install it but first ensure the server is fully up-to-date;
sudo apt-get update
sudo apt-get upgrade
Install Apache On an Ubuntu server
sudo apt-get install apache2
After you have installed Apache you should be able to visit your servers IP address in a browser. You should see the Apache test page. This confirms your Apache server is installed and ready for configuration.

Now we need to configure Apache to resolve our domain name. First, we are going to create a directory where Apache will look for our websites files. You need to create a folder for each website that will be hosted on the server. Our first website will be first2host.com.
sudo mkdir -p /var/www/html/first2hostcom/public_html/
And a directory for the second website, we are using first2host.net
sudo mkdir -p /var/www/html/first2hostnet/public_html/
Both of these folders will be owned by the root user. We need to change this to a regular user. Create a user using the adduser command. Switch first2host for your username.
addusr first2host
Now, add a password for that user. Again, switch first2host for the username you picked above.
passwd first2host
Now assign the two folders you created to this user. We use CHOWN to do this
sudo chown -R first2host:first2host /var/www/html/first2hostcom/public_html
and the second folder, remember to CHROW the right username
sudo chown -R first2host:first2host /var/www/html/first2hostnet/public_html
Now set the correct permissions on the Apache root directory.
sudo chmod -R 755 /var/www/html/
We set the Apache directory to read only because we are going to be configuring Apache to access our websites files via the directories we created above.
Create Demo Website Pages
Now we are going to create demo pages for our websites so we know they are resolving and loading correctly. These will be called index.html and will be placed in the respective websites home directory.
nano /var/www/html/first2hostcom/public_html/index.html
In this new file paste the below lines and save the file
<html>
<head>
<title>First2Host</title>
</head>
<body>
<h1>Great, you have a working website running off an Ubuntu server with Apache.</h1>
</body>
</html>
Repeat the above step for each directory you created above saving the demo index.html file into each websites public_html directory.
nano /var/www/html/first2hostnet/public_html/index.html
<html>
<head>
<title>First2Host</title>
</head>
<body>
<h1>Great, you have a working website running off an Ubuntu server with Apache.</h1>
</body>
</html>
Configure your Apache Virtual Hosts
Next, we’re going to modify the files in Apache to accommodate our websites. You can use different IP addresses for different websites if you wish and we’re going to show you how to also use just one IP for multiple websites. We can copy the default Apache virtual host file and modify this to meet our needs.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/first2hostcom.conf
And for our second domain name, just repeat if you have more domain names.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/first2hostnet.conf
You must save your virtual hosts files ending .conf or this will not work. Now let’s edit the files to work from your website
nano /etc/apache2/sites-available/first2hostnet.conf
The only things that need to be changed in this file are the following lines
ServerAdmin [email protected]
ServerName server.first2host.com
ServerAlias www.first2host.com
DocumentRoot /var/www/html/first2hostcom/public_html
Post-Apache File Update
After you have changed the information to reflect your domain name the file should look like this

Repeat the process for the second domain name, in our example the first2host.net domain name
nano /etc/apache2/sites-available/first2hostnet.conf

Save and close the file. That is the Apache virtual hosts setup completed. Next, let’s disable the default virtual hosts file and enable the virtual hosts files we just created
sudo a2ensite first2hostcom.conf
sudo a2ensite first2hostnet.conf
sudo a2dissite 000-default.conf
Now, restart Apache
sudo systemctl restart apache2
Finally, let’s open up the hosts file and specify what IPs are going to be used for our website. We are just using a single IP, the servers main IP
nano /etc/hosts
You can list all domain names in this file and specify the main server IP. The file should look like this.
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
151.80.124.86 first2host.com
151.80.124.86 first2host.net
127.0.0.1 localhost.localdomain localhost
151.80.124.86 server.apache.com server
::1 localhost ip6-localhost ip6-loopback
You should now be able to visit each site in a browser. Here’s our first site

And finally the second site

That’s the complete guide on configuring a website to run off an Apache server using a single IP. That’s called name-based hosting. If you are unable to see your website at this point ensure you have configured the A records correctly. Ask your domain name registrar or hosting provider to check this for you.
How was this article? – Apache Virtual Hosts
You might also like
More from All About Linux
Install Ioncube Loaders In Ubuntu, Debian, CentOS and AlmaLinux
Ioncube Loaders are a piece of software that is used to protect the underlying code in PHP applications. Its aim …
How to install FTP and configure FTP on an Ubuntu 22 LTS instance
If you need to upload files to your NVMe VPS you have a couple of options. You can use a …
How to install a Cloudflare Origin SSL Certificate – NGINX
An SSL Certificate is vital to encrypt data between you and your clients. SSLs can be complicated things. If they …