How to Create a VirtualHost in Apache 2.2 on CentOS 6.7

 

1. Confirm Apache 2.2 is correctly installed and running

Type in the following command to verify your Apache version:

 
httpd -v

 

Output should look like:

 
Server version: Apache/2.2.15 (Unix)
Server built: Aug 24 2015 17:52:49

 

Now let’s confirm Apache is running:

 
service httpd status

 

Output should look like:

 
httpd (pid 26102) is running...

 

Perfect!

 

2. Configure VirtualHost container

Apache stores the configuration files not directly related to the core in “/etc/httpd/conf.d”. Any time you create a VirtualHost, you’ll want to make sure you put it in this directory. It’s not a requirement, but it keeps your system looking nice and tidy.

First, we need to configure the httpd.conf file for VirtualHost’s.

 
nano /etc/httpd/conf/httpd.conf

 

Scroll to the bottom of the configuration file (press and hold CTRL-V) and add the following line:

 
NameVirtualHost *:80

 

Now type CTRL-O to save, and CTRL-X to exit

Next, let’s create the actual VirtualHost file

 
nano /etc/httpd/conf.d/yourdomain.com.conf

 

You’re now going to have to copy/paste the following into that file. (NOTE: MAKE SURE YOU CHANGE YOURDOMAIN.COM TO YOUR ACTUAL DOMAIN!!)

 
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
DocumentRoot /var/www/virtual/yourdomain.com/public_html
ServerName www.yourdomain.com
ServerAlias yourdomain.com
ErrorLog /var/www/virtual/yourdomain.com/logs/error_log
CustomLog /var/www/virtual/yourdomain.com/logs/access_log combined
<Directory "/var/www/virtual/yourdomain.com">
Options -Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

 

Type CTRL-O to save, and CTRL-X to close the file.

At this point, I need to re-iterate that this guide is based on the assumption you followed my previous guide. If you simply copy/paste everything, you probably won’t have a working web server.

Create the DocumentRoot and Log Directory we specified in the VirtualHost configuration file:

 
mkdir -p /var/www/virtual/yourdomain.com/public_html
mkdir -p /var/www/virtual/yourdomain.com/logs
chown -R apache:apache /var/www/virtual/
chmod -R 755 /var/www/virtual/

 

3. Test VirtualHost and confirm everything is working

Now, let’s test out your VirtualHost, and make sure everything is working correctly.

Move into your VirtualHost’s DocumentRoot. This is where you’ll store all of your web content:

 
cd /var/www/virtual/yourdomain.com/public_html

 

Create an index file so we can test functionality:

 
nano index.html

 

Copy and paste the following into the file:

 
<h3>Hello World!</h3>

Type CTRL-O to save, and CTRL-X to close the file.

 

Now restart Apache:

 
service httpd restart

 

Should produce the following output:

 
Stopping httpd: [ OK ]
Starting httpd: [ OK ]

 

If you’ve already updated DNS at your registrar and pointed yourdomain.com to your server’s IP address, you should now be able to open up a browser, and type in the URL yourdomain.com. It should load up the “Hello World!” index file that we created.

If you haven’t updated DNS yet, but want to check if everything is working, we can do the following:

http://your-server-ip

That about wrap this tutorial up! Thanks for reading, hopefully this helps you get Apache 2.2 and VirtualHosts up and running.

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Adding a Public IPv6 Address to a Linux Server (CentOS 6)

Adding a Public IPv6 Address to a Linux Server (CentOS 6) To configure an additional public...

Install LAMP Server (Apache, MySQL, PHP) On RHEL, CentOS, Scientific Linux 6.5/6.4

LAMP is a combination of operating system and open-source software stack. The acronym LAMP is...

Install EPEL Repository On RHEL / CentOS / Scientific linux 6.x, 5.x

EPEL stands for Extra Packages for Enterprise Linux. It is a Fedora Special Interest Group that...

Install REMI Repository On RHEL, CentOS, Scientific Linux 7/6.x/5.x And Fedora

This tutorial describes how to add REMI repository which is created and maintained by a French...