Set up apache with multiple virtualhosts - apache

I'm having problem getting virtual hosts to work as I want to. I've been searching for the last hours but it feels like I'm more lost than before.
So basically I want the following setup:
http://test.localhost => D:\xampp\htdocs\test\site
http://test.localhost/call => D:\xampp\htdocs\test\back\call.pl
And possibly add other stuff like /whatever that points to some other .pl script
Could anyone give me a hint? I must have missed something obvious...

For localhost subdomains you must add that subdomain to /etc/hosts
127.0.0.1 localhost test.localhost

Did you try editing your apache conf file?
Something like this
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin ramesh#thegeekstuff.com
DocumentRoot "/usr/local/apache2/docs/thegeekstuff"
ServerName thegeekstuff.com
ServerAlias www.thegeekstuff.com
ErrorLog "logs/thegeekstuff/error_log"
CustomLog "logs/thegeekstuff/access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin ramesh#top5freeware.com
DocumentRoot "/usr/local/apache2/docs/top5freeware"
ServerName top5freeware.com
ServerAlias www.top5freeware.com
ErrorLog "logs/top5freeware/error_log"
CustomLog "logs/top5freeware/access_log" common
</VirtualHost>

Related

Order to declare VirtualHost for domain and subdomains, Apache2, Ubuntu

I declared in my DNS, my domain, subdomain1 and subdomain2, and everything works.
Then I create my directories like this :
/var/www/domain.com/public_html/index.html /var/www/subdomain1.domain.com/public_html/index.html /var/www/subdomain2.domain.com/public_html/index.html
Then I create : /etc/apache2/sites-available/domain.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.subdomain1.domain.com
ServerAlias www.subdomain1.domain.com
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/subdomain1.domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain2.domain.com
ServerAlias www.subdomain2.domain.com
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/subdomain2.domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias www.domain.com
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I disable the default configuration: sudo a2dissite 000-default.conf
Then I activate each of the websites: sudo a2ensite domain.conf
I restart Apache 2: sudo systemctl restart apache2.service
The problem is that all links point to the first declared VirtualHost, even when I separate the VirtualHost declarations in separate .conf files, it will always be the first VirtualHost in the directory that will be opened for all DNS domains.
Apache will try to match the requested domain to one of the VirtualHost it knows about. When it cannot find a match, it will use the first one it read, top to bottom in the configurations. Hence here, your domains are not being recognized by Apache, and you get the first one all the time.
I modified your configuration a little bit:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.subdomain1.example.com
ServerAlias subdomain1.example.com
ServerAdmin webmaster#example.com
DocumentRoot /var/www/subdomain1.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/sub1_error.log
CustomLog ${APACHE_LOG_DIR}/sub1_access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain2.example.com
ServerAlias subdomain2.example.com
ServerAdmin webmaster#example.com
DocumentRoot /var/www/subdomain2.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/sub2_error.log
CustomLog ${APACHE_LOG_DIR}/sub2_access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster#example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
Details:
NameVirtualHost is only required if you run Apache 2.2. >2.4, it is automatic, no need to put it, it will work anyway.
ServerAlias is useful to define another domain name that applies to this VirtualHost. If you define ServerAlias == ServerName, it is useless.
Therefore, the ServerAlias are now configured with the domain names, without "www."
Split your log files. If you have 3 VH pointing to the same logs, it will be a real mess to debug. It makes it easier when split.
So with this configuration...
http://www.subdomain1.example.com and http://subdomain1.example.com will go to the 1st VH.
Similar for subdomain2
And http://www.example.com and http://example.com will use the third one.
This is based on your question and the comment where you say you tried example.com. In your configuration, example.com was not listed anywhere (i.e. www.example.com != example.com).

How to setup two virtual hosts in CentOS?

I have two domains I'm trying to host on the same IP. This is what I've been trying, but it doesn't work:
<VirtualHost host1.me:80>
ServerAdmin admin#menet.me
DocumentRoot /var/www/html/host1/
ServerName host1.me
ErrorLog logs/host1-error_log
CustomLog logs/host1-access_log common
</VirtualHost>
<VirtualHost host2.me:80>
ServerAdmin admin#menet.me
DocumentRoot /var/www/html/host2/
ServerName www.host2.me
ServerAlias host2.me
ErrorLog logs/host2-error_log
CustomLog logs/host2-access_log common
</VirtualHost>
What should I put in my httpd.conf?
if you are accessing the urls locally i.e. on server itself,
then u need to add their entries in /etc/hosts file.
eg: suppose ur server ip is 192.168.1.1, thn put following entry in hosts file:
192.168.1.1 host1.me host2.me www.host2.me
save the file...
and then check...
hope this helps :)

VirtualHost Configuration causing issues

I am quite new to VirtualHosts,
My use case is:
The domain mobc.in should be redirected to /var/www/html/mobc
And all other requests should be redirected to /var/www/html
The configuration that I am using it is
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/mobc
ServerName www.mobc.in
ServerAlias mobc.in *.mobc.in
ErrorLog logs/mobc.in-error_log
CustomLog logs/mobc.in-access_log common
</VirtualHost>
This is not serving the purpose and even mobc.in is being redirected to /var/www/html
Kindly help me.
Remove the fist virtualhost tags and contents. Just use the 2nd one and use the default documentroot in httpd.conf

How to enable multiple domains and or subdomains with apache httpd server

I'm trying to enable multiple domains in my environment Development, but am not succeeding the way I tried below, at which point I'm wrong?
I installed httpd and changed DocumentRoot in httpd.conf to:
C:/Webserver/www
*I changed the Windows hosts file to(in Italics would like to access):
If I switch to 127.0.0.1 api.rotadorock the address resolves to www/ but the right is www/rotadorock/api.
127.0.0.1 localhost
127.0.0.1 webserver
127.0.0.1/rotadorock/ecommerce rotadorock
127.0.0.1/rotadorock/api api.rotadorock
127.0.0.1/rotadorock/ecommerce ecommerce.rotadorock
127.0.0.1/rotadorock/mobile mobile.rotadorock
127.0.0.1/rotadorock/sistema sistema.rotadorock
127.0.0.1/rotadorock/social social.rotadorock
*Update(windows hosts file)
I removed the hosts file changes I mentioned above, because as #Volker Birk said are not necessary. But even so, still can not access as desire (api.rotadorock/ or localhost/api.rotadorock/ and should point to C:/Webserver/www/rotadorock/api). What could be wrong?
And finally changed httpd-vhost.conf for:
NameVirtualHost webserver:80
<Directory "C:/Webserver/www">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName localhost
DocumentRoot "c:/Webserver/www"
ServerAlias localhost
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName webserver
DocumentRoot "c:/Webserver/www"
ServerAlias webserver
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost rotadorock:80>
ServerName rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/ecommerce"
ServerAlias rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost api.rotadorock:80>
ServerName api.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/api"
ServerAlias api.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost ecommerce.rotadorock:80>
ServerName ecommerce.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/ecommerce"
ServerAlias ecommerce.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost mobile.rotadorock:80>
ServerName mobile.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/mobile"
ServerAlias mobile.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost sistema.rotadorock:80>
ServerName sistema.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/sistema"
ServerAlias sistema.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost social.rotadorock:80>
ServerName social.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/social"
ServerAlias social.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
You don't need the hosts file. Have a look into the documentation:
http://httpd.apache.org/docs/2.2/en/vhosts/name-based.html
Finally solved the problem. And I could just using the Windows hosts file and httpd-vhosts.conf httpd together.
Let me give an example of what was done to enable multiple subdomains accessing locally.
On Windows hosts file to add, for each domain and subdomain you want something like this:
127.0.0.1 api.rotadorock #my subdomain
127.0.0.1 rotadorock #my domain
And then the httpd-vhosts apache httpd:
# Accessing the API
<VirtualHost 127.0.0.1>
DocumentRoot "C:/Webserver/www/rotadorock/api"
ServerName api.rotadorock
ServerAlias ​​api.rotadorock
<Directory "C:/Webserver/www/rotadorock/api">
All Options Includes Indexes
</ Directory>
</VirtualHost>
# Accessing the domain
<VirtualHost 127.0.0.1>
DocumentRoot "C:/Webserver/www/rotadorock/"
ServerName rotadorock
ServerAlias ​​*.rotadorock
<Directory "C:/Webserver/www/rotadorock/">
All Options Includes Indexes
</Directory>
</VirtualHost>
And then that way I could access api.rotadorock/ and rotadorock/ locally. I tried all ways without the hosts file. But just gotta use it. If someone can explain to me how it should have done so it would not need to use the hosts I would be grateful.

Why can I only have one virtual host?

My site on http://localhost is just fine. My first virtual host, as specified in httpd-vhosts.conf is just fine ... but when I add a second, just a variant with an extra letter tacked on, Apache doesn't seem to try to serve it.
I did restart Apache after editing the file (Xampp on Windows 7)
My httpd-vhosts.conf says
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "o:/abc"
ServerName abc
ErrorLog "logs/abc-error.log"
CustomLog "logs/abc-access.log" combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "o:/abc2"
ServerName abc2
ErrorLog "logs/abc2-error.log"
CustomLog "logs/abc2-access.log" combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "f:/htdocs"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
</VirtualHost>
When I access http://lcoalhost or http://abc, everything is fine. When I try to access http://abc2 (the local directory does exist) my browser tries to load http://www.abc2.com
What might I be doing wrong?
Did you specify abc2 in your hosts file?