How to Setup Virtualhosts to Point Two Ports on Same Ip to Different Servernames

How do I setup VirtualHosts to point two ports on same IP to different ServerNames?

This is what you need to do.

    NameVirtualHost *:80

<VirtualHost *:80>
ServerName hist118.wcaleb.rice.edu
DocumentRoot /var/www/
RewriteEngine On
ProxyPreserveHost On
ProxyRequests Off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPassReverse / http://127.0.0.1:1848
RewriteRule ^(.*) http://127.0.0.1:1848$1 [P]

ErrorLog /var/log/apache2/error.log
LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

</VirtualHost>

Serving same content in same ip address with different domain names

You have to configure two virtual host with same DocumentRoot but different ServerNames

http://httpd.apache.org/docs/2.2/vhosts/examples.html

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

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

# Other directives here

</VirtualHost>

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

# Other directives here

</VirtualHost>

Apache VirtualHost : multiple sites on same IP

What you want to do is called Name-Based Virtual Hosting, you'll need

NameVirtualHost *:80

to enable it on port 80, and for each VirtualHost, you need to give the name(s):

<VirtualHost *:80>
ServerName blah2.com
ServerAlias www.blah2.com

DocumentRoot /var/www/site1
</VirtualHost>

Note that there are limitations on SSL/TLS when doing name-based virtual hosting, but it's a bit of a moot point since post-POODLE, people start to require TLS anyway, so ancient browsers are out of luck anyway.

As to the config files, it's very very useful to have two classes of config files: the ones with defaults that a package update will overwrite, and your local ones that it will not touch, or better even, a directory full of the former and a directory full of the latter. (Because additional packages might want to make configuration settings, they'll all install in the former place, and you should only ever change/override config in the second place.)

Different VirtualHosts with the same port

Add different ServerName directive in all virtual hosts:

<VirtualHost *:80>
ServerName dev.localhost
DocumentRoot /home/projects/smk/cms
ErrorLog /var/log/apache2/smk-cms-error.log
</VirtualHost>

<VirtualHost *:80>
ServerName my-project.localhost
DocumentRoot /home/projects/smk/deploy
ErrorLog /var/log/apache2/smk-deploy-error.log
</VirtualHost>

Don't forget to add host-entries for dev.localhost and my-project.localhost in /etc/hosts to 127.0.0.1 or whatever ip you want it to point to.

Multiple Virtual Hosts on Different Ports in WAMP

I suppose you have solved the issue. Anyway is good to share some nice information on how to set up multiple Virtual Hosts in Wamp. This is working for me:

http://www.kristengrote.com/blog/articles/how-to-set-up-virtual-hosts-using-wamp

In my case I am working with ports 8080 and 8181. 8080 is redirecting to a subfolder under c:\wamp\www\myfolder, while 8181 is redirecting to root c:\wamp\www.

To make 8181 work I had to edit httpd-vhosts.conf, hosts (in \drivers\etc folder) and httpd.conf.

In httpd.conf my Apache is listening:

Listen 8080
Listen 8181

also I uncommented:

Include conf/extra/httpd-vhosts.conf

my root is pointing to

DocumentRoot "c:/wamp/www/myfolder"

root directory is configured as:

<Directory "c:/wamp/www">
Options Indexes FollowSymLinks
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from ::1
Allow from localhost
</Directory>

and added:

<VirtualHost *:8181>
DocumentRoot "C:\wamp\www"
ServerName name-of-my-fake-server
</VirtualHost>

in httpd-vhosts.conf I have set:

NameVirtualHost *:8181

in hosts (c:\windows\system32\drivers\etc) I have added:

127.0.0.1       localhost
127.0.0.1 name-of-my-fake-server #My Test Site

Doing that I have now two ports working 8080 and 8181: so 8080 points to directory "c:\wamp\www\myfolder" and the other port 8181 points to my root folder "c:\wamp\www\"

Apache Virtual Hosts on different domain names AND ports

I managed to get it done, but I think it's more of a dirty hack than an actual solution. I made two more virtual hosts like so :

maindomain.com.trap.conf

<VirtualHost *:8080>
ServerAdmin webmaster@localhost
ServerName maindomain.com
ServerAlias www.maindomain.com maindomain.com
DocumentRoot "/var/www/html/maindomain"
<Directory /var/www/html/maindomain>
Require all denied
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The other one having the names and port switched.

By the way, I left <VirtualHost *:80> and <VirtualHost *:8080> in the first two .conf files I mentioned in my first post.



Related Topics



Leave a reply



Submit