Apache Virtual Hosts on CentOS Rumi, August 24, 2019 Step One— Create a New Directory The first step in creating a virtual host is to a create a directory where we will keep the new website’s information. This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory. sudo mkdir -p /var/www/example.com/public_html You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name. However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Six. Step Two—Grant Permissions We need to grant ownership of the directory to the user, instead of just keeping it on the root system. sudo chown -R apache:apache /var/www/example.com/public_html Additionally, it is important to make sure that everyone will be able to read our new files. sudo chmod 755 /var/www Now you are all done with permissions. Step Three— Create the Page We need to create a new file called index.html within our configurations directory. sudo vi /var/www/example.com/public_html/index.html We can add some text to the file so we will have something to look at when the IP redirects to the virtual host <html> <head> <title>www.example.com</title> </head> <body> <h1>Success: You Have Set Up a Virtual Host</h1> </body> </html> Save and Exit Step Four—Turn on Virtual Hosts The next step is to enter into the apache configuration file itself. sudo vi /etc/httpd/conf/httpd.conf There are a few lines to look for. Make sure that your text matches what you see below. #Listen 12.34.56.78:80 Listen 80 Scroll down to the very bottom of the document to the section called Virtual Hosts. NameVirtualHost *:80 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. # # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # <VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot /var/www/example.com/public_html ServerName www.example.com ServerAlias example.com ErrorLog /var/www/example.com/error.log CustomLog /var/www/example.com/requests.log </VirtualHost> The most important lines to focus on are the lines that say NameVirtualHost, Virtual Host, Document Root, and Server Name. Let’s take these one at a time. ServerAlias is a new line in the config file that is not there by default. Adding it will allow you to list a few variants of the domain name, for example without the www in the front. The rest of the lines in this section are not required to set up a virtual host. However, it is still helpful to know what they do. Step Five—Restart Apache We’ve made a lot of the changes to the configuration. However, they will not take effect until Apache is restarted. First stop all apache processes: sudo apachectl -k stop Then start up apache once again. sudo /etc/init.d/httpd start You may see the following error: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName The message is just a warning, and you will be able to access your virtual host without any further issues. Administrations Configurations (Linux) ApacheApache2CentOS