Set up Apache Virtual Host on Ubuntu 20.04

In this article, we want to teach you How to set up an Apache virtual host on Ubuntu 20.04. As you may know, virtual hosts allow more than one Web site on one system or Web server. The servers are different by their hostname. Visitors to the Web site are routed by hostname or IP address to the correct virtual host. Virtual hosting allows companies to share one server to each have their own domain names. Now you can follow the rest of the article to see how you can create an Apache virtual host on Ubuntu 20.04.

Learn To Set up Apache virtual host on Ubuntu 20.04

Before setting up Apache virtual host on Ubuntu 20.04, you need some requirements. Let’s see what we need.

Requirements

First, you need to log in as a non-root user with “Sudo” permissions. To do this, you can visit this guide on Initial Server Setup on Ubuntu 20.04.

Then, you need to install Apache and adjust a basic firewall by visiting our article on Install Apache on Ubuntu 20.04.

Also, you need a valid domain name that is pointed to your server’s IP address. Now follow the steps below to complete this guide.

Create a Virtual Host in Apache on Ubuntu

1) Create a directory for your domain:

sudo mkdir /var/www/stack.orcacore.net

2) Set ownership of the HTML directory with the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/stack.orcacore.net

3) To set up an Apache virtual host you need to set default permissions for your webroot:

sudo chmod -R 755 /var/www/stack.orcacore.net

4) create a sample index.html page using Vi editor:

sudo vi /var/www/stack.orcacore.net/index.html

Add the following content to the file:

<html>
<head>
<title>Welcome to Stack.orcacore.net!</title>
</head>
<body>
<h1>Success! The Stack.orcacore.net server block is working!</h1>
</body>
</html>

Save and close the file, when you are finished.

Then continue to set up the Apache virtual host.

5) You need to create a sites-available directory:

sudo vi /etc/apache2/sites-available/stack.orcacore.net.conf

Add the following content to the file:

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

Save and close the file, when you are finished.

6) Enable the file with the a2ensite tool:

sudo a2ensite your_domain.conf

7) Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

8) Test for configuration errors:

sudo apache2ctl configtest

You should see the following output:

Ouput
Syntax OK

9) Restart your web server with the following command:

sudo systemctl restart apache2

Now you are ready to test your Apache virtual host on Ubuntu 20.04 configuration. From your Web browser, type your domain name:

http://stack.orcacore.net

You will see your Apache virtual host test page in the image below:

Apache virtual host on Ubuntu 20.04

Also, you can test PHP on your web browser.

Testing PHP processing on Apache Web server

First, create the info.php file then Type the PHP scripts below in it with the following command:

sudo vi /var/www/stack.orcacore.net/info.php
<?php
phpinfo();

When you are finished, save and close the file.

To visit your page, type this in your web browser:

http://stack.orcacore.net/info.php

You will see your PHP information.

If you want to remove this file after your test you can use this command:

sudo rm /var/www/stack.orcacore.net/info.php

You can always recreate this page if you need to access the information again later.

Conclusion

At this point, you have learned to Set up Apache Virtual Host on Ubuntu 20.04 by following the steps provided in the article. I hope you enjoy it.

Also, you may be interested in these articles:

Set up Apache Virtual Host File on Centos 7

Install LAMP Stack on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!