Apache virtual host for browsing with IP address - apache

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>

Related

how to redirect browser to two different site with apache virtualHost?

I have a valid IP, e.g. x.x.x.x, and a domain, e.g. site.com which point to x.x.x.x.
First, I need to redirect any request from port 80 to 443 (security issue).
Second, for every request sent to my IP, I want apache to show "Hello it's
working" (/var/www/index.html) and everyone request to my domain to show the real site.
I tried this, but it did not work:
<VirtualHost x.x.x.x:433>
ServerName x.x.x.x
ServerAlias x.x.x.x
DocumentRoot /var/www/
DirectoryIndex index.html
Options -Indexes
</VirtualHost>
<VirtualHost site.com:443>
...
</VirtualHost>
for redirection:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect / https://www.yourdomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
for domain separation:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/www/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot "/www/otherdomain"
</VirtualHost>

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 hosts not working when using subdomain

I am trying to setup a virtual host. I have done this many times before on different servers with no problem, however, I have never tried it using a subdomain.
For some strange reason when I go to the subdomain: sub.mysite.co.uk I get redirected to the /var/www/mysite directory and not /var/www/other-site
Here is my apache.conf file:
Include /etc/apache2/sites-enabled/
NameVirtualHost *:80
<ifmodule mod_ssl.c>
NameVirtualHost *:443
</ifmodule>
<VirtualHost *:80>
ServerName *.mysite.co.uk
DocumentRoot "/var/www/newsletters/"
</VirtualHost>
And my sites-enabled file:
<VirtualHost *:80>
ServerName mysite.co.uk
DocumentRoot /var/www/newsletters
ServerAlias sub.mysite.co.uk
</VirtualHost>
Have I missed something?
Did you check your DNS entries? Maybe it's redirecting all subdomains to mysite.co.uk.
Write this in httpd.conf
Include /etc/apache2/sites-enabled/*.conf
Listen *:80
Write this in custom-vhost.conf located in sites-enabled
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot /var/www/domain1
ServerName domain1.me
ServerAlias www.domain1.me
</VirtualHost>
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot /var/www/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
Hope it works

Apache default host

I have Apache2, it's document root structure is:
/var/www/html
---index.html
---example.com
------/index.html
I want all requests to my-site.com to point to example.com/index.html and all other requests to be served from /var/www/html.
Here's my config:
<VirtualHost _default_:80>
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName my-site.com
DocumentRoot /var/www/html/example.com
</VirtualHost>
But all requests are served from Apache document root.
Thanks for any help.
You need a NameVirtualHost directive somewhere in your config before the second virtual host.
<VirtualHost _default_:80>
DocumentRoot /var/www/html
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName my-site.com
DocumentRoot /var/www/html/example.com
</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.