Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate a domain name to a directory in Apache

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 !

like image 484
Hamza Avatar asked Jun 23 '11 22:06

Hamza


1 Answers

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.

like image 143
Corey Henderson Avatar answered Sep 28 '22 05:09

Corey Henderson