How to deploy a Python Django application using FastCGI

Source: Internet
Author: User
Tags unix domain socket
This article describes how to deploy a Django application using FastCGI. FastCGI is also the most widely used module for Python framework and server connection, if you need it, you can refer to it as an alternative to the mod_python module. you can consider using the mod_wsgi module. The development time of this module is closer than that of mod_python, it has been used in the Django community. A complete overview is beyond the scope of this book. you can view more information from the official Django document.
Deploy Django applications using FastCGI

Although building a Django environment using Apache and mod_python is the most robust, FastCGI is the only option on many virtual host platforms.

In addition, FastCGI provides superior security and performance in many cases than mod_python. FastCGI is more lightweight than Apache for small websites.
FastCGI introduction

How can an external application effectively explain dynamic page requests on the WEB server? The answer is FastCGI! The procedure is described as follows:

Like mod_python, FastCGI also returns dynamic information for customer requests resident in the memory, and avoids the time spent starting a process like traditional CGI. However, unlike mod_python, it does not run in the same process of the web server as a module, but has its own independent process.

Why should I run code in an independent process?

In the traditional mode, several script languages (such as PHP, Python/mod_python, and Perl/mod_perl) are embedded into Apache using mod ), they all use the apache extension module to embed themselves into the Apache process.

Every Apache process is a copy of the Apache engine. it fully includes all the functional features of Apache (even those that are not good for Django ). FastCGI is different. it only gets necessary things such as Python and Django into the memory.

According to the characteristics of FastCGI, we can see that FastCGI processes and Web server processes run under different user permissions. For a system shared by multiple users, this feature is very beneficial to security, because you can securely share and reuse code.

If you want your Django to run in FastCGI mode, you must install the Python library flup, which is used to process FastCGI. Many users complain that the release of flup is too long to be updated. Actually, they are not. they have been working hard, but they have not been released.
Run your FastCGI server

FastCGI runs as a client/server. in many cases, you have to start FastCGI service processes on your own. Web servers (such as Apache and lighttpd) interact with your Django-FastCGI process only when there are dynamic page access requests. Because Fast-CGI has been resident in the memory, it responds very quickly.

Record

If used on a VM, you may be forced to use the Web server-managed FastCGI process. In this case, see the section "Running Django in an Apache shared host" below.

The web server interacts with the FastCGI process in two ways: using Unix domain socket (named pipe in win32) or TCP socket. which one is used depends on your preference. However, if the TCP socket is poor, some permission problems may occur. What you choose is a manner of preference; a TCP socket is usually easier due to permissions issues.

Start your server project, first go to your project directory (where your manage. py file is located), and then run the manage. py runfcgi command:

./manage.py runfcgi [options]

To learn how to use runfcgi, enter the manage. py runfcgi help command.

You can specify a socket or both host and port. To create a Web server, you only need to direct the server to the socket or host/port you have determined when starting the FastCGI server.

Example:

Run a thread server on the TCP port:

./manage.py runfcgi method=threaded host=127.0.0.1 port=3033

Run the prefork server on a Unix socket:

./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid

Startup, but not as a background process (convenient during debugging ):

./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock

Stop FastCGI's itinerary

If your FastCGI is running on the frontend, press Ctrl + C to stop the process conveniently. But if it is running in the background, you need to use the kill command of Unix to kill it. However, when you are processing background processes, you need to put them into Unix kill commands.

If you specify the pidfile option in manage. py runfcgi, you can kill the FastCGI background process as follows:

kill `cat $PIDFILE`

$ PIDFILE is the one you specified in pidfile.

You can use the following script to conveniently restart the FastCGI daemon in Unix:

#!/bin/bash# Replace these three settings.PROJDIR="/home/user/myproject"PIDFILE="$PROJDIR/mysite.pid"SOCKET="$PROJDIR/mysite.sock"cd $PROJDIRif [ -f $PIDFILE ]; then kill `cat -- $PIDFILE` rm -f -- $PIDFILEfi

exec /usr/bin/env -  PYTHONPATH="../python:.."  ./manage.py runfcgi socket=$SOCKET pidfile=$PIDFIL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.