Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added in the new nginx-vhost, which is awesome in every way.

It generates vhosts for nginx all on its own, and installs Wordpress
in the directory.
  • Loading branch information...
commit bcd03b46a7a0b7dd8c2ba4854a93e2c85ca0275c 1 parent ab3d850
Josh Gachnang authored
View
44 cmd-test.py
@@ -0,0 +1,44 @@
+import os, subprocess, sys
+
+
+def main(argv):
+ run(argv)
+
+def run(arg):
+ """
+ Runs the given arg at the command line using the default shell. Outputs
+ when commands are run successfully.
+
+ Based on http://developer.spikesource.com/wiki/index.php/How_to_invoke_subprocesses_from_Python
+
+ @param Tuple args
+ A tuple of args, with the first being the command to be run, and
+ the remaining ones flags and arguments for the command. STDOUT and
+ STDERR are piped to tuple, waiting until the output is finished,
+ then writing both to the log file, if not empty.
+ Ex. ['apt-get', '-y', 'install', 'dnsmasq'], which installs
+ dnsmasq using apt-get, and assumes yes to questions.
+ """
+
+ # Open output and write some info about the command to be written, including
+ # name of command and arguments.
+ # This could be modified to adjust how much is printed via a DEBUG variable.
+ with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
+ outFile.write("Command: ")
+ for a in arg:
+ outFile.write(a,)
+ outFile.write(" ")
+ outFile.write("\n")
+ # Open output and error log file and append to them the output of the commands
+ with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
+ with open(os.path.join(os.curdir, "error.log"), 'a') as errorFile:
+ # Call the subprocess using convenience method
+
+ retval = subprocess.call(arg, -1, None, None, outFile, errorFile)
+ # Check the process exit code, print error information if it exists
+ if not retval == 0:
+ errData = errorFile.read()
+ raise Exception("Error executing command: " + repr(errData))
+
+if __name__=="__main__":
+ main(sys.argv[1:])
View
43 cmd-test.py~
@@ -0,0 +1,43 @@
+import os, subprocess, sys
+
+
+def main(argv):
+ run(argv)
+
+def run(arg):
+ """
+ Runs the given arg at the command line using the default shell. Outputs
+ when commands are run successfully.
+
+ Based on http://developer.spikesource.com/wiki/index.php/How_to_invoke_subprocesses_from_Python
+
+ @param Tuple args
+ A tuple of args, with the first being the command to be run, and
+ the remaining ones flags and arguments for the command. STDOUT and
+ STDERR are piped to tuple, waiting until the output is finished,
+ then writing both to the log file, if not empty.
+ Ex. ['apt-get', '-y', 'install', 'dnsmasq'], which installs
+ dnsmasq using apt-get, and assumes yes to questions.
+ """
+
+ # Open output and write some info about the command to be written, including
+ # name of command and arguments.
+ with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
+ outFile.write("Command: ")
+ for a in arg:
+ outFile.write(a,)
+ outFile.write(" ")
+ outFile.write("\n")
+ # Open output and error log file and append to them the output of the commands
+ with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
+ with open(os.path.join(os.curdir, "error.log"), 'a') as errorFile:
+ # Call the subprocess using convenience method
+
+ retval = subprocess.call(arg, -1, None, None, outFile, errorFile)
+ # Check the process exit code, print error information if it exists
+ if not retval == 0:
+ errData = errorFile.read()
+ raise Exception("Error executing command: " + repr(errData))
+
+if __name__=="__main__":
+ main(sys.argv[1:])
View
0  error.log
No changes.
View
146 nginx-vhost.sh
@@ -0,0 +1,146 @@
+#!/bin/bash
+VHOST_DIR='/etc/nginx/sites-available'
+USER_DIR='/ebs/www'
+USERNAME=''
+USERPASS=''
+DOMAIN=''
+MSYQLPASS=''
+USER_TRUNC=''
+DOMAIN_TRUNC=''
+DB_PASS=''
+
+RET=''
+function sanity_check {
+ if [ "$(id -u)" != "0" ]; then
+ echo "Script must be run as root."
+ exit 1
+ fi
+ if [[ $1 != 2 ]]; then
+ echo $1
+ echo "Usage: nginx-vhost.sh username example.com"
+ exit 4
+ fi
+
+ egrep "^$USERNAME:" /etc/passwd >/dev/null
+ if [ $? -eq 0 ]; then
+ echo "$USERNAME exists!"
+ exit 3
+ fi
+
+}
+
+function password {
+ RET=$(cat /dev/urandom | tr -cd [:alnum:] | head -c ${1:-16})
+}
+
+function setup_user {
+ useradd -m -d $USER_DIR/$USERNAME -U $USERNAME
+
+ password
+ PASSWORD=$RET
+ echo "$USERNAME:$PASSWORD" | chpasswd
+
+ mkdir -p $USER_DIR/$USERNAME/$DOMAIN/htdocs
+ mkdir $USER_DIR/$USERNAME/$DOMAIN/logs
+ touch $USER_DIR/$USERNAME/$DOMAIN/logs/access.log
+ touch $USER_DIR/$USERNAME/$DOMAIN/logs/error.log
+ chown -R $USERNAME $USER_DIR/$USERNAME
+ chgrp -R $USERNAME $USER_DIR/$USERNAME
+
+ echo "Create user: $USERNAME with password: $PASSWORD"
+}
+function php_pool {
+ if [ ! -f /etc/php5/fpm/port ]; then
+ echo "Cannot access /etc/php5/fpm/port"
+ exit 2
+ fi
+
+ # Grab the port from file, and increment.
+ PORT=$(cat /etc/php5/fpm/port)
+ echo $(($PORT + 1)) > /etc/php5/fpm/port
+
+ cp /etc/php5/fpm/pool.template /etc/php5/fpm/pools/$DOMAIN
+ sed -i "s|example.com|$DOMAIN|" /etc/php5/fpm/pools/$DOMAIN
+
+ sed -i "s|PORT|$PORT|" /etc/php5/fpm/pools/$DOMAIN
+ sed -i "s|user = example|user = $USERNAME|" /etc/php5/fpm/pools/$DOMAIN
+ sed -i "s|group = example|group = $USERNAME|" /etc/php5/fpm/pools/$DOMAIN
+ echo "Added $DOMAIN to the PHP pool"
+}
+function nginx_vhost {
+ cp /etc/nginx/vhost.template /etc/nginx/sites-available/$DOMAIN
+ sed -i "s|example.com|$DOMAIN|g" /etc/nginx/sites-available/$DOMAIN
+ sed -i "s|username|$USERNAME|g" /etc/nginx/sites-available/$DOMAIN
+ sed -i "s|PORT|$PORT|g" /etc/nginx/sites-available/$DOMAIN
+ # Should probably sed through and replace /ebs/www with $USER_DIR
+
+ # Enable the site
+ ln -s /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/$DOMAIN
+ echo "Enabled $DOMAIN in the web server"
+}
+function server_reload {
+ /etc/init.d/php5-fpm reload
+ /etc/init.d/nginx reload
+ echo "Servers reloaded"
+}
+function prepare_mysql {
+ # Generate dbuser password
+ password
+ DB_PASS=$RET
+
+ # Truncate username (15 chars max) and dbname (63 chars max)
+ USER_TRUNC=$(echo $USERNAME | cut -c1-15)
+
+ # This should be a separate user with only create perms.
+ echo "CREATE DATABASE $USER_TRUNC;
+GRANT ALL PRIVILEGES ON $USER_TRUNC.* to '$USER_TRUNC'@'localhost' IDENTIFIED BY '$DB_PASS';" > $USERNAME.sql
+ mysql -u root -p$(cat /root/mysql) < $USERNAME.sql
+ rm $USERNAME.sql
+ echo "Created MySQL user: $USER_TRUNC password: $DB_PASS database: $DOMAIN_TRUNC"
+}
+function install_wordpress {
+ wget http://wordpress.org/latest.tar.gz
+ tar xf latest.tar.gz
+ mv wordpress/* $USER_DIR/$USERNAME/$DOMAIN/htdocs/
+ cp $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config-sample.php $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+ sed -i "s|database_name_here|$USER_TRUNC|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+ sed -i "s|username_here|$USERNAME|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+ sed -i "s|password_here|$DB_PASS|g" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+
+ # Add in salts from the wordpress salt generator
+ wget https://api.wordpress.org/secret-key/1.1/salt/
+ sed -i "s/|/a/g" index.html
+# cat index.html | while read line; do
+ # sed -i "s|define('.*',.*'put your unique phrase here');|$line|" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+# sed 1d $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+# done
+ sed -i '/#@-/r index.html' $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+ sed -i "/#@+/,/#@-/d" $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+ rm index.html
+ rm latest.tar.gz
+
+ # Add in FTP stuff, even though that's not defined yet..
+
+ # Own these new files
+ chown -R $USERNAME $USER_DIR/$USERNAME/$DOMAIN/htdocs/*
+ chgrp -R $USERNAME $USER_DIR/$USERNAME/$DOMAIN/htdocs/*
+ # Make sure no one else can read this file
+ chmod 700 $USER_DIR/$USERNAME/$DOMAIN/htdocs/wp-config.php
+
+ #echo "Wordpress for $DOMAIN installed."
+ #echo "Visit http://$DOMAIN/wp-admin/install.php to complete installation"
+}
+
+##############################################################################
+# Start of program
+USERNAME=$1
+DOMAIN=$2
+
+sanity_check $#
+setup_user
+php_pool
+nginx_vhost
+server_reload
+prepare_mysql
+install_wordpress
+exit 0
View
65 output.log
@@ -0,0 +1,65 @@
+total 36
+-rw-r--r-- 1 josh josh 1471 2011-01-04 14:29 cmd-test.py
+-rw-r--r-- 1 josh josh 1473 2011-01-04 14:29 cmd-test.py~
+-rw-r--r-- 1 josh josh 0 2011-01-04 14:29 error.log
+-rw-r--r-- 1 josh josh 4514 2011-01-04 13:02 install-wordpress.sh
+-rw-r--r-- 1 josh josh 1133 2011-01-04 13:02 karmicsetup.sh
+-rw-r--r-- 1 josh josh 0 2011-01-04 14:29 output.log
+-rw-r--r-- 1 josh josh 0 2011-01-04 13:02 README
+-rw-r--r-- 1 josh josh 881 2011-01-04 13:02 rsyncFreeNAS.sh
+-rw-r--r-- 1 josh josh 809 2011-01-04 13:02 ssh-secure.py
+-rwxr-xr-x 1 josh josh 3147 2011-01-04 14:20 vhost-gen.py
+-rwxr-xr-x 1 josh josh 3141 2011-01-04 14:20 vhost-gen.py~
+Command: ls-ltotal 40
+-rw-r--r-- 1 josh josh 1471 2011-01-04 14:30 cmd-test.py
+-rw-r--r-- 1 josh josh 1471 2011-01-04 14:30 cmd-test.py~
+-rw-r--r-- 1 josh josh 0 2011-01-04 14:29 error.log
+-rw-r--r-- 1 josh josh 4514 2011-01-04 13:02 install-wordpress.sh
+-rw-r--r-- 1 josh josh 1133 2011-01-04 13:02 karmicsetup.sh
+-rw-r--r-- 1 josh josh 663 2011-01-04 14:30 output.log
+-rw-r--r-- 1 josh josh 0 2011-01-04 13:02 README
+-rw-r--r-- 1 josh josh 881 2011-01-04 13:02 rsyncFreeNAS.sh
+-rw-r--r-- 1 josh josh 809 2011-01-04 13:02 ssh-secure.py
+-rwxr-xr-x 1 josh josh 3147 2011-01-04 14:20 vhost-gen.py
+-rwxr-xr-x 1 josh josh 3141 2011-01-04 14:20 vhost-gen.py~
+Command: ls-l
+total 40
+-rw-r--r-- 1 josh josh 1492 2011-01-04 14:30 cmd-test.py
+-rw-r--r-- 1 josh josh 1490 2011-01-04 14:30 cmd-test.py~
+-rw-r--r-- 1 josh josh 0 2011-01-04 14:29 error.log
+-rw-r--r-- 1 josh josh 4514 2011-01-04 13:02 install-wordpress.sh
+-rw-r--r-- 1 josh josh 1133 2011-01-04 13:02 karmicsetup.sh
+-rw-r--r-- 1 josh josh 1327 2011-01-04 14:30 output.log
+-rw-r--r-- 1 josh josh 0 2011-01-04 13:02 README
+-rw-r--r-- 1 josh josh 881 2011-01-04 13:02 rsyncFreeNAS.sh
+-rw-r--r-- 1 josh josh 809 2011-01-04 13:02 ssh-secure.py
+-rwxr-xr-x 1 josh josh 3147 2011-01-04 14:20 vhost-gen.py
+-rwxr-xr-x 1 josh josh 3141 2011-01-04 14:20 vhost-gen.py~
+Command: ldfs-l
+Command: ls-l
+total 40
+-rw-r--r-- 1 josh josh 1492 2011-01-04 14:32 cmd-test.py
+-rw-r--r-- 1 josh josh 1494 2011-01-04 14:32 cmd-test.py~
+-rw-r--r-- 1 josh josh 0 2011-01-04 14:29 error.log
+-rw-r--r-- 1 josh josh 4514 2011-01-04 13:02 install-wordpress.sh
+-rw-r--r-- 1 josh josh 1133 2011-01-04 13:02 karmicsetup.sh
+-rw-r--r-- 1 josh josh 2007 2011-01-04 14:32 output.log
+-rw-r--r-- 1 josh josh 0 2011-01-04 13:02 README
+-rw-r--r-- 1 josh josh 881 2011-01-04 13:02 rsyncFreeNAS.sh
+-rw-r--r-- 1 josh josh 809 2011-01-04 13:02 ssh-secure.py
+-rwxr-xr-x 1 josh josh 3147 2011-01-04 14:20 vhost-gen.py
+-rwxr-xr-x 1 josh josh 3141 2011-01-04 14:20 vhost-gen.py~
+Command: lfs-l
+Command: ls-l
+total 40
+-rw-r--r-- 1 josh josh 1468 2011-01-04 14:33 cmd-test.py
+-rw-r--r-- 1 josh josh 1492 2011-01-04 14:33 cmd-test.py~
+-rw-r--r-- 1 josh josh 0 2011-01-04 14:29 error.log
+-rw-r--r-- 1 josh josh 4514 2011-01-04 13:02 install-wordpress.sh
+-rw-r--r-- 1 josh josh 1133 2011-01-04 13:02 karmicsetup.sh
+-rw-r--r-- 1 josh josh 2686 2011-01-04 14:33 output.log
+-rw-r--r-- 1 josh josh 0 2011-01-04 13:02 README
+-rw-r--r-- 1 josh josh 881 2011-01-04 13:02 rsyncFreeNAS.sh
+-rw-r--r-- 1 josh josh 809 2011-01-04 13:02 ssh-secure.py
+-rwxr-xr-x 1 josh josh 3147 2011-01-04 14:20 vhost-gen.py
+-rwxr-xr-x 1 josh josh 3141 2011-01-04 14:20 vhost-gen.py~
View
170 vhost-gen.py~
@@ -0,0 +1,170 @@
+#!/usr/bin/python
+# Copyright Josh Gachnang 2010
+# Release under the New BSD License
+# ServerCobra.com
+
+# Based off http://www.straw-dogs.co.uk/12/10/python-virtual-host-creator/
+
+import getopt
+import os
+import subprocess
+import sys
+from random import randint, choice
+
+def main(argv):
+
+ try:
+ opts, args = getopt.getopt(argv, "hd:a:", ["help", "domain="])
+ except getopt.GetoptError:
+ usage()
+ sys.exit(2)
+
+ if len(opts) == 0:
+ usage()
+ sys.exit(2)
+
+ for o, a in opts:
+ if o in ("-h", "--help"):
+ usage()
+ sys.exit()
+ if o in ("-d", "--domain"):
+ domain = a
+ normalize_domain = domain.replace(".", "__")
+ else:
+ assert False, "unhandled option"
+
+
+ #if not os.path.isdir("/var/www/%s" % directory):
+ # os.mkdir("/var/www/%s" % directory)
+
+ #if not os.path.isdir("/var/www/%s/htdocs" % directory):
+ # os.mkdir("/var/www/%s/htdocs" % directory)
+
+ default_vhost_template = """
+ <VirtualHost *:80>
+ ServerName __DOMAIN__
+ ServerAlias www.__DOMAIN__
+
+
+ #Log files
+ CustomLog /home/__NORM_DOM__/__NORM_DOM__/logs/access.log combined
+ ErrorLog /home/__NORM_DOM__/__NORM_DOM__/logs/error.log
+
+ ErrorDocument 404 /404.html
+ ErrorDocument 401 /401.html
+ ErrorDocument 500 /500.html
+
+ DocumentRoot /home/__NORM_DOM__/__NORM_DOM__/htdocs
+ <Directory /home/__NORM_DOM__/__NORM_DOM__/htdocs >
+ Options +Indexes +FollowSymlinks +ExecCGI +Includes -MultiViews
+ AllowOverride All
+ Order Allow,Deny
+ Allow from all
+ </directory>
+
+ </virtualhost>
+ """
+
+ django_vhost_template = """
+ <VirtualHost *:80>
+ ServerName __DOMAIN__
+ ServerAlias www.__DOMAIN__
+
+
+ #Log files
+ CustomLog /home/__NORM_DOM__/__NORM_DOM__/logs/access.log combined
+ ErrorLog /home/__NORM_DOM__/__NORM_DOM__/logs/error.log
+
+ ErrorDocument 404 /404.html
+ ErrorDocument 401 /401.html
+ ErrorDocument 500 /500.html
+
+ DocumentRoot /home/__NORM_DOM__/__NORM_DOM__/htdocs
+ <Directory /home/__NORM_DOM__/__NORM_DOM__/htdocs >
+ Options +Indexes +FollowSymlinks +ExecCGI +Includes -MultiViews
+ AllowOverride All
+ Order Allow,Deny
+ Allow from all
+ </directory>
+
+ Alias /static /home/__NORM_DOM__/__NORM_DOM__/__APP__/static/
+ <Location "/static">
+ Order allow,deny
+ Allow from all
+ </Location>
+
+ Alias /media/ /usr/lib/python-django/django/contrib/admin/media/
+ <Location "/media/">
+ SetHandler None
+ Order allow,deny
+ Allow from all
+ </Location>
+
+ #Start mod_wsgi
+ WSGIScriptAlias / /home/__NORM_DOM__/__NORM_DOM__/__APP__/apache/wsgi_handler.py
+ <Directory "/home/__NORM_DOM__/__NORM_DOM__/__APP__/apache">
+ Order allow,deny
+ Allow from all
+ </Directory>
+
+ WSGIDaemonProcess __NORM_DOM__ user=__NORM_DOM__ group=__NORM_DOM__ processes=2 threads=10
+ WSGIProcessGroup __NORM_DOM__
+
+ </virtualhost>
+ """
+
+
+ vhost = vhost_template.replace('__DOMAIN__', domain).replace('__NORM_DOM__', normalize_domain)
+
+ open('/etc/apache2/sites-available/%s.conf' % directory, 'w').write(vhost)
+ os.system('a2ensite %s.conf' % directory)
+ os.system('/etc/init.d/apache2 reload')
+
+# Characters to be used while generating password
+chars = string.ascii_letters + string.digits + "!#$&"
+
+def random_password(length):
+ return "".join(choice(chars) for x in range(randint(length, length)))
+
+def usage():
+ print 'usage: newv.py [-d domain.com]'
+ print ' Please exclude the "www" at the front of the domain'
+
+def run(arg):
+ """
+ Runs the given arg at the command line using the default shell. Outputs
+ when commands are run successfully.
+
+ Based on http://developer.spikesource.com/wiki/index.php/How_to_invoke_subprocesses_from_Python
+
+ @param Tuple args
+ A tuple of args, with the first being the command to be run, and
+ the remaining ones flags and arguments for the command. STDOUT and
+ STDERR are piped to tuple, waiting until the output is finished,
+ then writing both to the log files, if not empty.
+ Ex. ['apt-get', '-y', 'install', 'dnsmasq'], which installs
+ dnsmasq using apt-get, and assumes yes to questions.
+ """
+
+ # Open output and write some info about the command to be written, including
+ # name of command and arguments.
+ # This could be modified to adjust how much is printed via a DEBUG variable.
+ with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
+ outFile.write("Command: ")
+ for a in arg:
+ outFile.write(a,)
+ outFile.write(" ")
+ outFile.write("\n")
+ # Open output and error log file and append to them the output of the commands
+ with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
+ with open(os.path.join(os.curdir, "error.log"), 'a') as errorFile:
+ # Call the subprocess using convenience method
+
+ retval = subprocess.call(arg, -1, None, None, outFile, errorFile)
+ # Check the process exit code, print error information if it exists
+ if not retval == 0:
+ errData = errorFile.read()
+ raise Exception("Error executing command: " + repr(errData))
+
+if __name__=="__main__":
+ main(sys.argv[1:])
Please sign in to comment.
Something went wrong with that request. Please try again.