Create an Apache Virtual Host on Ubuntu 22.04

Posted
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.

Contents

Prerequisites

Type Host TTL Record
A ricbre.com 300 68.183.229.66
A www.ricbre.com 300 68.183.229.66

Throughout this guide I use two variables: the domain name ricbre.com and my cloud servers IP address 68.183.229.66. 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/ricbre.com

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/ricbre.com

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

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

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


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

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

Next, we enable your new website:

sudo a2ensite ricbre.com

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/ricbre.com/index.html

Then paste the following code inside the new document:


<html>
  <head>
    <title>Welcome to RicBre</title>
  </head>
  <body>
    <h1>Hello World!</h1>

    <p>Welcome to my new website <strong>ricbre.com</strong>.</p>
  </body>
</html>

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:

http://www.ricbre.com/
Enter your domain name to visit your new website

Conclusion

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

Author
Categories Ubuntu

Comments

There are currently no comments on this article.

Comment

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