Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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>
share|improve this question

3 Answers 3

up vote 2 down vote accepted

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

share|improve this answer
    
If I do this, how do I handle visits to the document root at /var/www/? Assume my IP is 234.56.78.90. Since a visit to mysite.com will use what is in /var/www/mysite/ but the document root is /var/www/ what happens when someone visits my IP address directly? Do I need to use htaccess to redirect such visits to /var/www/mysite? –  Lothar_Grimpsenbacher Jul 28 '12 at 5:34
    
If you don't have a VirtualHost or the main server configured to serve documents from /var/www then nobody will ever see it. –  Michael Hampton Jul 28 '12 at 5:40

You do not have to mix it. Not one bit.

Place the virtual host directory in /var/vh1 /var/vh2 and so on. Its not necesary to place all of them in /var/www

An example:

Listen 80

# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver

# This is the other address
NameVirtualHost 172.20.30.50

<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example.com

# Other directives here ...

</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example.org

# Other directives here ...

</VirtualHost>

I took it from Apache page itself. So its pretty reliable.

share|improve this answer
1  
You could also look at VirtualDocumentRoot, which is a more flexible variant of DocumentRoot. It allows variables, so you don't have to hardcode a VirtualHost entry for each domain. For example, VirtualDocumentRoot /var/www/%0 would allow you to simply create a new directory /var/www/www.example3.com and it would be served by Apache. Very useful if you often change or create new sites. –  Martijn Heemels Jul 28 '12 at 3:50
1  
Or you could split it up like VirtualDocumentRoot /var/www/%-2.0.%-1 which takes the penultimate and ultimate parts of the domain name, effectively ignoring the subdomain for the path. You'd get /var/www/domain.com whether you came in via www.domain.com or domain.com. All depends on what you find useful. –  Martijn Heemels Jul 28 '12 at 4:07

I personally think you should just make a folder /var/Www/main for your own site. It's easier to keep track of then.

Just create a virtualhost for your mainsite and point it to the new folder.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.