virtualhosts

Easily manage Apache VirtualHosts with Ansible and Jinja2

Server Check.in's entire infrastructure is managed via Ansible, which has helped tremendously as the service has grown from one to many servers.

Ansible Borg Cow
cowsay and Ansible were made for each other.

One pain point with running Apache servers that host more than one website (using name-based virtual hosts) is that the virtual host configuration files quickly get unwieldy, as you have to define the entire <virtualhost></virtualhost> for every domain you have on the server, and besides Apache's mod_macro, there's no easy way to define a simple structured array of information and have the vhost definitions built from that.

Force SSL (https://) for only one virtual host with .htaccess

Many servers I help administer host many websites; and every now and then, someone wants me to set up a secure (SSL) certificate for one of the websites on the server. Once the certificate is working in Apache, and users can access the site at https://example.com/, they also request that all traffic that was originally destined for either http://www.example.com/ or http://example.com/ be routed to the secure site.

This can be slightly tricky if you're using multiple VirtualHosts on the same server/multisite installation with something like WordPress or Drupal, because if you just add in something like below with multiple sites routed through the same .htaccess file, ALL sites will be routed to the https version (which is not what's desired):

<br />
RewriteEngine On<br />
RewriteCond %{SERVER_PORT} 80<br />
RewriteRule ^(.*)$ https://example.com/$1 [R,L]<br />

Simple MAMP/MAMP Pro VirtualHosts in Parallels / Windows XP

(This tip should also work similarly with Vista, Windows 7, etc.).

After browsing around a bunch of different forums, the MAMP site, and Parallels documentation, I was still flummoxed by Parallels' weird Shared Host networking behavior, which seemingly didn't allow me to access virtualhosts I set up with MAMP for developing sites locally.

After much experimentation, I found that the simplest way to be able to type in 'local.example.com' (or 'dev.example.com', if that's your style) in Internet Explorer on Windows, and get a virtual host running via MAMP on my Mac, is to do the following: