motorscript.com

Setting up the environment

Installing Development Tools

yum -y install vim git links #install vim and git and links text browser
yum group install "Development Tools"
yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel

Installing Python2.7 in CentOS

yum install -y python-devel
wget https://bootstrap.pypa.io/get-pip.py #get pip installer
python2.7 get-pip.py #run pip installer
pip install virtualenv #installs virtual Python environment manager

Installation and Initialization of Postgres

Installing Postgresql 9.2

yum -y install python-psycopg2 postgresql-devel postgresql-libs postgresql postgresql-server postgresql-contrib
su - postgres
cd
initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/pgsql/data'
exit
vi /var/lib/pgsql/data/pg_hba.conf #Ensure METHOD is trust
service postgresql restart
su - postgres
psql template1
ALTER USER postgres with encrypted password 'your_password';
\q
exit
vi /var/lib/pgsql/data/pg_hba.conf #Ensure METHOD is md5
service postgresql restart

Creating DB user

su postgres #switch as system postgres user
cd
createdb db_name #create db named 'db_name'
psql db_name #launch sql console for 'db_name' db
CREATE ROLE user_name WITH SUPERUSER LOGIN PASSWORD 'password';
\q
exit

Nginx

yum install nginx

OR
Write the following contents to vi /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Then:
yum -y install nginx

Set nginx to autostart:

chkconfig --add nginx
chkconfig --levels 235 nginx on

Supervisord

Install supervisor
pip install supervisor

Create a sample config file with
echo_supervisord_conf > /etc/supervisord.conf and edit it.

Or create yourself one
vi /etc/supervisord.conf

[program:appname]
command = /home/user/env/bin/gunicorn app.wsgi:application -c /home/user/conf/gunicorn.conf.py
directory = /home/user/app/
user = user
environment=PATH= /home/user/env/bin

[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/home/user/logs/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisor/conf.d/*.conf

Create supervisord conf directory mkdir -p /etc/supervisor/conf.d/

Download and save supervisord init script:
wget -O /etc/init.d/supervisord https://bitbucket.org/kencochrane/django-gunicorn-nginx-supervisord-bootstrap/raw/695880c118c60a17b90f6a4a654d2d16fcc2ee12/server/init.d/supervisord

Add executable permission
chmod a+x /etc/init.d/supervisord

Start supervisord service
service supervisord start

Set it to autostart

chkconfig --add supervisord
chkconfig supervisord on

The Application

The Application Environment

cd #goto user home directory
virtualenv env #Creates new virtual environment
source env/bin/activate #Enter the virtual environment

Installing Application and its Dependencies

cd #goto user home directory
source env/bin/activate # Enter the virtual environment
git clone git@github.com:username/reponame.git app # git clone the repo
cd app # cd to project dir
pip install -r requirements/production.txt # install required Python packages
python manage.py migrate # run migrations
python manage.py collectstatic

You might want to add the last three commands in your post receive hook.

Configuring and Testing Application

./manage.py runserver # Test application
links http://127.0.0.1:8000 # Test run from browser

Gunicorn

If gunicorn isn’t already installed inside the virtual environment:
pip install gunicorn

Create gunicorn config file vi /home/user/conf/gunicorn.conf.py

import multiprocessing

bind = "unix:/home/user/conf/gunicorn.sock"
workers = 3
errorlog = '/home/user/logs/gunicorn.error.log'
#accesslog = '/home/user/logs/gunicorn.access.log'
#loglevel = 'debug'
proc_name = 'gunicorn_appname'

From the app directory, you may test if it runs successfully with:
gunicorn app.wsgi:application -c /home/user/conf/gunicorn.conf.py

Nginx

Nginx Config file:
vi /home/user/conf/nginx.conf

upstream app_name{
    server unix:/home/user/conf/gunicorn.sock;
}

server {
    listen 216.224.179.43:80;
    server_name  www.site.com;
    return       301 http://site.com$request_uri;
}

server {
    listen 216.224.179.43:80;
    server_name site.com;
    access_log /home/user/logs/nginx.access.log;
    error_log /home/user/logs/nginx.error.log;
    index index.html index.htm;

location /robots.txt {
    alias /home/user/static/robots.txt;
}

location /favicon.ico {
    alias /home/user/static/img/favicon.ico;
}

location ~ ^/(media|static)/  {
    root    /home/user;
    expires 30d;
}

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    if (!-f $request_filename) {
        proxy_pass http://app_name;
        break;
    }
}

# this prevents hidden files (beginning with a period) from being served
location ~ /\. { access_log off; log_not_found off; deny all; }

}

Soft link the configuration to nginx conf dir: ln -s /home/user/conf/nginx.conf /etc/nginx/conf.d/app_name.conf

Restart nginx:
service nginx restart

Supervisord

vi /home/user/conf/supervisor.conf

[program:app_name]
command = /home/user/env/bin/gunicorn app_name.wsgi:application -c /home/user/conf/gunicorn.conf.py
directory = /home/user/app/
user = user
environment=PATH= /home/user/env/bin

Soft link to supervisord conf dir: ln -s /home/user/conf/supervisor.conf /etc/supervisor/conf.d/app_name.conf

Allow users to control the process with sudo visudo
and Append

user ALL = (root) NOPASSWD:/usr/bin/supervisorctl start app_name
user ALL = (root) NOPASSWD:/usr/bin/supervisorctl stop app_name
user ALL = (root) NOPASSWD:/usr/bin/supervisorctl restart app_name
user ALL = (root) NOPASSWD:/usr/bin/supervisorctl status app_name

supervisorctl reload

Now you can control your program with supervisorctl

sudo supervisorctl start appname
sudo supervisorctl status appname
sudo supervisorctl stop appname
sudo supervisorctl restart appname