{"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 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 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 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 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 Enter the following base content into your configuration file:<\/p>\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 In the above, ErrorLog<\/em> and CustomLog<\/em> use the default server log locations. If you have multiple websites it is a good idea to have separate files for access and error logs. This will help when it comes to troubleshooting and checking stats on a site-to-site basis.<\/p>\n\n\n\n Change the following lines:<\/p>\n\n\n\n to:<\/p>\n\n\n\n You can now use a2ensite<\/strong> to enable the new virtual host:<\/p>\n\n\n\n You can optionally disable the default site that is set up by Apache. This default site will be shown if you access your server using an IP address.<\/p>\n\n\n\n You can run the following command anytime to make sure your configuration file doesn’t contain syntax errors:<\/p>\n\n\n\n Finally, reload Apache so that these changes take effect:<\/p>\n\n\n\n Your new website is now active. Because the webroot \/var\/www\/domain_name\/public_html<\/em> is still empty we need to create an index.html file in that location to test that the virtual host works like so:<\/p>\n\n\n\n Now add the following content in this file:<\/p>\n\n\n\n Save and close the file.<\/p>\n\n\n\n Open your web browser<\/a> and enter your server’s domain name and you should see the above page served to your web browser. That is how to create an Apache virtual host with basic details.<\/p>\n\n\n\n Even though your website is live you will not be able to serve secure requests. To do this you will need to create another virtual host block to serve secure pages. Open the site’s configuration file:<\/p>\n\n\n\n Enter the following content at the end of your configuration file:<\/p>\n\n\n\n Save and close the file.<\/p>\n\n\n\n The Secure block adds four basic components as follows:<\/p>\n\n\n\n The values for SSLCertificateFile, SSLCertificateKeyFile, SSLCertificateChainFile<\/em> depend on how you acquired your SSL certificate and where it is placed on your system. You can follow this guide to learn how to install an SSL certificate on your Linux system and how to get the path.<\/p>\n\n\n\n You can now reload Apache so these changes take effect:<\/p>\n\n\n\n Go to your web browser and access your domain_name again but this time use HTTPS instead of HTTP. You should see the secure padlock on your web browser confirming that your server is now configured to server secure requests.<\/p>\n\n\n\n There are more tasks you would need to complete to create an Apache virtual host for your website. You may want to harden your server security and even protect it against DDOS attacks. You may also want to force HTTPS and install an SSL\/TLS certificate<\/a>, create aliases to non-public facing applications, and so on. All these are beyond the scope of this tutorial but all-in-all you still have a working site running off an Apache virtual host.<\/p>\n","protected":false},"excerpt":{"rendered":" Usually, when you set up your web server you want to have the capability to host more than one website or web application should the need arise. Similar to server…<\/p>\n","protected":false},"author":1,"featured_media":8805,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,16],"tags":[58,106,121,164,193,320,354,393,424,431,433,452,531,573,598,619,635,636,638],"yoast_head":"\nPrerequisites<\/h2>\n\n\n\n
Steps to Create Apache Virtual Host<\/h2>\n\n\n\n
$ sudo mkdir \/var\/www\/domain_name<\/code><\/pre>\n\n\n\n
$ sudo chown -R $USER:$USER \/var\/www\/domain_name<\/code><\/pre>\n\n\n\n
$ sudo nano \/etc\/apache2\/sites-available\/domain_name.conf<\/code><\/pre>\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
ErrorLog ${APACHE_LOG_DIR}\/error.log\nCustomLog ${APACHE_LOG_DIR}\/access.log combined<\/code><\/pre>\n\n\n\n
ErrorLog \/var\/www\/domain_name\/error.log\nCustomLog \/var\/www\/domain_name\/access.log combined<\/code><\/pre>\n\n\n\n
$ sudo a2ensite domain_name<\/code><\/pre>\n\n\n\n
$ sudo a2dissite 000-default<\/code><\/pre>\n\n\n\n
$ sudo apache2ctl configtest<\/code><\/pre>\n\n\n\n
$ sudo systemctl reload apache2<\/code><\/pre>\n\n\n\n
$ nano \/var\/www\/domain_name\/public_html\/index.html<\/code><\/pre>\n\n\n\n
<html>\n <head>\n <title>domain_name website<\/title>\n <\/head>\n <body>\n <h1>Hello World!<\/h1> \n <p>This is the home page of domain_name.<\/p>\n <\/body>\n<\/html><\/code><\/pre>\n\n\n\n
Add SSL to your Apache Virtual Host<\/h2>\n\n\n\n
$ sudo nano \/etc\/apache2\/sites-available\/domain_name.conf<\/code><\/pre>\n\n\n\n
<IfModule ssl_module>\n <VirtualHost *:443>\n ServerName domain_name\n ServerAlias www.domain_name \n ServerAdmin admin@domain_name\n DocumentRoot \/var\/www\/domain_name\/public_html\n ErrorLog \/var\/www\/domain_name\/error.log\n CustomLog \/var\/www\/domain_name\/access.log combined\n\n SSLEngine on\n SSLCertificateFile \/path\/to\/domain_name\/certificate_file\n SSLCertificateKeyFile \/path\/to\/domain_name\/certificate_key_file \n SSLCertificateChainFile \/path\/to\/domain_name\/certificate_chain_file\n\n <\/VirtualHost>\n<\/IfModule><\/code><\/pre>\n\n\n\n
$ sudo systemctl reload apache2<\/code><\/pre>\n\n\n\n
Conclusion<\/h2>\n\n\n\n