background preloader

Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL - Michał Karzyński

Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL - Michał Karzyński
Django is an efficient, versatile and dynamically evolving web application development framework. When Django initially gained popularity, the recommended setup for running Django applications was based around Apache with mod_wsgi. The art of running Django advanced and these days the recommended configuration is more efficient and resilient, but also more complex and includes such tools as: Nginx, Gunicorn, virtualenv, supervisord and PostgreSQL. In this text I will explain how to combine all of these components into a Django server running on Linux. Prerequisites I assume you have a server available on which you have root privileges. If you don’t have a server to play with, I would recommend the inexpensive VPS servers offered by Digital Ocean. I’m also assuming you configured your DNS to point a domain at the server’s IP. Update your system Let’s get started by making sure our system is up to date. $ sudo aptitude update $ sudo aptitude upgrade PostgreSQL Application user Gunicorn Nginx Nginx Related:  ProgrammingStuff

Setting up an asynchronous task queue for Django using Celery and Redis - Michał Karzyński Celery is a powerful, production-ready asynchronous job queue, which allows you to run time-consuming Python functions in the background. A Celery powered application can respond to user requests quickly, while long-running tasks are passed onto the queue. In this article we will demonstrate how to add Celery to a Django application using Redis. Celery uses a broker to pass messages between your application and Celery worker processes. Prerequisites In this article we will add Celery to a Django application running in a Python virtualenv. This article was tested on a server running Debian 7, so everything should also work on an Ubuntu server or other Debian-based distribution. Update your system Let’s get started by making sure your system is up to date. $ sudo aptitude update $ sudo aptitude upgrade Install Redis The first piece of software we’ll install is Redis. $ sudo aptitude install redis-server $ redis-server --version Redis server version 2.4.14 (00000000:0) $ redis-cli ping PONG

How to serve static files Django itself doesn’t serve static (media) files, such as images, style sheets, or video. It leaves that job to whichever Web server you choose. The reasoning here is that standard Web servers, such as Apache, lighttpd and Cherokee, are much more fine-tuned at serving static files than a Web application framework. With that said, Django does support static files during development. You can use the django.views.static.serve() view to serve media files. See also If you just need to serve the admin media from a nonstandard location, see the --adminmedia parameter to runserver. The big, fat disclaimer Using this method is inefficient and insecure. For information on serving static files in an Apache production environment, see the Django mod_python documentation. How to do it Here’s the formal definition of the serve() view: def serve(request, path, document_root, show_indexes=False) To use it, just put this in your URLconf: Given the above URLconf: STATIC_DOC_ROOT = '/path/to/media' Directory listings

Deploy Django on Apache with Virtualenv and mod_wsgi | The Code Ship I have recently got into Django and built a small project with it which is this blog. Then came the time for me to deploy the project on an unmanaged VPS, the process went fine yet with a few problems here and there due to the lack of a definitive guide to deployment. So I ended up checking several sources to have everything work the way I wanted. In this post, I'll be sharing the whole process with you in detail and what will hopefully be helpful to the fellow newcomers. Prerequisites In this tutorial we will assume that we have a fresh Ubuntu server installation with Python already installed and nothing else. Installing Pip Pip is a tool for installing and managing Python packages,basically a replacement for easy_install, many Python developers should be already familiar with it and have it already. $ sudo apt-get install python-pip python-dev build-essential Then upgrade to latest version $ sudo pip install --upgrade pip Setting up virtualenv $ pip install virtualenvwrapper Upload files

How to use Django with Gunicorn Gunicorn (‘Green Unicorn’) is a pure-Python WSGI server for UNIX. It has no dependencies and is easy to install and use. There are two ways to use Gunicorn with Django. Installing Gunicorn Installing gunicorn is as easy as sudo pip install gunicorn. Running Django in Gunicorn as a generic WSGI application When Gunicorn is installed, a gunicorn command is available which starts the Gunicorn server process. gunicorn [OPTIONS] APP_MODULE Where APP_MODULE is of the pattern MODULE_NAME:VARIABLE_NAME. So for a typical Django project, invoking gunicorn would look like: gunicorn myproject.wsgi:application (This requires that your project be on the Python path; the simplest way to ensure that is to run this command from the same directory as your manage.py file.) Using Gunicorn’s Django integration Note If you are using Django 1.4 or newer, it’s highly recommended to simply run your application with the WSGI interface using the gunicorn command as described above. Having trouble?

Alex Raichev > Blog > Adding Celery with RabbitMQ to a Django project on Webfaction I recently added Celery with RabbitMQ to one of my Django projects on Webfaction. Along the way, i read lots of documentation, encountered difficulties, and got advice from the helpful staff at Webfaction. What worked for me in the end are the instructions below. May they might work for you too and save you some pain! I'll assume that you've already added Celery with RabbitMQ to your Django project in your development environment (encapsulated within a virtual environment) and have that working as desired. Now, let's add Celery with RabbitMQ to your production environment (encapsulated within a virtual environment) on Webfaction. Preparation Ssh into your Webfaction server. Install and configure Erlang From your Webfaction control panel, create a new application with App category: Custom, App type: Custom app (listening on port). Test Erlang Install and configure RabbitMQ Test RabbitMQ Install and configure Celery Test Celery That's it. Todo

Data types Strings Strings are the most basic kind of Redis value. Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance a JPEG image or a serialized Ruby object. A String value can be at max 512 Megabytes in length. You can do a number of interesting things using strings in Redis, for instance you can: Use Strings as atomic counters using commands in the INCR family: INCR, DECR, INCRBY. Check all the available string commands for more information. Lists Redis Lists are simply lists of strings, sorted by insertion order. The LPUSH command inserts a new element on the head, while RPUSH inserts a new element on the tail. Some example of list operations and resulting lists: LPUSH mylist a # now the list is "a" LPUSH mylist b # now the list is "b","a" RPUSH mylist c # now the list is "b","a","c" (RPUSH was used this time) The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list). Sets Hashes Sorted sets

Encoders and Decoders - Base64, URL, IDN, CP, UU, XML, Bin-Hex Django database migration tool: south, explained | DjangoPro If you are using Django for production level application, you will need to use south. Requirement changes, and therefore your data model will change over time. South is a great tool, but it is complicated. You do not want to make a mistake when migrating an application in production. This is a detail look at how it interacts with your application so that you can understand and use it better. South interacts (reads and writes) with four different items in an application. models.py — South reads this to determine your current data model.migrations/*.py — South creates a sub directory inside your app, and creates for you a migration file for each database migration generation. Different commands in south interacts with these items differently: Let is start with a normal database schema migration, from generation N to generation N+1. 1. This diagram shows what are the inputs and outputs to each step. Let’s add the very first step when using south on a new applicatoin. 1. 1. 1. Diagram: . .

Related: