Ext.: Apache+fastcgi+django

Source: Internet
Author: User
Tags touch command unix domain socket web hosting

07 The author of the article posted, see how old way of running is still in use.


To install Flup (http://www.saddi.com/software/flup/) First,
This is a library of Python processing FastCGI.

FACTCGI uses the C/S model to run a process independently. Web server (Apache, httpd,..) when a request needs to be processed Communicate directly with the factcgi process.

The Web server can be connected to FastCGI server in two ways:
1. Unix domain sockets (or Win32 "Named pipes")
2. TCP socket
Typically, TCP sockets are simpler because permissions issues are better configured.

How to start the FACTCGI server:
To the project directory, execute:
./manage.py runfcgi [Options]
If you want to see Help:
./manage.py runfcgi Help

In the options, you need to specify a socket or combination of host and port, so that when you start Web server, you can connect to the FACTCGI server by using this information.


In a TCP port, run a server in a thread:
./manage.py runfcgi method=threaded host= port=3033

Run a preforked server on the Unix domain socket:
./manage.py runfcgi method=prefork Socket=/home/user/mysite.sock pidfile=django.pid

Do not execute as a background process (for debugging purposes):
./manage.py runfcgi Daemonize=false Socket=/tmp/mysite.sock

How to stop the FastCGI process in the background:
1. If the Pidfile attribute is specified, you can do this: Kill ' cat $PIDFILE '. Where $PIDFILE is the pidfile option you specified.

To restart the background factcgi process on Unix, execute the following shell script:


# Replace these three settings.
Projdir= "/home/user/myproject"
pidfile= "$PROJDIR/mysite.pid"
socket= "$PROJDIR/mysite.sock"

If [-f $PIDFILE]; Then
Kill ' cat--$PIDFILE '

Pythonpath= ". /python: "\
./manage.py runfcgi socket= $SOCKET pidfile= $PIDFILE

Apache settings:
1. Need to install the mod_fastcgi
2. Edit httpd.conf:
(1) Use fastcgiexternalserver to point to the location of the FastCGI server
The socket can also be specified with the host + port. Example:

# Connect to FastCGI via a socket/named pipe.

# Connect to FastCGI via a TCP host/port.

Regardless of the above situation,/home/user/public_html/mysite.fcgi This file does not need to exist. Its role indicates that the WEB server as a URL indicates which requests need to be FastCGI to be processed.

(2) Use Mod_rewrite to assign URLs that need to be processed to FastCGI.

ServerName example.com
Rewriteengine on
Rewriterule ^/(media.*) $/$1 [qsa,l]
Rewriterule ^/(. *) $/mysite.fcgi/$1 [qsa,l]

The rewritten URL indicated here is the one in the fastcgiexternalserver above.
The above configuration will not start with/media/, or a request to a nonexistent file, to FastCGI.

LIGHTTPD Configuration

LIGHTTPD Natural support for FastCGI.
First make sure that your mod_fastcgi is in the module list, and after Mod_rewrite and Mod_access, before Mod_accesslog.
In order to serve admin media, you may also need to mod_alias.
In the LIGHTTPD configuration file, add the following paragraph:

Server.document-root = "/home/user/public_html"
Fastcgi.server = (
    "/mysite.fcgi" = (
        "Main" = (
             # Use Host/port instead of socket for TCP fastcgi
         ;    # "Host" = "",
             # "Port" = 3033,
            "socket" = "/ Home/user/mysite.sock ",
           " check-local "= > "Disable",
Alias.url = (
    "/media/" = "/home/user/django/contrib/admin/media/",

Url.rewrite-once = (
"^ (/media.*) $" = "$",
"^/favicon\.ico$" = "/media/favicon.ico",
"^ (/.*) $" = "/mysite.fcgi$1",

You can also run multiple Django on a lighttp server. Just specify a separate FastCGI host for each application.

How to run Django/apache on a shared web hosting:

Edit. Htaccess:
AddHandler Fastcgi-script. fcgi
Rewriteengine on
Rewriterule ^ (. *) $ mysite.fcgi/$1 [qsa,l]

Then write a mysite.fcgi executable script:

Import sys, OS

# ADD a custom Python path.
Sys.path.insert (0, "/home/user/python")

# Switch to the directory of your project. (Optional.)
# Os.chdir ("/home/user/myproject")

# Set the django_settings_module environment variable.
os.environ[' django_settings_module '] = "myproject.settings"

From django.core.servers.fastcgi import runfastcgi
runfastcgi (method= "threaded", daemonize= "false")

After updating the code, you can instruct Apache to restart the Django program by re-uploading the mysite.fcgi file.
If you have Shell permissions, you can change the timestamp directly with the Touch command:
Touch mysite.fcgi

Ext.: Apache+fastcgi+django

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.