Thursday 13 April 2017

Reset debian root password through KVM

Boot into grub, select single user but do not press enter.
Press e to bring you into edit mode.
Scroll down to the kernel line, it starts with “linux /boot/vmlinuz-2.6…….”
Scroll to the end of that line and press space key once and type
init=/bin/bash
Press Crtl x to boot
Then type
mount -rw -o remount /
Now you can right to disk and change password or whatever else you want

----------------------
referece:
https://derwynd.com/2011/04/14/debian-squeeze-change-root-password/


Wednesday 12 April 2017

Deploy django on Debian with gunicorn using upstart

In my experience gunicorn is more stable and uses less memory than uwsgi to handle the same number of requests. It is also much simpler to configure and deploy.

The only issue with it is that finding good straightforward tutorial on its deployment hasn't been easy for me. The problem araise not by gunicorn but the rather convulted method of supervisor which adds more complexity to the process.

Fortunately there is the much simpler way to keep gunicorn running:‌ upstart

First you need to install pip and virtaulenv:

apt-get install python-pip
pip install virtualenv
Switch to a non-root user and create a virtualenv



su myuser 
virtualenv ~/.myenv
virtualenv ~/.myenv
activate the virtaualenv

source ~/.myenv/bin/activate

Install gunicorn while you'r on your virtualenv:

(.myenv)# pip install gunicorn


create a gunicorn.sh in  /path/to/project, like this:
#!/bin/bash
NAME="myproject"                                  # Name of the application
DJANGODIR=/path/to/project             # Django project directory
SOCKFILE=/tmp/gunicorn.sock  # we will communicte using this unix socket
USER=john                                        # the user to run as
GROUP=www-data                                     # the group to run
NUM_WORKERS=9                                     # twice the number of cpu cores
DJANGO_SETTINGS_MODULE=sai.settings             # which settings file should Django use
DJANGO_WSGI_MODULE=sai.wsgi                     # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /home/user/.virtualenv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/john/.virtualenv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --log-file=-
Note: DO NOT create /tmp/gunicorn.sock  
gunicorn will do that for you


Make sure that upstart is installed:

apt-get install upstart

Now create an upstart job in /etc/init to run gunicorn.sh

nano /etc/init/gunicorn.conf
And put in it:
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid john
setgid www-data
chdir /path/to/project
exec bash gunicorn.sh


Now you can start gunicorn like any service :

service gunicorn restart

The only remaining step is to pass request to gunicorn in nginx, by a file like this:

upstream myproject {
    server unix:///tmp/gunicorn.sock;    # the socket will be created automatically
    }
server {
#...other stuff
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host;
        if (!-f $request_filename) {
            proxy_pass http://myproject;
            break;
        }
    }
}
Note 2: If you just started upstart, you may need to  reboot to be able to service gunicorn restart.



That's it!
Restart nginx and enjoy gunicorn.