Create an Apache Virtual Host on Ubuntu 22.04

Comments 0

If you followed my guide on how to install a LAMP server on Ubuntu 22.04, this guide is the next logical step in creating a secure web server. The Apache Virtual Host environment enables a higher degree of security, flexibility and the ability to host multiple websites and sub-domains. Apache achieves this by using website directories under /var/www/, and configuration (.conf) files filled with directives. This makes managing multiple websites easier and more secure.

We will be using HTTP port 80 (non-secure) initially, so after you complete this guide, head over to my other guide How to install Let’s Encrypt SSL Certificate on Ubuntu 22.04 and Apache.

This article is part of my series The Ultimate Web Server.



Type Host TTL Record
A 300
A 300

Throughout this guide I use two variables: the domain name and my cloud servers IP address You need to replace any instance of this data with your own domain name and cloud server IP address.

Creating a Virtual Host for your Website

First, we create the website directory inside /var/www/:

sudo mkdir /var/www/

Then we change ownership of the folder to your current system user (we’re using the $USER variable to reference the current user):

sudo chown -R $USER:$USER /var/www/

Now we create a new Virtual Host file using nano to hold all the configuration data:

sudo nano /etc/apache2/sites-available/

Inside this new file, copy and paste the following configuration data replacing with your own domain name:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Then save ctrl + o, ENTER and exit nano ctrl + x.

Next, we enable your new website:

sudo a2ensite

Then we disable the default Apache site so it doesn’t override your new site:

sudo a2dissite 000-default

Let’s confirm that your new website’s configuration is working (you should see the message syntax OK):

sudo apache2ctl configtest
Apache configtest result - Syntax OK

To enable the changes we need to restart Apache:

sudo systemctl reload apache2

Creating a Test HTML Page

Let’s create a new HTML document inside your websites root folder:

sudo nano /var/www/

Then paste the following code inside the new document:

    <title>Welcome to RicBre</title>
    <h1>Hello World!</h1>

    <p>Welcome to my new website <strong></strong>.</p>

Then save ctrl + o, ENTER and exit nano ctrl + x..

Open your web browser and enter your domain name address using HTTP (not HTTPS) otherwise you’ll get an err_connection_refused error:
Enter your domain name to visit your new website


You now have a working web server capable of hosting multiple websites or sub-domains. You can now take the next step and install a free Let’s Encrypt SSL certificate.

Newsletter Signup

Privacy Policy

See Also

Further Reading

Categories Ubuntu


There are currently no comments on this article.


Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.

PLEASE NOTE: You must preview a comment before submitting

Comments use Textile formatting