Associate a domain name to a directory in Apache

Hamza picture Hamza · Jun 24, 2011 · Viewed 8.3k times · Source

I have an Ubuntu dedicated server, and I have 2 domain names. The first one is related to the directory '/var/www/' and the second one is too, I didn't know how to associate the second one to another directory like '/var/www/site2/' Can you help me ? Thank you !

Answer

Corey Henderson picture Corey Henderson · Jun 24, 2011

To host multiple domains on the same server with different directories of their own, you need to use the VirtualHost config directive. Inside each one you can specify their own set of configurations (by default the configuration file is stored at /etc/apache2/sites-enabled/000-default.conf):

NameVirtualHost *:80

<VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/site1
        <Directory /var/www/site1>
            Options -Indexes
        </Directory>
</VirtualHost>

<VirtualHost *:80>
        ServerName another-example.com
        DocumentRoot /var/www/site2
        <Directory /var/www/site2>
            Options +Indexes
        </Directory>
</VirtualHost>

The first one lives in /var/www/site1 and has directory indexing turned off. The other is in /var/www/site2 and has directory indexing turned on. You can specify pretty much most configs to be virtualhost specific - ie, custom logging, use of modules such as php or perl, and ServerAlias, among much more. See http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost for more details.