Find file
Fetching contributors…
Cannot retrieve contributors at this time
131 lines (100 sloc) 3.33 KB
# Setup a Ubuntu 12.04 server install for Django
# Needs to be run as root (or with sudo)
# The ubuntu user password
uid=`id -u`
if [ $uid -ne 0 ]
echo 'Must be run as root'
# Determine if this script is being run on an EC2 image or a local VM
if [[ ! `hostname` =~ "^domU-" ]]
host=`ifconfig eth0 | awk '$1 == "inet" {split($2,a,":"); print a[2]}'`
apt-get install -y git nginx monit mysql-server python-mysqldb
# Install Django
wget -O Django-1.4.tar.gz ""
tar xzvf Django-1.4.tar.gz
cd Django-1.4
sudo python install
# Set up nginx
echo "# the IP(s) on which node server is running
upstream app_apoxz {
# the nginx server instance
server {
server_name $host;
access_log /var/log/nginx/apoxz.log;
if (\$host != '$host' ) {
rewrite ^/(.*)$ http://$host/\$1 permanent;
# pass the request to the Django server with the correct headers and much more can be added, see nginx config options
location / {
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_apoxz/;
proxy_redirect off;
location /static/ {
autoindex on;
alias /var/www/;
}" > /etc/nginx/sites-available/apoxz
ln -s /etc/nginx/sites-available/apoxz /etc/nginx/sites-enabled/apoxz
sed -i -e s/#.server_names_hash_bucket_size/server_names_hash_bucket_size/ /etc/nginx/nginx.conf
nginx -t
# Set up upstart config
echo '#!upstart
description "APOXZ Django server"
author "Dave Kelly"
start on runlevel [2345]
stop on runlevel [016]
export HOME="/root"
echo $$ > /var/run/
exec sudo -u ubuntu python /var/www/ runserver >> /var/log/apoxz.sys.log 2>&1
end script
pre-start script
# Date format same as (new Date()).toISOString() for consistency
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/apoxz.sys.log
end script
pre-stop script
rm /var/run/
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/apoxz.sys.log
end script' > /etc/init/apoxz.conf
# Setup git repository
mkdir -p /opt/apoxz.git
chown -R root:ubuntu /opt/apoxz.git/
chmod -R 775 /opt/apoxz.git/
git init --bare /opt/apoxz.git
mkdir -p /var/www/
chown -R root:ubuntu /var/www/
chmod -R 775 /var/www/
echo "#!/bin/sh
GIT_WORK_TREE=/var/www/ git checkout -f
echo $PASSWORD | sudo -S service apoxz restart
echo $PASSWORD | sudo -S service nginx restart" > /opt/apoxz.git/hooks/post-receive
chmod +x /opt/apoxz.git/hooks/post-receive
# Set up monit conf
echo '#!monit
set logfile /var/log/monit.log
check process apoxz with pidfile "/var/run/"
start program = "/sbin/start apoxz"
stop program = "/sbin/stop apoxz"
if failed port 8000 protocol HTTP
request /
with timeout 10 seconds
then restart' > /etc/monit/conf.d/apoxz.conf
service nginx start
rm -rf Django-1.4.tar.gz Django-1.4
mysql -u root -p -e 'create database apoxz;'
echo 'Completed apoxz web installation.'