How to handle a main site plus multiple virtualhosts on one server

Lothar_Grimpsenbacher asked:

I have a standard Ubuntu install. Apache web root is /var/www.

I want to host my “main” site and a handful of others.

Normally I would put the files for mysite.com right in the web root. However, if I want to host multiple sites that seems like it could get messy. It seems like I would end up with a structure like:

My “main” site’s files:

/var/www/index.html
/var/www/images/
/var/www/js/

etc…

and then my virtual sites like:

/var/www/somesite/ #somesite's files in here
/var/www/foobar/ #foobar's files in here

How should I be organizing such a setup? Mixing files from the main site with directories for virtualhost sites seems wrong and messy. I thought about putting my “main” site into its own directory in the webroot (like “somesite” and “foobar” are above) but then people visiting my IP address would get the web root without any site files in it. Should I do this and then redirect anything to the main IP to the “main” site’s dir? Maybe with htaccess? Maybe in the apache configuration somewhere?

How would you approach this?


What I ended up doing (thanks to the suggestions below, all of which were helpful)…

Here is what the config file (/etc/apache2/sites-enabled/default) had originally:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
<Directory /var/www>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>

By changing it to:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

DocumentRoot /var/www/mysite/public
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
<Directory /var/www/mysite/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>

I was able to get visits to go to “mysite.com” (I have the public files served out of the public directory so I can have some system files up one level and not web accessible)

I then set up another site at next.mysite.com by creating a new config file in /etc/apache2/sites-enabled/nextconfig that looks like:

<VirtualHost *:80>
DocumentRoot /var/www/next
<Directory "/var/www/next">
allow from all
Options +Indexes
</Directory>
ServerName next.mysite.com
</VirtualHost>

My answer:


Put the “main” site in a subdirectory of /var/www as well, and change your Apache configuration to reflect its new location.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.