<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>edsalisbury.net &#187; Apache</title>
	<atom:link href="http://edsalisbury.net/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://edsalisbury.net</link>
	<description>your guide to user-friendly entertainment</description>
	<lastBuildDate>Wed, 28 Mar 2012 02:07:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to Set Up Virtual Web Hosting with Apache</title>
		<link>http://edsalisbury.net/how-to-set-up-virtual-web-hosting-with-apache/</link>
		<comments>http://edsalisbury.net/how-to-set-up-virtual-web-hosting-with-apache/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 15:58:50 +0000</pubDate>
		<dc:creator>Ed</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.edsalisbury.net/?p=419</guid>
		<description><![CDATA[<a href="http://edsalisbury.net/how-to-set-up-virtual-web-hosting-with-apache/" title="How to Set Up Virtual Web Hosting with Apache"></a>Up until a couple of years ago, I used shared web hosting for serving up my various sites. I went through several of them because for whatever reason, they turned out to not be what I wanted. I finally came &#8230;<p class="read-more"><a href="http://edsalisbury.net/how-to-set-up-virtual-web-hosting-with-apache/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://edsalisbury.net/how-to-set-up-virtual-web-hosting-with-apache/" title="How to Set Up Virtual Web Hosting with Apache"></a><p><img src="http://www.edsalisbury.net/wp-content/uploads/2009/08/apache_logo.png" alt="Apache" title="Apache" width="150" height="149" class="alignright size-full wp-image-430" /><br />
Up until a couple of years ago, I used shared web hosting for serving up my various sites.  I went through several of them because for whatever reason, they turned out to not be what I wanted.  I finally came to the realization that I needed to get my own dedicated host.  I found a good provider (<a href="http://www.shareasale.com/r.cfm?B=106084&#038;U=369672&#038;M=15362">The Planet</a>), and started migrating over my websites (along with my friend Chris&#8217; sites).  Something I knew I would have to learn is how to set up Apache to be able to do virtual hosting.  In this guide, I&#8217;ll show you how I did it, and the script I created to make things easier.<br />
<span id="more-419"></span><br />
The first thing to do is to set up a logical directory structure &#8212; I hate putting web docs in some silly folder like /usr/local/apache2/htdocs &#8212; that&#8217;s always seemed like such a weird place to put things (I guess it goes against my feelings that anything in /usr shouldn&#8217;t be writable by users)   Since this is a webserver, I wanted something closer to the root, like /www/&lt;domain&gt;.  I also wanted to have a different directory structure and logging for each virtual host, I chose /www/logs/&lt;domain&gt;.</p>
<p>The next thing is to set up the apache config file to be able to include a separate file just for vhosts.  Add this section somewhere in your httpd.conf file:</p>
<pre>
# Virtual Hosts
NameVirtualHost *
Include etc/httpd-vhosts.conf
</pre>
<p>Now, create httpd-vhosts.conf in the apache config dir, and create a manual entry to make sure it works:</p>
<pre>
<VirtualHost *>
    ServerName      www.mydomain.com
    ServerAdmin     webmaster@mydomain.com
    ServerAlias     mydomain.com
    DocumentRoot    /www/mydomain.com/docs
    DirectoryIndex  index.html index.php
    AccessFileName  .htaccess
    HostnameLookups Off
    ErrorLog        /www/logs/mydomain.com/error.log
    LogLevel        warn
    CustomLog       /www/logs/mydomain.com/access.log combined
</VirtualHost>
</pre>
<p>(Be sure to change &#8220;mydomain.com&#8221; to your domain)</p>
<p>Create /www/&lt;domain.com&gt; and log files, as well as change ownerships properly:</p>
<pre>
# mkdir /www/logs/&lt;domain.com&gt;
# mkdir /www/&lt;domain.com&gt;
# chmod g+w /www/&lt;domain.com&gt;
# chown nobody:nobody /www/&lt;domain.com&gt;
# chown nobody:nobody /www/logs/&lt;domain.com&gt;
</pre>
<p>Next, create some sort of test file (like index.html) in the new doc directory.</p>
<p>When you&#8217;re ready, restart the webserver (usually just by running apachectl graceful)  With any luck, you should be seeing the webpage you created when you go do www.&lt;domain.com&gt;.  If not, look at the apache config for errors, and verify that the DNS entries are correct.</p>
<p>Now, as you were expecting, I have a script to be able to automate this.  It&#8217;s pretty simple, but handy.  I call it &#8220;add_vhost&#8221; and I put it into /usr/local/bin:</p>
<pre class="brush:perl; gutter: false; wrap-lines: true; ruler: false">
#!/usr/bin/perl -w
# AddVhost
# Add Virtual Host Configs for Apache
# by Ed Salisbury (ed@edsalisbury.net)
# http://www.edsalisbury.net
# (c)2009 Ed Salisbury, Some Rights Reserved
#
# License:
# Except where otherwise noted, this work is licensed under Creative Commons
#   Attribution ShareAlike 3.0.
#
# You are free:
#   * to Share -- to copy, distribute and transmit the work
#   * to Remix -- to adapt the work
#
# Under the following conditions:
#   * Attribution. You must attribute the work in the manner specified by the
#     author or licensor (but not in any way that suggests that they endorse
#     you or your use of the work).
#   * Share Alike. If you alter, transform, or build upon this work, you may
#     distribute the resulting work only under the same, similar or a
#     compatible license.
#   * For any reuse or distribution, you must make clear to others the license
#     terms of this work. The best way to do this is with a link to the
#     license's web page (http://creativecommons.org/licenses/by-sa/3.0/)
#   * Any of the above conditions can be waived if you get permission from the
#     copyright holder.
#   * Nothing in this license impairs or restricts the author's moral rights.

use strict;

my $DOCROOT = '/www';
my $LOGDIR = '/www/logs';
my $USER = 'nobody';
my $GROUP = 'nobody';
my $VHOST_CFG = &quot;/usr/local/apache2/etc/httpd-vhosts.conf&quot;;

my $domain = $ARGV[0];

if (!$domain)
{
    print &quot;Error: No domain specified!n&quot;;
    exit(1);
}
open (OUT, &quot;&gt;&gt; $VHOST_CFG&quot;);
print OUT &quot;n&quot;;
print OUT &quot;&lt;VirtualHost *&gt;n&quot;;
print OUT &quot;tServerName      www.$domainn&quot;;
print OUT &quot;tServerAdmin     webmaster@$domainn&quot;;
print OUT &quot;tServerAlias     $domainn&quot;;
print OUT &quot;tDocumentRoot    $DOCROOT/$domainn&quot;;
print OUT &quot;tDirectoryIndex  index.html index.phpn&quot;;
print OUT &quot;tAccessFileName  .htaccessn&quot;;
print OUT &quot;tHostnameLookups Offn&quot;;
print OUT &quot;tErrorLog        $LOGDIR/$domain/error.logn&quot;;
print OUT &quot;tLogLevel        warnn&quot;;
print OUT &quot;tCustomLog       $LOGDIR/$domain/access.log combinedn&quot;;
print OUT &quot;&lt;/VirtualHost&gt;n&quot;;
close (OUT);

system(&quot;mkdir $LOGDIR/$domain&quot;);
system(&quot;mkdir $DOCROOT/$domain&quot;);
system(&quot;chmod g+w $DOCROOT/$domain&quot;);
system(&quot;chown $USER:$GROUP $DOCROOT/$domain&quot;);
system(&quot;chown $USER:$GROUP $LOGDIR/$domain&quot;);
</pre>
<p>Be sure to change the config variables to suit your environment.  When you want to add a virtual host, simply run:</p>
<pre>
% sudo add_vhost domain.com
</pre>
<p>It will create the config and set everything up for you.  All you need to do then is to restart the webserver, and you&#8217;re good to go!</p>
]]></content:encoded>
			<wfw:commentRss>http://edsalisbury.net/how-to-set-up-virtual-web-hosting-with-apache/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

