Categories: ApachecentOs 7How To

How to set up Apache Virtual Hosts on CentOS 6

To set up Apache Virtual Hosts on CentOS 6

                               Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site. There is no limit to the number of virtual hosts that can be added to a VPS.

 

 

 

To Set Up

In this article you can see how to set up in the Initial Server Setup in steps 3 and 4. Furthermore, if I reference the user in a step, I’ll use the name www. You can implement whatever username suits you.

Additionally, you need to have apache already installed and running on your virtual server If this is not the case,

  • Then Download it with this command:

sudo yum install httpd

 

 

Step 1 : To 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/Hostripples.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 2 : To 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/hostripples.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

You are all done with permissions.

That’s it!

 

 

Step 3 : To Create the Page

 

  • Create a new file called index.html within our configurations directory.

sudo vi /var/www/hostripples.com/public_html/index.html

  • 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.hostripples.com</title>
</head>
<body>
         <h1>Success: You Have Set Up a Virtual Host</h1>
     </body>
</html>

  • Save and Exit.

That’s it!

 

 

Step 4 : To Turn on Virtual Hosts

 

  • 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@hostripples.com
DocumentRoot /var/www/hostripples.com/public_html
ServerName www.hostripples.com
ServerAlias hostripples.com
ErrorLog /var/www/hostripples.com/error.log
CustomLog /var/www/hostripples.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.

Uncomment (remove the number sign) NameVirtualHost without making any changes. The star means that any IP address going through port 80 will be a virtual host. As your system probably only has one IP address this is not an issue—however, if you prefer, you can replace the star with your IP address.
You can leave the rest of the number marks in place until you reach the line <VirtualHost *:80> . Uncomment everything from there through <VirtualHost>.
Leave <VirtualHost *:80> as is—its details must match with those in the NameVirtual Host section. If you replaced the star with your IP address in that section, be sure to do the same here.
Document Root is key! For this section, write in the extension of the new directory created in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.
Server Name is another important piece of information, containing the virtual host’s domain name (eg. www.hostripples.com). Make sure that you spell the domain out in full; we will put in any alternate possibilities in the next line.
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.

Server admin asks for the webmaster’s email.
The Error Logs and Custom Logs keep track of any issues with the server. The error log covers issues that arise while maintaining the server, and the custom log tracks server requests. You can set up a custom location for these processes.
Make sure that <VirtualHost> is uncommented; then save and exit.

 

 

Step 5 : To 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

  • 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.

 

 

Optional Step 6 : To Setting Up the Local Hosts

 

If you have pointed your domain name to your virtual private server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone. For this step, make sure you are on the computer itself, not your droplet.

To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use an actual domain name to test the virtual hosts.

  • If you are on a Mac or Linux, access the root user (su) on the computer and open up your hosts file:

nano /etc/hosts

If you are on a Windows Computer, you can find the directions to alter the host file on the Microsoft site

You can add the local hosts details to this file, as seen in the example below. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost

#Virtual Hosts
Sever IP www.hostripples.com

However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion.

 

 

Step 7 : RESULTS: See Your Virtual Host in Action

 

Once you have finished setting up your virtual host, you can see how it looks online. Type your ip address into the browser.

It should look somewhat similar to my handy screenshot

Good Job!

 

Adding More Virtual Hosts

 

To create additional virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate new domain name each time.

  • Copy and paste the new Virtual Host information into the Apache Config file, as shown below:

<VirtualHost *:80>
ServerAdmin webmaster@hostripples.com
DocumentRoot /var/www/hostripples.com/public_html
ServerName www.hostripples.com
ServerAlias hostripples.com
ErrorLog /etc/var/www/hostripples.com/error.log
CustomLog /var/www/hostripples.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@hostripple.org
DocumentRoot /var/www/hostripple.org/public_html
ServerName www.hostripple.org
ServerAlias hostripple.org
ErrorLog /var/www/hostripple.org/error.log
CustomLog /var/www/hostripple.orgrequests.log
</VirtualHost>

 

That’s it! 🙂


Vishwajit Kale
Vishwajit Kale blazed onto the digital marketing scene back in 2015 and is the digital marketing strategist of Hostripples, a company that aims to provide affordable web hosting solutions. Vishwajit is experienced in digital and content marketing along with SEO. He's fond of writing technology blogs, traveling and reading.

Recent Posts

The Ultimate Showdown: Linux vs Windows for VPS Hosting

As the demand for virtual private servers (VPS) continues to grow, businesses and individuals are faced with a crucial decision:…

2 weeks ago

Questions to Ask Your Web Hosting Support Team

Web hosting is a large industry, as many other factors help any web hosting provider to form a company. The…

2 weeks ago

How to Secure Your WordPress Site in 2025

Welcome to the complete guide to WordPress security best practices in 2024. As technology evolves rapidly, implementing strong security measures…

3 weeks ago

Unlocking the Secrets of Hosting: Essential Questions to Ask Hostripples

Hey, wanted to learn about web hosting? Or do you want to start a new website and need hosting? Questions…

3 weeks ago

DDoS Attacks: What You Need to Know for Protection

In today's digital world, the threat of DDoS attacks has become increasingly prevalent. These types of attacks have the power…

1 month ago