Web Server Config Generator

Installation:

# gem install web_server_config_generator

First Run (initialize global config):

# web_server_setup /path/to/projects_dir

Second Run (generate web server config files for all projects with a config):

# web_server_setup

Generating a config file for a new project:

# web_server_setup /path/to/new_project

Examples

$ pwd /Users/jason/projects $ ls twitter_live_demo

# first run $ web_server_setup . # dot to specify current directory It looks like this is the first time you’ve run me.

setup /Users/jason/projects as your projects dir? [Y/n] |Y|

setting up config dir

Setup ghost entries for projects? [Y/n] |Y|

Setting up ghost entries…

0 hosts were already setup correctly

Warning: You’ll need to make sure this line is in your nginx config, in the http block:

include /Users/jason/.webconfig/vhost/nginx/projects.conf;

Warning: Couldn’t find the following line in your nginx conf. It should be in the http block.

server_names_hash_bucket_size 128;

Restart nginx? [Y/n] |Y|

Restarting nginx… running: sudo nginx -s quit; sleep 1 && sudo /opt/nginx/sbin/nginx Password:

# this generates twitter_live_demo/.webconfig.yml then generates the web server config files based on that $ web_server_setup twitter_live_demo

Setup ghost entries for projects? [Y/n] |Y|

Setting up ghost entries… The following hostnames were added for 127.0.0.1:

twitter-live-demo-cucumber.local
twitter-live-demo-culerity_continuousintegration.local
twitter-live-demo-culerity_development.local
twitter-live-demo-development.local
twitter-live-demo-production.local
twitter-live-demo-test.local

Restart nginx? [Y/n] |Y|

Restarting nginx… running: sudo killall nginx; sleep 1 && sudo /opt/nginx/sbin/nginx

# regenrate all the server config files for the projects that have configs $ web_server_setup

Setup ghost entries for projects? [Y/n] |Y|

Setting up ghost entries…

6 hosts were already setup correctly

Restart nginx? [Y/n] |Y|

Restarting nginx… running: sudo killall nginx; sleep 1 && sudo /opt/nginx/sbin/nginx

Relative Root Url Apps

Check out the example at the top of a newly genereated .webconfig.yml file for a project to see how you configure relative root apps.

Motivation

I got tired of setting up vhosts for new rails projects, and jumping through hoops to get vhosts for different environments working under the same webserver. So, point this script at your projects directory and it’ll generate all the necessary nginx config files for you to access all of your projects.

Here’s a rundown of how it works:

You’ve got a projects directory:

/Users/jason/projects

Inside there you have a bunch of stuff, some of which are rails projects, maybe even some relative root url projects:

projects/
  bouncy_balls/
  shooter_mc_blasty_pants/
  lib_intent/
  drug_wars/

Now, the first time you run this script you’ll tell it where your projects directory is by either cd’ing there, or passing the path as the first argument:

# web_server_setup /Users/jason/projects

It’ll prompt you to create an initial configuration including where your projects directory resides. This is by default at $HOME/.webconfig.

In that folder full of useful files it will generate some symlinks to allow each project to run in a different environment, as well as an individual config file for each project/environment combination. You don’t really need to worry about all that though because in the end…

You just have to run the script and point it at new project directories and it’ll create a config file for that project that you can edit to suit your needs. From that point on you can just run web_server_setup without arguments and it’ll generate all the necessary web server conf files for all projects with a config file.

The script then prints out a line that you’ll copy into your nginx.conf file that takes care of loading up all of your configuration.

The other piece of the puzzle this script takes care of is hostname resolution. Assuming you have the ‘ghost’ gem installed you’ll be prompted to add all of the generated host names to ghost.

So, once all that’s done you can reload nginx and fire up your trusty browser, visiting:

http://drug-wars-development.local

But wait there’s more, you’re developing along, and you see something weird, and you want to pair up with bob to check it out. It’s a hassle to reproduce, but that’s ok, you can just point bob to your box because we’re also listening on a generated port number. A quick look at the drug_wars config file (.webconfig.yml) and we see it is running in development on port 45397, so bob can just browse to your machine on that port and bingo, he’s looking at the same data as you.

Now you can point your tests at the test environment of an app and run your tests against nginx for speedier testing as well.

And the next time you create a new project? You can run this script and pass that directory as the first argument and we’ll generate our config for that project, then the web server config files.