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>
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:
I took it from Apache page itself. So its pretty reliable.
Put the "main" site in a subdirectory of
/var/www
as well, and change your Apache configuration to reflect its new location.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.