Shared WebServer (Apache howto)

Challenge:

Using the same web server to serve more than a single website, the server has 1 IP and you have multiple URLs that you want to each map to a different document root.

 

Solution:

Virutal Hosts, apache enables you to interpret the get request and route it to the appropriate document route based on the URL in the get request, this is done through what they call the virtual host technology, Since I’m using Ubuntu setting it up was as simple as going to /etc/apache2 and editing ports.conf. By adding the following you tell apache that whenever it gets a request with the mentioned ServerName map it to the mentioned Document route, if the URL didn’t match any of the mentioned servers route it to the first document route (/www/example1) in this example.

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here

</VirtualHost>

Once this is done a quick /etc/init.d/apache2 reload is required to refresh the settings, and then you can test it. If you receive an error message while reloading, make sure that you created the document routes mentioned in the ports.conf, and chowned them to www-data.www-data.

To test it go to /etc/hosts and add new entries that match the servers mentioned in the rules

127.0.0.1       www.example1.com
127.0.0.1       www.example2.org

Open your web browser and try it out (preferably by placing an obvious index file at each document route and seeing if you get it).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s