Virtual Host With SSL Support on OS X Mavericks - apache

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.

Related

ordering VirtualHosts in Apache conf

I'm having issues with my apache configuration. The expected outcome is to allow all traffic to go to the default site trekfederation.com, UNLESS the subdomain is specified and matched otherwise. My problem is that some of my subdomains still default to the main site.
I have two files as part of my configuration:
httpd.conf
ships.conf
here's httpd.conf's portion for virtualhost:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
# ServerAdmin webmaster#dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
<VirtualHost *:80>
ServerName weather.trekfederation.com
ServerAlias weather.trekfederation.com
DocumentRoot "/var/www/html/weather"
</VirtualHost>
<VirtualHost *:80>
ServerName oldsite.trekfederation.com
ServerAlias oldstore.trekfederation.com
DocumentRoot "/var/www/html/trekfed"
</VirtualHost>
<VirtualHost *:80>
ServerName promenade.trekfederation.com
Redirect / http://trekfederation.com/shop
</VirtualHost>
<VirtualHost *:80>
ServerName donations.trekfederation.com
Redirect / http://trekfederation.com/product-category/donations/
</VirtualHost>
Include conf.d/ships.conf
<VirtualHost *:80>
ServerName trekfederation.com
ServerAlias trekfederation.com www.trekfederation.com
Alias /phpmyadmin "/usr/share/phpMyAdmin"
<Directory "/usr/share/phpMyAdmin">
DirectoryIndex index.php
AllowOverride All
Options FollowSymlinks
Order allow,deny
Allow from all
</Directory>
ServerAlias www.iftcommand.com
DocumentRoot "/var/www/html/trekweb"
</VirtualHost>
# Include conf.d/personal.conf
<VirtualHost *:80>
ServerName ccc.trekfederation.com
DocumentRoot "/var/www/html/ccc"
</VirtualHost>
Ships.conf:
<VirtualHost *:80>
ServerName ussgeorgewashington.trekfederation.com
ServerAlias ussgeorgewashington.trekfederation.net
ServerAlias nfc1773a.trekfederation.com
ServerAlias nfc1773a.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_GEORGE_WASHINGTON
</VirtualHost>
<VirtualHost *:80>
ServerName ussfarragut.trekfederation.com
ServerAlias ussfarragut.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_FARRAGUT
</VirtualHost>
<VirtualHost *:80>
ServerName ussvortex.trekfederation.com
ServerAlias ussvortex.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_VORTEX
</VirtualHost>
<VirtualHost *:80>
ServerName ussvictory.trekfederation.com
ServerAlias ussvictory.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_VICTORY
</VirtualHost>
<VirtualHost *:80>
ServerName ussnavigator.trekfederation.com
ServerAlias ussnavigator.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_NAVIGATOR
</VirtualHost>
<VirtualHost *:80>
ServerName ussfirelace.trekfederation.com
ServerAlias ussfirelace.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_FIRELACE
</VirtualHost>
the site i'm working on is ussfirelace.trekfederation.com. it still routes to trekfederation.com.
it does register with apache -S:
port 80 namevhost ussfirelace.trekfederation.com (/etc/httpd/conf.d/ships.conf:34)
alias ussfirelace.trekfederation.net
what am I missing?
Thanks.
The configs look good to me. Possible solutions / problems:
a) Did you restart your apache to load the new virtual host?
b) Have you checked that the DNS record points as A Record (or CNAME) to your server and no redirect is configured with your NS provider?
c) Have you checked your code, that it does not trigger a redirect (e.g. 301, or javascript...)?
d) Clearing browser cache is also some thing that you should take care of.
e) Doublecheck the URL you type for typos.

Apache22: how to redirect a dir to https?

Say, I have a domain http://www.example.com. I have this in httpd.conf: DocumentRoot "/Apache22/htdocs".
I also have /Apache22/htdocs/dir1 application directory, which I want to force users to connect to with https, say, with URL: https://www.example.com/dir1 and redirect any http requests to https for this directory.
I tried VirtualHost and Redirect so:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /Apache22/htdocs/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com/dir1
DocumentRoot /Apache22/htdocs/dir1
Redirect permanent /dir1 https://www.example.com/dir1
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
ServerName www.example.com/dir1
DocumentRoot /Apache22/htdocs/dir1
SSLEngine on
SSLCertificateFile conf/ssl/server.cert
SSLCertificateKeyFile conf/ssl/server.key
</VirtualHost>
But apparently Apache ignores any subdrectories in ServerName, so both www.example.com and www.example.com/dir1 are the same in VirtualHost block, as far as Apache concerned.
How do I achieve an https redirect of any URL accessing dir1?
Try:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /Apache22/htdocs/
Redirect permanent /dir1 https://www.example.com/dir1
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /Apache22/htdocs
SSLEngine on
SSLCertificateFile conf/ssl/server.cert
SSLCertificateKeyFile conf/ssl/server.key
</VirtualHost>
The ServerName directive takes a host name, not anything including a path component.

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.