Skip to content

Instantly share code, notes, and snippets.

@psywhale
Last active March 1, 2019 21:40
Show Gist options
  • Save psywhale/42b8b19794105a1370e190078f791791 to your computer and use it in GitHub Desktop.
Save psywhale/42b8b19794105a1370e190078f791791 to your computer and use it in GitHub Desktop.
#!/bin/bash
#USAGE:
# /etc/skel has the user home directory setup /home/[user]
# run this script and pipe the output to whereever your apache sites are configured
# create the virtualenv in /home/[user]/venv
# activate it
# pip install -r requirements.txt
# upload your django project
# update the WSGIScriptalias line below to point to wsgi.py
# enable this site and restart apache
do_help () {
echo "$0 usage:"
echo " -h hostname to create"
echo " EX: $0 -h pencil"
echo " creates a pencil.example.com django host"
}
touchlogfiles () {
host=$1
echo "$host log files"
touch "/var/log/apache2/${host}_error.log"
touch "/var/log/apache2/${host}_access.log"
}
createuser () {
echo "making user...$1"
if [ -n "$1" ]; then
useradd -m --shell /bin/false $1
fi
}
createsite () {
cat << EOLOL
<VirtualHost *:443>
#CHANGE THE FOLLOWING Paths and CERT files to your implementation
SSLEngine on
SSLCertificateFile /etc/ssl/certs/Cert.crt
SSLCertificateKeyFile /etc/ssl/private/PrivateKey.key
SSLCACertificateFile /etc/ssl/certs/CACert.crt
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName $1.example.com
ServerAdmin webmaster@localhost
DocumentRoot /home/$1
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
LogLevel info debug
Alias /static /home/$1/static
<Directory /home/$1/static>
Header always unset X-Frame-Options
Require all granted
</Directory>
<Directory /home/$1/Status>
<Files wsgi.py>
Header always unset X-Frame-Options
Require all granted
</Files>
</Directory>
WSGIDaemonProcess $1 python-path=/home/$1 python-home=/home/$1/venv
WSGIProcessGroup $1
WSGIScriptAlias / /home/$1/TODO/wsgi.py
ErrorLog \$\{APACHE_LOG_DIR\}/$1_error.log
CustomLog \$\{APACHE_LOG_DIR\}/$1_access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with 'a2disconf'.
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName $1.example.com
ServerAdmin webmaster@localhost
DocumentRoot /home/$1
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
LogLevel info debug
# Alias /static /home/$1/static
# <Directory /home/$1/static>
# Require all granted
# </Directory>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
<Directory /home/$1/dir>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog \${APACHE_LOG_DIR}/$1_error.log
CustomLog \${APACHE_LOG_DIR}/$1_access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with 'a2disconf'.
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
EOLOL
echo $template
}
if [ -z "$1" ]; then
do_help
exit 1
fi
while getopts "s:" opt; do
case $opt in
s)
hosting=$OPTARG
;;
*)
do_help
exit 1
;;
esac
done
if [ -z "$hosting" ]; then
do_help
exit 1
fi
touchlogfiles $hosting
createuser $hosting
createsite $hosting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment