Why are all virtual hosts pointing to the first virtual host? - apache

After reinstalling a development environment on my windows 7 laptop all subdomains are pointing to the first virtual host in the .conf file!?
hosts (c:/windows/system32/drivers/etc/hosts)
127.0.0.1 localhost
127.0.0.1 admin.localhost
127.0.0.1 api.localhost
127.0.0.1 cdn.localhost
127.0.0.1 demo.localhost
127.0.0.1 secure.localhost
httpd.conf
<VirtualHost *:80>
ServerName admin.localhost
DocumentRoot c:/wamp/www/admin
</VirtualHost>
<VirtualHost *:80>
ServerName api.localhost
DocumentRoot c:/wamp/www/api
</VirtualHost>
<VirtualHost *:80>
ServerName cdn.localhost
DocumentRoot c:/wamp/www/cdn
</VirtualHost>
<VirtualHost *:80>
ServerName secure.localhost
DocumentRoot c:/wamp/www/secure
</VirtualHost>
All subdomains are pointing to the first virtual host admin.localhost..!? When requesting eg. http://cdn.localhost the dir c:/wamp/www/admin is showed?
I don't know what I'm doing wrong in the conf? This setup worked before reinstallation... hmmm

This line should be uncommented NameVirtualHost *:80
Restart apache after making the changes

Related

Multiple Virtual host with Port 443 pointing to wrong directory

I have created two virtual host files on ubuntu apache2
www.example1.com.conf
www.example2.com.conf
which is working fine with port 80. without https.
I have added following code for 443
VirtualHost *:443>
ServerAdmin info#example1
ServerName example1.com
DocumentRoot /var/www/www.example1.com/
#SSLEngine on
#SSLCertificateFile /etc/ssl/example1.crt
#SSLCertificateKeyFile /etc/ssl/example1.key
#SSLCertificateChainFile /etc/ssl/SectigoRSADomainValidationSecureServerCA.crt
</VirtualHost>
and
<VirtualHost *:443>
ServerAdmin info#example2
ServerName example2.com
DocumentRoot /var/www/www.example2.com/
</VirtualHost>
Now, when i am opening https://www.example2.com/, i am seeing content of example1.com.
but if i do http://www.example2.com/ then content is ok.
Can you please suggest what will be the issue ?

Virtual Host With SSL Support on OS X Mavericks

I have a few virtual hosts setup on my local dev machine running Apache/2.2.24 on OS X 10.9 (Mavericks).
My http-vhosts.conf file (that is configured to load through httpd.conf looks like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite.com
ServerName testsite.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite2.com
ServerName testsite2.dev
</VirtualHost>
I have also configured my /etc/vhosts file to contain this line:
127.0.0.1 testsite2.dev
I want to be able to use the testsite2.dev over SSL (https). I have tried multiple configurations of the vhosts config file with no luck.
With this current configuration, going to http://testsite2.dev pulls up the page I expect while https://testsite2.dev points to the apache home page at /Library/WebServer/Documents/index.html.en
I have tried the following configuration, and multiple others, that do not work:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite.com
ServerName testsite.dev
</VirtualHost>
<VirtualHost *:80 *:443>
DocumentRoot "/Users/me/Sites/testsite2.com
ServerName testsite2.dev
</VirtualHost>
Is it possible to have a virtual host listen on port 80 and port 443 on a local machine?
I figured out how to do this. I was simply missing a few directives to show where my certificate and key are located. Here's what I added:
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /private/etc/apache2/ssl/server.crt
SSLCertificateKeyFile /private/etc/apache2/ssl/server.key
DocumentRoot "/Users/me/Sites/testsite2.com"
ServerName testsite2.dev
</VirtualHost>
Shown in context, my http-vhosts.conf file looked like this:
Listen *:443
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite.com
ServerName testsite.dev
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /private/etc/apache2/ssl/server.crt
SSLCertificateKeyFile /private/etc/apache2/ssl/server.key
DocumentRoot "/Users/me/Sites/testsite2.com"
ServerName testsite2.dev
</VirtualHost>
If you don't have a certificate and key installed, you can create your own by following a tutorial like this one:
http://www.cfdad.com/2012/12/12/creating-a-self-signed-ssl-cert-for-mac-osx-mountain-lion-apache/
It should work on both Mountain Lion and Mavericks.

Apache virtual host for browsing with IP address

I'm using virtual host. but I want that when user browses IP address, my server will serve resource in local host. The content of my virtual host config:
NameVirtualHost *:80
# for main web
<VirtualHost *:80>
DocumentRoot /Library/WebServer/Documents
ServerName localhost
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
<VirtualHost 192.168.1.6:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
#for other webs
...
When user type 192.168.1.6 in the browser, the resource in /Library/WebServer/Documents will be return. But I don't want to designate 192.168.1.6 explicitly, I want something like ..*.*
I changed
...
<VirtualHost 192.168.1.6:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
...
to
...
<VirtualHost *.*.*.*:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
...
but it don't work.
What should I change? Is this even possible?
I am personally using this one for IP Vhost
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#steelbrain.com.pk
DocumentRoot /var/www/ipbased/
ServerName 192.168.1.1
ServerAlias 192.168.1.1 192.168.1.2
</VirtualHost>

Multiple domains on one Apache server(WAMP)

I have got one IP address on my sv, and I want to set up two domains on one Apache Server with another DocumentRoot. Here is wat I got in httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "C:\wamp\www\mail"
ServerName mail.A.pl
</VirtualHost>
<VirtualHost *:80>
ServerName B.eu
DocumentRoot "c:\wamp\www\B"
</VirtualHost>
<VirtualHost *:80>
ServerName A.pl
DocumentRoot "c:\wamp\www"
</VirtualHost>
Even when I try to load page: http://www.A.pl or www.B.eu or B.eu or A.pl it loads mail.A.pl.
What am I doing wrong?
EDITED
Here is new httpd-vhost, now it loads correct pages, but f.e. for A.pl doesn't show any images, and on mail.A.pl doesn't want to send POST data:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\wamp\www\mail"
ServerAlias mail.A.pl
ServerName mail.A.pl
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName B.eu
ServerAlias *.B.eu
DocumentRoot "c:\wamp\www\B"
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName A.pl
ServerAlias A.pl
DocumentRoot "c:\wamp\www"
</VirtualHost>
EDIT2
Thank you both for help.
I just switched first VirtualHost with last and it mistery started working :O
There are a few things that you need to have I think.
1) Uncomment the Virtual hosts line in Apache's config file so it includes httpd-vhosts.conf. On OSX that is:
# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
2) Put the necessary lines in your hosts file ( http://en.wikipedia.org/wiki/Hosts_(file) ):
127.0.0.1 mail.A.pl
127.0.0.1 A.pl
127.0.0.1 B.pl
127.0.0.1 every.A.pl
127.0.0.1 subdomain.A.pl
127.0.0.1 needs-a-line.A.pl
The ServerAlias can go actually, you don't need that for subdomains, only for similar "main" domains.
Your httpd-vhosts.conf file is fine.
Why the images and POSTs are not working I don't know. That doesn't have anything to do with this host file. Perhaps with another .htaccess file.
You need to map NameVirtualHost to the IP address 127.0.0.1 for mapping multiple virtual host.
For example:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot 'C:\wamp\www'
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName myclient.local
DocumentRoot 'C:\wamp\www\ClientsMyClient'
</VirtualHost>
Have a look at this tutorial.

Setting up subdomain for local development

I have XAMPP installed for local development and I'd like to create a sub domain for each project. In my apache vhosts config I've put this:
<VirtualHost localhost:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
ServerAdmin admin#localhost
</VirtualHost>
<VirtualHost nexus.localhost:80>
DocumentRoot C:/xampp/htdocs/nexus/
ServerName nexus.localhost
ServerAdmin admin#nexus.localhost
</VirtualHost>
And in my Windows hosts file:
# development
127.0.0.1 localhost
127.0.0.1 nexus.localhost
localhost works as normal. As in, if I go to http://localhost/project_name everything works fine. However, if I navigate to http://nexus.localhost/ I just get Object not found! errors.
What could be wrong here? Thank you.
The documentation http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost says that the <VirtualHost> directive should contain the IP address, so try this instead:
<VirtualHost 127.0.0.1:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
ServerAdmin admin#localhost
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot C:/xampp/htdocs/nexus/
ServerName nexus.localhost
ServerAdmin admin#nexus.localhost
</VirtualHost>