This is a set of instructions to setup a Django Nginx Gunicorn MySQL/Postgres stack on a single Amazon EC2 instance.
EC2 instance, choose Amazon AMI type - it's a centOS flavour.
ssh -i PERMSFILE ec2-user@domain
Open ports in ec2 security group:
22 (SSH)
80 (HTTP)
3306 (MYSQL) -- TESTING DB -- DANGER
8000 (Django/Gunicorn) -- TESTING
make sure everything is up to date:
sudo yum update
Install:
yum install nginx
sudo vim /etc/nginx/conf.d/arquivo.conf
conf file:
server {
listen 80;
server_name staging.arquivo.co.pt;
# no security problem here, since / is alway passed to upstream
root /srv/www/arquivo/checkout/website;
# serve directly - analogous for static/staticfiles
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8000/;
}
# what to serve if upstream is not available or crashes
error_page 500 502 503 504 /media/50x.html;
}
initscript:
sudo /sbin/chkconfig nginx on
start:
sudo service nginx start
sudo yum -y install mysql mysql-server
sudo mysqladmin -u root password 'new-password'
mysql -u root -p
mysql> create database arquivo;
[local access - safer]
mysql> GRANT ALL ON arquivo.* TO sgarcez@'localhost' IDENTIFIED BY 'sgarcez';
[remote access]
mysql> GRANT ALL ON arquivo.* TO sgarcez@'%' IDENTIFIED BY 'sgarcez';
mysql> FLUSH PRIVILEGES;
control:
sudo /sbin/chkconfig mysqld on
sudo service mysqld start
security:
[removes the test database]
mysql> DROP DATABASE test;
[Removes anonymous access]
mysql> DELETE FROM mysql.user WHERE user = ;
mysql> FLUSH PRIVILEGES;
mkdir /srv/www/arquivo
sudo chown ec2-user arquivo
git clone [email protected]:sgarcez/arquivo.co.pt.git checkout
install virtualenv:
sudo easy_install virtualenv
make a virtualenv:
cd /srv/www/arquivo
virtualenv --no-site-packages env
start it:
source env/bin/activate
install package manager:
sudo easy_install pip
probably not needed:
sudo yum install python-imaging
sudo yum install freetype-devel
sudo yum install gcc
sudo yum install python-devel
sudo yum install python26* mysql-devel
example requirements.txt in project folder:
Django==1.3.1
gunicorn==0.13.4
MySQL-python==1.2.3
install:
pip install -r requirements.txt
test Django
#to test the base setup - open port 8000 in sec group
python manage.py runserver 0.0.0.0:8000
#test gunicorn
gunicorn_django -b 0.0.0.0:8000
make gunicorn script in project folder (http://senko.net/en/django-nginx-gunicorn/)
#!/bin/bash
set -e
LOGFILE=/var/log/gunicorn/arquivo.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
# user/group to run as
USER=ec2-user
GROUP=ec2-user
cd /srv/www/arquivo/checkout/website
source ../../env/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec ../bin/gunicorn_django -w $NUM_WORKERS \
--user=$USER --group=$GROUP --log-level=debug \
--log-file=$LOGFILE 2>>$LOGFILE
# for local add to the above: --bind 0.0.0.0:8000
chmod ug+x run_gunicorn.sh
sudo chown ec2-user /var/log/gunicorn/
This controls Gunicorn
sudo easy_install supervisor
sudo vim /etc/supervisord.conf
echo_supervisord_conf > /etc/supervisord.conf
#append :
[program:arquivo]
directory = /srv/www/arquivo/checkout/website/
user = ec2-user
command = /srv/www/arquivo/checkout/website/run_gunicorn.sh
stdout_logfile = /var/log/supervisor/stout.log
stderr_logfile = /var/log/supervisor/error.log
add script to /ect/init.d/supervisord: http://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu/259230#259230
sudo chmod +x /etc/init.d/supervisord
sudo chkconfig --add supervisord
sudo /etc/init.d/supervisord status
sudo /etc/init.d/supervisord start
restart nginx:
sudo service nginx restart
http://senko.net/en/django-nginx-gunicorn/ http://davidpoblador.com/run-django-apps-using-gunicorn-and-nginx/