{"id":8848,"date":"2020-07-27T14:56:33","date_gmt":"2020-07-27T18:56:33","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=8848"},"modified":"2021-12-04T07:33:15","modified_gmt":"2021-12-04T07:33:15","slug":"create-apache-virtual-host-website","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/create-apache-virtual-host-website\/","title":{"rendered":"How to Create Apache Virtual Host for Your Website"},"content":{"rendered":"\n

Usually, when you set up your web server you want to have the capability to host more than one website or web application<\/a> should the need arise. Similar to server blocks in Nginx<\/a>, here we will show you how to create a virtual apache host for your website.<\/p>\n\n\n\n

In this tutorial, we will be using Ubuntu 20.04. Other Linux<\/a> distros or even older Ubuntu<\/a> versions have similar instructions. The major difference is the default folder locations in other Linux distros.<\/p>\n\n\n\n

Prerequisites<\/h2>\n\n\n\n

This guide assumes you have already set up a server installation of Ubuntu 20.04 with a firewall configured, SSL certificate installed and configured<\/a>, and an Apache<\/a> web server installed. You can learn how to install a LAMP stack on Ubuntu by following this tutorial<\/a>. LAMP stack will get you Apache, MySQL<\/a>, and PHP<\/a> on your system.<\/p>\n\n\n\n

You will also need to have a user added to your system with sudo <\/em>administrative privileges.<\/p>\n\n\n\n

Steps to Create Apache Virtual Host<\/h2>\n\n\n\n

By default Apache on Ubuntu 20.04 is configured to serve documents from the \/var\/www\/html<\/em> directory. This is well and good but suitable for only single websites. This needs to change to accommodate multiple websites.<\/p>\n\n\n\n

The convention we will use here is to set each virtual host site as a child of \/var\/www<\/em>. That way your site will be hosted within domain_name giving us a structure such as \/var\/www\/domain_name<\/em>.<\/p>\n\n\n\n

Create the directory for domain_name using the following command:<\/p>\n\n\n\n

$ sudo mkdir \/var\/www\/domain_name<\/code><\/pre>\n\n\n\n

Next, assign ownership of the directory. In this case, we will set the ownership with the $USER<\/strong> environment variable, which will reference your current user:<\/p>\n\n\n\n

$ sudo chown -R $USER:$USER \/var\/www\/domain_name<\/code><\/pre>\n\n\n\n

In some cases the user would set to the Apache server user www-data<\/strong>, however, this is not always the best idea with multiple sites setups. if one site is compromised using this account then the unauthorized user has privileged access to all the sites.<\/p>\n\n\n\n

Next, you will need to open a new configuration file in Apache’s sites-available directory using your preferred editor. In this case, we will be using nano:<\/p>\n\n\n\n

$ sudo nano \/etc\/apache2\/sites-available\/domain_name.conf<\/code><\/pre>\n\n\n\n

Enter the following base content into your configuration file:<\/p>\n\n\n\n

<VirtualHost *:80>\n    ServerName domain_name\n    ServerAlias www.domain_name \n    ServerAdmin admin@domain_name\n    DocumentRoot \/var\/www\/domain_name\/public_html\n    ErrorLog ${APACHE_LOG_DIR}\/error.log\n    CustomLog ${APACHE_LOG_DIR}\/access.log combined\n<\/VirtualHost><\/code><\/pre>\n\n\n\n

Save and close the file when you’re done by pressing CTRL+S<\/strong> the CTRL+X<\/strong>, or simply CTRL+X<\/strong> then Y<\/strong> and ENTER<\/strong>.<\/p>\n\n\n\n

Definitions in the configuration file:<\/p>\n\n\n\n