<h1>Adding Virtual Hosts to Ubuntu Apache</h1>

                                        <div>
                                                <span>Posted on</span> <a href="http://brucewampler.wordpress.com/2009/02/28/adding-virtual-hosts-to-ubuntu-apache/" title="9:27 pm" rel="bookmark" target="_blank"><span>February 28, 2009</span></a> <span>by</span> <span><a href="http://brucewampler.wordpress.com/author/brucewampler/" title="View all posts by brucewampler" target="_blank">brucewampler</a></span>                                      </div>



                                        
                                                <p>Now that I have Ubuntu installed and the Apache server working,
 I need to configure some Virtual Hosts. I have several names registered
 at <a href="http://dyndns.com/" target="_blank">dyndns.com</a> that all point to the same IP. Right after you get Apache
 going, it defaults to serving from a default location, in this case <code>/var/www</code>. Any http requests coming into the box are all routed to that directory.</p>
<p>But Apache can support multiple virtual hosts. So if I route say 
<a href="http://example1.dnsalias.com/" target="_blank">example1.dnsalias.com</a> and <a href="http://example2.dynalias.com/" target="_blank">example2.dynalias.com</a> to my IP via <a href="http://dyndns.com/" target="_blank">dyndns.com</a>,
 I want them to be each served from their own directory. The default <code>/var/www</code>
 has system ownership and would need to be changed. So I’m going to 
create a directory for each site under my user directory. So I’ll create
 <code>/home/bruce/sites/example1</code> and <code>/home/bruce/sites/example2</code> to host the files for each site.</p>
<p>Now we need to make Apache use them. In the default configuration, 
Apache is already set up to support multiple virtual Hosts. In order to 
get the new sites working, we need to create a couple of configuration 
files in the <code>/etc/apache2/sites-available</code> directory, then enable them, and then restart Apache.</p>
<p>If you look in the <code>sites-available</code>, you’ll find a file 
called default. This represents the default Apache serving 
configuration. Copy this file twice to serve as a basis for the new 
configuration files. <code>sudo cp default example1</code> and <code>sudo cp default example2</code>. You need to make two or three changes to <strong>each</strong> of these files, and add one line. There’s a line in the file tagged <code>ServerAdmin</code>. If you want, you can make the value a real e-mail address. There are also two lines labeled <code>Document Root</code> and another in a <code>%lt;Directory> statement</code> that have the path to the directory being served – in this case, it should be <code>/var/www/</code>. Change both of those to the directory that will contain the new site files – <code>/home/bruce/sites/example1/</code> and again with <code>example2</code>.</p>


<p>Almost done. Now after the <code>ServerAdmin</code> line add a new line:<br>
<code>ServerName <a href="http://example1.dynalias.com/" target="_blank">example1.dynalias.com</a></code> and again with <code>example2</code> file. Use whatever actual domain names you’ve registered with <a href="http://dyndns.com/" target="_blank">dyndns.com</a> or other dynamic name server you’re using.</p>


<p>So now in addition to the <b><code>/etc/apache2/sites-available/default</code></b> file, you’ll have two more files – <code>example1</code> and <code>example2</code>, which have been modified to point to the actual hosting directories, and with the appropriate domain names.</p>


<p>Now we have to tell Apache to serve the new domains. Ubuntu has a 
couple of convenience commands to enable and disable sites. So, to 
enable the new sites, enter <code>sudo a2ensite example1</code> and again <code>sudo a2ensite example2</code>. These commands copy the configuration files you created into the <code>/etc/apache2/sites-enabled</code> directory. We’re almost there.</p>


<p>Finally, you have to restart Apache2 so that it reconfigures to serve the new sites. Simply enter <code>sudo /etc/init.d/apache2 reload</code>.</p>
<p>Be sure there’s an index.html file with some content in each of the<br>
<code>/home/username/sites/example1</code> and <code>example2</code> directories. Now if you enter <code><a href="http://example1.dnsalias.com/" target="_blank">http://example1.dnsalias.com</a></code> to a browser, you should see the contents of the appropriate file.</p>


<p>I hope I’ve made these steps pretty clear. It really isn’t that hard.</p>
<ol><li>Create directories to host the content of a new virtual site.</li><li>Make a copy of the <code>/etc/apache2/sites-available/defaults</code> file with a site appropriate name.</li><li>Change the two <code>/var/www/</code> entries to the actual directory of the new site.</li>
<li>Add the <code>ServerName</code> line with the real domain name you will be hosting.</li><li>Use <code>a2ensite</code> to enable the new site, and finally reload Apache2 so it knows about the new site</li></ol>One point to add though – after making all these changes  you will <b>need to add <a href="http://example1.com/" target="_blank">example1.com</a> to /etc/hosts.conf</b><br>
<br><div class="gmail_quote">2011/3/29 Alexandros Georgopoulos <span dir="ltr"><<a href="mailto:alexander_g16@hotmail.com">alexander_g16@hotmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
παμε στην επομενη ερωτηση τωρα.... :P :P<br>
πως μπορω να κανω το <a href="http://test.dyndns.org" target="_blank">test.dyndns.org</a> να ανοιγει τον φακελο test και να μην διαβαζει το κεντρικο /www/<br>
<br>
> Date: Tue, 29 Mar 2011 15:12:51 +0300<br>
> From: <a href="mailto:platsakos@gmail.com">platsakos@gmail.com</a><br>
<div class="im">> To: <a href="mailto:ubuntu-gr@lists.ubuntu.com">ubuntu-gr@lists.ubuntu.com</a><br>
> Subject: Re: erwthseis gia server<br>
><br>
</div><div><div></div><div class="h5">> Ας ελπισουμε οτι δεν ειναι production το μηχανημα :)<br>
> On 03/29/2011 03:03 PM, Alexandros Georgopoulos wrote:<br>
> > δούλεψε!! :) ναι κατι γινοταν με το σκριπτ λογικά. τασβησα ολα, ξανα ανεβασα τα αρχεια εδωσα permissions στα αρχεια που ζητουσε το σκριπτ κ δουλεψε κανονικα. :)<br>
> ><br>
> > σασ ευχαριστω παρα πολυ όλους για την υποστιριξη παιδια και σιγουρα κατι θα προκυψει πάλι.... :P :)<br>
><br>
><br>
> --<br>
> Ubuntu-gr mailing list<br>
> <a href="mailto:Ubuntu-gr@lists.ubuntu.com">Ubuntu-gr@lists.ubuntu.com</a><br>
><br>
> If you do not want to receive any more messages from the ubuntu-gr mailing list, please follow this link and choose unsubscribe:<br>
> <a href="https://lists.ubuntu.com/mailman/listinfo/ubuntu-gr" target="_blank">https://lists.ubuntu.com/mailman/listinfo/ubuntu-gr</a><br>
<br>
</div></div><div class="im">-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
</div>URL: <<a href="https://lists.ubuntu.com/archives/ubuntu-gr/attachments/20110329/1115bfee/attachment.html" target="_blank">https://lists.ubuntu.com/archives/ubuntu-gr/attachments/20110329/1115bfee/attachment.html</a>><br>

<div><div></div><div class="h5">--<br>
Ubuntu-gr mailing list<br>
<a href="mailto:Ubuntu-gr@lists.ubuntu.com">Ubuntu-gr@lists.ubuntu.com</a><br>
<br>
If you do not want to receive any more messages from the ubuntu-gr mailing list, please follow this link and choose unsubscribe:<br>
<a href="https://lists.ubuntu.com/mailman/listinfo/ubuntu-gr" target="_blank">https://lists.ubuntu.com/mailman/listinfo/ubuntu-gr</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Παναγιώτης Θεοδωρόπουλος<br>Panagiotis Theodoropoulos<br><<a href="mailto:tpanagiotis@gmail.com">tpanagiotis@gmail.com</a>><br>