How to set up Apache virtual host on Ubuntu 20.04

In this article, we want to teach you How to set up Apache virtual host on Ubuntu 20.04.

What is a virtual host?

The concept of virtual hosts allows 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.

Requirements

Before setting up Apache virtual host on Ubuntu 20.04 you need to install the Apache and adjust a basic firewall by visiting our article about How to install Apache on Ubuntu 20.04. and you need to log in as a non-root user with “Sudo” permissions on your server with our article about the Initial server setup with Ubuntu 20.04.

How to set up Apache virtual host on Ubuntu 20.04

To set up an Apache virtual host on Ubuntu 20.04 follow these steps.

Set up virtual hosts on Ubuntu 20.04

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

Type this in your editor:

<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 Apache virtual host.

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

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

Type this in your editor:

<VirtualHost *:80>
ServerAdmin [email protected]
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:

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.

http://stack.orcacore.net

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

Apache virtual host on Ubuntu 20.04

Also, you can test PHP on your web browser.

Testing PHP processing on your web server

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

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

Then put the commands below in the file:

<?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

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.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular