Skip to content

Instantly share code, notes, and snippets.

@cbfrance
Created August 9, 2010 10:11
Show Gist options
  • Save cbfrance/515241 to your computer and use it in GitHub Desktop.
Save cbfrance/515241 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#This does what I need done when creating a new site on Ubuntu.
#you probably have different stuff here:
site_path = "/www/sites"
available_path = "/www/confs/sites-available"
enabled_path = "/www/confs/sites-enabled"
log_path = "/www/logs"
puts "==> Enter the domain you want to setup, without subdomains or TLDs (likethis):"
domain = gets.chomp
puts "==> What's the email address of the sysadmin?"
email = gets.chomp
puts "Setting up #{domain} ...."
dummy_index= <<-EOF
<html>
<body><img src="http://www.amazing-animations.com/animations/construction5.gif" /></body>
</html>
EOF
vhost= <<-EOF
<VirtualHost *>
ServerAdmin #{email}
ServerName #{domain}.com
ServerAlias *.#{domain}.net
ServerAlias *.#{domain}.com
ServerAlias *.#{domain}.org
ServerAlias #{domain}.#{instance_address}
DirectoryIndex index.html index.htm index.php
DocumentRoot #{site_path}/#{domain}
<Directory #{site_path}/#{domain}/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog #{log_path}/#{domain}/error.log
CustomLog #{log_path}/#{domain}/access.log combined
</VirtualHost>
EOF
puts "==========================================="
puts vhost
puts "==========================================="
begin
directory_name = "#{site_path}/#{domain}"
if FileTest::directory?(directory_name)
puts "... looks like there are already some files in #{directory_name} ... leavin' em ..."
else
Dir::mkdir(directory_name)
File.open("#{directory_name}/index.html", 'w') {|f| f.write(dummy_index) }
puts "created #{site_path}/#{domain}/index.html"
end
conf = "#{available_path}/#{domain}.conf"
if File.exists?(conf)
puts "conf already exists ..."
else
File.open(conf, 'w') {|f| f.write(vhost)}
puts "created #{conf}"
end
link= "#{enabled_path}/#{domain}.conf"
if FileTest::symlink?(link)
puts "symlink already exists ..."
else
File.symlink(conf, link )
puts "created #{link}"
end
puts "do you want to edit #{conf}? [N,y]"
%x{vi #{conf}} if gets.chomp == "y"
puts "restart apache? [Y,n]"
%x{sudo apachectl restart} unless gets.chomp == "n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment