Apache Virtual Host Configuration

Apache virtual HostWhat is an Apache Virtual Host?

So for whatever reason I always find myself having to look up how one makes virtual hosts in Apache. For you that don’t know what a virtual host is, it’s basically being able to host multiple DNS names via a single IP on a web server. Rather important unless A) you’re crazy B) you happen to have a nice swath of static IP addresses.

For the long version head over to the Apache site. Apache Virtual Host Documentation

The Short Virtual Host Configuration

Go ahead and edit your httpd.conf file with your favorite editor. Mine is VIM but I’m odd.

Where is my httpd.conf?

Well assuming you’re running Linux and in the case of RHEL distros it’s usually around:

[important]/etc/httpd/conf/httpd.conf[/important]

I like to put my virtual host configurations near the bottom that way they are easy to find and keep track of.

[important]NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin admin@your-website.com
DocumentRoot /var/www/html/Your-Website-Directory/
<Directory /var/www/html/tYour-Website-Directory/>
Options FollowSymLinks
AllowOverride All
</Directory>
ServerName your-website.com
ServerAlias www.your-website.com
ErrorLog /var/log/httpd/your_website_error_log
CustomLog /var/log/httpd/your_website_access_log common
</VirtualHost>[/important]

The above configuration will do a couple of things.

  1. It will make Apache respond to port 80 requests destined for your-website.com and www.your-website.com.
  2. Apache will serve up pages from /var/www/html/your-website-directory/.
  3. It will create a new error and access log at /var/log/httpd/ This is a nice to have if you’re going to host multiple websites and want an easy way to know what’s going on with each.
  4. The Directory statements will setup this virtual host for listening to .htaccess directives in the site should you need them. Ala what WordPress requires.

Issue a restart of httpd so that it reads in the new configuration, making sure it doesn’t complain. And that’s all there is to it.

 

For more details check out the following blog. http://www.jacmoe.dk/multiple-local-apache-virtual-hosts

Permanent link to this article: https://tripplehelix.net/apache-virtual-host-configuration/

Leave a Reply