Apache Config Steps – Run multiple domains on same ip

|
| By Webner

Suppose you want to point abc.mydomain.com and mydomain.com to same machine/ip, but to different applications. There are multiple ways to achieve this (basically with same config settings but in different files). I am giving one way here that worked for me:

* Go to /etc/apache2/sites-available
* open a file abc.mydomain.com.conf
* copy this to abc.mydomain.com.conf, make changes to folder path domain name etc. and Save the file

<VirtualHost *:80>

DocumentRoot /var/www/abcappfolder/
ServerAdmin admin@mydomain.com
ServerName abc.mydomain.com
ServerAlias http://www.abc.mydomain.com

Options FollowSymLinks
AllowOverride None

#we want specific log file for this server
CustomLog /var/log/apache2/abc.com-access.log combined
ErrorLog /var/log/apache2/abc.com-error.log

</VirtualHost>

* Now create another file mydomain.com.conf
* copy this to mydomain.com.conf, make changes to folder path domain name etc. and Save the file. Note that here I am assuming mydomain.com will actually redirect to an app server (like tomcat or jboss) running on port 8080, that’s why you see ProxyPass statements:


<VirtualHost *:80>

DocumentRoot /var/www/mysecondapp/
ServerAdmin admin@mydomain.com
ServerName mydomain.com
ServerAlias http://www.mydomain.com

Options FollowSymLinks
AllowOverride None

ProxyPass / [code]127.0.0.1:8080[/code]
ProxyPassReverse [code]/ 127.0.0.1:8080[/code] ProxyPreserveHost on

#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/mydomain.com-access.log combined
ErrorLog /var/log/apache2/mydomain.com-error.log

# if your machine’s name is also mydomain.com
# then you need to use config settings below
# otherwise site will NOT be accessible with mydomain.com
# (although it will be accessble as http://www.mydomain.com)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

</VirtualHost>

* on command line run these commands to enable these 2 configs:

a2ensite abc.mydomain.com.conf
a2ensite mydomain.com.conf

This creates symlinks to the 2 config files in /etc/apache2/sites-enabled folder
You can create the same symlinks manually also like this:

cd /etc/apache2/sites-enabled
ln -s /etc/apache2/sites-available/abc.mydomain.com.conf abc.mydomain.com.conf
ln -s /etc/apache2/sites-available/mydomain.com.conf mydomain.com.conf

* Following step is required only if your machine’s name is also mydomain.com(that means you sftp or ssh to your machine using mydomain.com). In this case I was not able to access my site using mydomain.com in browser. Hence I did this which worked, there may be a different way to solve this problem though:

remove symlink to ln -s /etc/apache2/sites-available/default file from /etc/apache2/sites-enabled folder. You can use rm command or you can run this command

a2dissite default

You do not need to do this for sub-domains (like abc.mydomain.com)

* Restart apache (/etc/conf.d/apache restart)
* Your sites should be accessible now

Note : If it does not work make sure you do not have ProxyPass statements inside httpd.conf or a conf file in /etc/apache2/conf.d to redirect URL

Leave a Reply

Your email address will not be published. Required fields are marked *