Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Created April 1, 2011 00:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrrgn/897536 to your computer and use it in GitHub Desktop.
Save dhrrgn/897536 to your computer and use it in GitHub Desktop.
A simple initialization script to create vhosts.
#!/usr/bin/env ruby
# USAGE: sudo ./init.rb project_name path/to/web/root
# NOTE: This file must be run with root privileges (sudo).
# You must also give it exec permissions: chmod +x init.rb
# This should include the trailing slash
DEFAULT_ROOT = '/Users/dan/Sites/'
VHOST_FOLDER = '/etc/apache2/sites/'
if ARGV[0].nil? or ARGV[1].nil? then
puts 'Usage: ./init.rb <project_name> <web_root>'
exit
end
project_name = ARGV[0]
web_root = ARGV[1].chomp("/") + "/"
web_root = DEFAULT_ROOT + web_root unless web_root.index('/') == 0
vhost_template = <<vhost
<VirtualHost *:80>
ServerAdmin admin@foo.com
DocumentRoot "#{web_root}"
ServerName #{project_name}.lvh.me
SetEnv ENVIRONMENT dev
ErrorLog "/private/var/log/apache2/#{project_name}-error_log"
CustomLog "/private/var/log/apache2/#{project_name}-access_log" common
</VirtualHost>
<Directory #{web_root}>
AllowOverride All
</Directory>
vhost
filename = "#{VHOST_FOLDER}#{project_name}.conf"
File.open(filename, 'w+') {|f| f.write(vhost_template) }
puts "#{filename} has been created."
puts 'Restarting apache...'
# Restart Apache
exec('sudo apachectl restart')
puts 'Apache Restarted'
@lukeholder
Copy link

nice!

@maxzender
Copy link

Super-useful, thank you for sharing!
I added this to my ~/.profile in order to call init.rb regardless of my current working directory:

alias init='sudo ~/init.rb'

This allows you to call the script from anywhere like that:

init project_name web_root

Thought you might find this useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment