Quantcast
Channel: LinOxide
Viewing all articles
Browse latest Browse all 1287

How to Install Django 1.9 with Gunicorn and Nginx on FreeBSD 10.2

$
0
0

Django is web framework based on python, maintain by Django Software Foundation. It is free and opensource python framework for building a macro web applications. Django is powerful, make your develpment process is so fast, make your application simple and useful. Make your developer easy to maintain and deploy the project. Django is a high-level python framework with MVC (Model-View-Controller) concept, used by big company like mozilla, pinterest, disquss, instagram, bitbucket etc. In this tutorial we will guide about django installation. We will install django 1.9 with gunicorn, supervisord and nginx as the web server. Nginx running on port 80 and django framework running under gunicorn with gunicorn sock file.

Prerequisite :

  • FreeBSD 10.2 - 64bit.
  • Root privileges.

Step 1 - Update FreeBSD Repository

Log in to your freebsd server and update the repository with 'freebsd-update' command :

freebsd-update fetch
freebsd-update install

Step 2 - Install Python, SQLite3 and Pip

In this step we will install python 2.7 with pip. If you want to use python3, please use MySQL or PostgreSQL as your django database. We will install python and pip from freebsd repository with command below :

pkg install python
pkg install py27-pip

And update the pip to latest version with command :

pip install --upgrade pip

Next, install sqlite3 databse and the python module py27-sqlite3 with pkg command :

pkg install sqlite3
pkg install py27-sqlite3

Python, pip and SQLite3 installed on the freebsd server.

Step 3 - Install Django on Virtual Environment

We will install and deploy django with the normal user called 'vagrant', not as root. We will install django under virtualenv, it is virtual python environment builder, tool to create the isolated python environment. Virtualenv is available on PyPI repository, we can install it with pip command :

pip install virtualenv

Next switch to the user 'vagrant' :

su - vagrant

Create new virtual environment called 'myenv' with python2.7 as the version of it. We can create it with virtualenv command below :

mkvirtualenv --python=python2.7 myenv

It will create new directory called 'myenv', go to the directory and activate the virtual environment that has been created before :

cd myenv
source bin/activate

And this our time to install django 1.9 and a gunicorn, install all it with pip command under the virtualenv :

pip install django==1.9
pip install gunicorn

Django and Gunicorn is installed, we can check it with pip command :

pip list

Django (1.9)
gunicorn (19.4.1)

Next, create new sample project with django. We will create new project named 'myproject' with django-admin command :

django-admin startproject myproject

That command will create new directory 'myproject', go to the directory and test the django installation by running manage.py file :

cd myproject/
python manage.py runserver

runserver = start our django project with port 8000.

Django_Virtualenv

If there is no error, then proceed with the next step by editing setting.py file inside django project directory :

nano -c myproject/settings.py

In the end of the line add configuration below :

STATIC_ROOT = '/home/vagrant/myenv/myproject/static/'
STATIC_URL = '/static/'

MEDIA_ROOT = '/home/vagrant/myenv/myproject/media/'
MEDIA_URL = '/media/'

Save and exit.

And generate the static file by running command below :

python manage.py collectstatic

collectstatic used to manage the static files, it will copied the django static file to directory 'static' on myproject dir.

Django_Collectstatic

Django is configured, and the last we need to configure the gunicorn. Go to the 'myenv' directory and activate the environment, then create new file on bin directory :

cd /home/vagrant/myenv/
source bin/activate
nano -c bin/gunicorn_start

Paste configuration below :

#!/bin/bash

NAME="myproject"                                             # Django Project Name
DJANGODIR=/home/vagrant/myenv/myproject                        # Django Project Directory
SOCKFILE=/home/vagrant/myenv/myproject/run/gunicorn.sock     # Gunicorn Sock File
USER=vagrant                                                # Django Project Running under user vagrant
GROUP=vagrant                                                # Django Project Running under group vagrant
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=myproject.settings                     # change 'myproject' with your project name
DJANGO_WSGI_MODULE=myproject.wsgi                             # change 'myproject' with your project name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source ../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 ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-

Save and exit.

And make the script executable :

chmod u+x bin/gunicorn_start

Django is configured with gunicorn, django will run under gunicorn sock file.

Note : If you want to exit from the virtualenv, you can use command "deactivate".

Step 4 - Install Supervisord

Supervisor is a process control on Linux and Unix operating system that allow the users to monitor and control the number of process. In this tutorial we need a py-supervisor to control the gunicorn that running with our django project.

Install py-supervisor from freebsd repository with pkg command :

pkg install py-supervisor

If it's done, add supervisor to the boot time with sysrc command, and then start it:

sysrc supervisord_enable=yes
service supervisord start

Supervisor is started, but we need to add our application to it. We can do it by editing the supervisor configuration file, go to the directory and edit the file 'supervisord.conf' with nano :

cd /usr/local/etc/
nano -c supervisord.conf

In the end of the line, add our app configuration below :

[program:myproject]
command = sh /home/vagrant/myenv/bin/gunicorn_start
user = vagrant
stdout_logfile = /home/vagrant/myenv/logs/gunicorn_supervisor.log
redirect_stderr = true
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

Save and exit.

Now we must create new directory for the gunicorn log file with user vagrant  :

mkdir -p /home/vagrant/myenv/logs/
touch /home/vagrant/myenv/logs/gunicorn_supervisor.log

Restart the supervisor and check our application is running :

service supervisord restart
supervisorctl status

Supervisord

Step 5 - Install Nginx

We will use nginx as front end the server and run on port 80. If someone access from the browsers, the connection will be passed to gunicorn that running with the sock file.

Install nginx from the freebsd repository with pkg command :

pkg install nginx

Next configure the virtualhost for our django project. Go to the nginx configuration directory and create new directory called vhost there :

cd /usr/local/etc/nginx/
mkdir -p vhost/

Go to vhost directory and create new file conafiguration 'mydjango.conf' :

cd vhost/
nano -c mydjango.conf

Paste virtualhost configuration below :

upstream myproject_server {
server unix:/home/vagrant/myenv/myproject/run/gunicorn.sock fail_timeout=0;
}

server {

listen   80;
server_name www.djangofreebsd.com;

client_max_body_size 4G;

access_log /home/vagrant/myenv/logs/nginx-access.log;
error_log /home/vagrant/myenv/logs/nginx-error.log;

location /static/ {
alias   /home/vagrant/myenv/myproject/static/;
}

location /media/ {
alias   /home/vagrant/myenv/myproject/media/;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://myproject_server;
break;
}
}

# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/vagrant/myenv/myproject/static/;
}
}

Save and exit.

Now we must add the virtualhost configuration to the nginx.conf. Go back to the nginx directory, edit the file nginx.conf with nano :

cd /usr/local/etc/nginx/
nano -c nginx.conf

Before the end of the line, add line below :

include vhost/*.conf;

Save and exit.

Test the nginx configuration, if there is no error, add nginx to the boot time :

nginx -t
sysrc nginx_enable=yes

And the last, restart supervisord and start nginx :

service supervisord restart
service nginx start

Now we can access our django applications with the browser, in this tutorial we use domain www.mydjango.co.

Django_with Nginx_Done

Django admin page.

Djang_Admin

Django 1.9 and gunicorn with supervisor and nginx is done.

Conclusion

Django is web application framework based on python with MVC (Model-View-Controller) concept. Django is the best solutions for you if you want to build a macro web application. It is fast and simple, free and opensource and maintained by Django Software Foundation. Django can be up with standalone, but we can use apache or nginx too as the front end of the django project.

The post How to Install Django 1.9 with Gunicorn and Nginx on FreeBSD 10.2 appeared first on LinOxide.


Viewing all articles
Browse latest Browse all 1287

Trending Articles