skip to Main Content

In this article, I’ll be showing you how to create a VirtualHost container for your domain on Apache 2.2 running on CentOS 6.7. This article assumes you have already installed Apache 2.2. If you haven’t, you can following my guide on How to Install Apache, MySQL, and PHP on CentOS 6.7. That should get you up to speed!

 

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.

Sponsored Links

Don't be shy, leave a reply!