-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_setup.sh
130 lines (100 loc) · 3.33 KB
/
web_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# Setup a Ubuntu 12.04 server install for Django
# Needs to be run as root (or with sudo)
# The ubuntu user password
PASSWORD='tiger'
uid=`id -u`
if [ $uid -ne 0 ]
then
echo 'Must be run as root'
exit
fi
# Determine if this script is being run on an EC2 image or a local VM
host='apoxz.org'
if [[ ! `hostname` =~ "^domU-" ]]
then
host=`ifconfig eth0 | awk '$1 == "inet" {split($2,a,":"); print a[2]}'`
fi
apt-get install -y git nginx monit mysql-server python-mysqldb
# Install Django
wget -O Django-1.4.tar.gz "http://www.djangoproject.com/download/1.4/tarball/"
tar xzvf Django-1.4.tar.gz
cd Django-1.4
sudo python setup.py install
# Set up nginx
echo "# the IP(s) on which node server is running
upstream app_apoxz {
server 127.0.0.1:8000;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
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/apoxz.org/static/;
}
}" > /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]
script
export HOME="/root"
export PYTHONPATH=$PYTHONPATH:/var/www/apoxz.org/apoxz/
echo $$ > /var/run/apoxz.pid
exec sudo -u ubuntu python /var/www/apoxz.org/apoxz/manage.py 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/apoxz.pid
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/apoxz.org
chown -R root:ubuntu /var/www/apoxz.org
chmod -R 775 /var/www/apoxz.org
echo "#!/bin/sh
GIT_WORK_TREE=/var/www/apoxz.org 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/apoxz.pid"
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.'