Last active
August 29, 2015 14:10
-
-
Save stefan-jonker/31fd850040033147df81 to your computer and use it in GitHub Desktop.
Attempt to automate the creation of VirtualHost for Apache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
require 'etc' | |
if ENV['USER'] != 'root' | |
raise "Script needs root-access to write in some folders." | |
else | |
print "Name of the website: " | |
siteName = gets.chomp() | |
print "Owner of the site in the Apache-folder (Default: \"#{Etc.getlogin})\": " | |
userName = gets.chomp() | |
if userName.empty? | |
userName = Etc.getlogin | |
end | |
print "Root folder location of the webserver, without trailing slash (Default: \"/var/www\"): " | |
rootFolder = gets.chomp() | |
if rootFolder.empty? | |
rootFolder = "/var/www" | |
end | |
Dir.mkdir "#{rootFolder}/#{siteName}" | |
File.open("#{rootFolder}/#{siteName}/index.html", 'w') { |file| | |
file.write( | |
<<-eos | |
<!doctype html> | |
<html> | |
<head> | |
<title>#{siteName}</title> | |
</head> | |
<body> | |
<h1>#{siteName}</h1> | |
</body> | |
</html> | |
eos | |
) | |
} | |
File.open("/etc/apache2/sites-available/#{siteName}.conf", 'w') { |file| | |
file.write( | |
<<-eos | |
<VirtualHost *:80> | |
ServerAlias #{siteName}.local | |
ServerName http://#{siteName}.local | |
DocumentRoot #{rootFolder}/#{siteName} | |
</VirtualHost> | |
eos | |
) | |
} | |
open('/etc/hosts', 'a') { |file| | |
file.puts "127.0.0.1\t#{siteName}.local" | |
} | |
FileUtils.chown_R "#{userName}", "www-data", "#{rootFolder}/#{siteName}" | |
FileUtils.chmod_R 0755, "#{rootFolder}/#{siteName}" | |
FileUtils.chmod_R "g+s", "#{rootFolder}/#{siteName}" | |
system("a2ensite #{siteName}.conf > /dev/null") | |
system("clear") | |
system("echo test") | |
print "Copy and paste:\n | |
sudo service apache2 reload\n\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment