Installation and configuration of linux + nginx + python + fastcgi and deployment of django + web. py

Source: Internet
Author: User
Tags chmod openssl

Deployment summary of nginx + python + fastcgi in linux (django version)

Note: Although this article has successfully set up an instance for django to run fastcgi, many problems have been found during actual operation, such as program execution exceptions and process exit after each request. It may be a problem with my machine or a bug in the program itself. If you are using it to build an Internet environment, Please test it a lot.

1. Compile nginx

I bought a copy of "practical nginx-replacing Apache high-performance server" on the Internet, which is relatively simple to write. It mainly involves configuration, but it is exactly what I need now. Because it needs to support https and rewrite, in addition to nginx source code, but also download the openssl-0.9.8r.tar.gz and pcre-8.12.tar.gz, put them and nginx-1.0.4.tar.gz in the same directory.
To facilitate compilation, I wrote a script with the following code:

#! /Bin/bash
 
#===================================================== ==============================================
# Absolute directory of the script
Abs_path (){
Local path = $1
Local basename = $ (basename $ path)
Local dirname = $ (dirname $ path)
Cd $ dirname
If [-h $ basename]; then
Path = $ (readlink $ basename)
Abs_path $ path
Else
Pwd
Fi
}
 
#===================================================== ==============================================
# Dependent Directory
Src_base_dir = $ (abs_path $0)
Src_openssl_dir = $ src_base_dir '/openssl-0.9.8r'
Src_pcre_dir = $ src_base_dir '/pcre-8.12'
Src_nginx_dir = $ src_base_dir '/nginx-1.0.4'
 
#===================================================== ==============================================
# Target Directory
Dest_base_dir = $ src_base_dir '/release'
Dest_nginx_dir = $ dest_base_dir '/nginx'
 
#===================================================== ==============================================
Unzip all tar.gz files
Find.-name "* .tar.gz" | xargs-IX tar zxvf X
 
#===================================================== ==============================================
# Compile nginx
Cd $ src_nginx_dir
Chmod u + x./configure
./Configure -- with-http_stub_status_module -- with-http_ssl_module -- with-openssl = $ src_openssl_dir -- with-pcre = $ src_pcre_dir -- prefix = $ dest_nginx_dir
Make & make install

After compilation, we need to configure nginx.

2. Configure nginx

Add under server configuration

Location /{
# Fastcgi_pass 127.0.0.1: 9001;
Fastcgi_pass unix: django. sock;
 
Fastcgi_param PATH_INFO $ fastcgi_script_name;
Fastcgi_param REQUEST_METHOD $ request_method;
Fastcgi_param QUERY_STRING $ query_string;
Fastcgi_param CONTENT_TYPE $ content_type;
Fastcgi_param CONTENT_LENGTH $ content_length;
Fastcgi_pass_header Authorization;
Fastcgi_intercept_errors off;
Fastcgi_param SERVER_PROTOCOL $ server_protocol;
Fastcgi_param SERVER_PORT $ server_port;
Fastcgi_param SERVER_NAME $ server_name;
}
 
Location/admin_media /{
Alias/usr/local/lib/python2.7/site-packages/django/contrib/admin/media /;
Break;
}
 
Location/site_media /{
Alias/home/dantezhu/htdocs/ngx_django/media /;
Break;
}

The three location configurations solve the problems of communication with the python process, style storage at the django backend management end, and website style storage. According to the apache configuration, it is easy to understand.

WSGIPythonEggs/tmp
<VirtualHost *>
ServerName fuload.qq.com
WSGIScriptAlias // home/dantezhu/htdocs/fuload/conf/setting. wsgi
<Directory/>
Options FollowSymLinks
AllowOverride
Order allow, deny
Allow from all
</Directory>
<Directory "/home/dantezhu/htdocs/fuload/mysite">
Order Deny, Allow
Deny from all
</Directory>
Alias/admin_media "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"
<Directory "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media">
Order allow, deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
 
# AliasMatch/site_media/(. *. (css | gif | png | jpg | jpeg)/home/dantezhu/htdocs/fuload/media/$1
Alias/site_media/home/dantezhu/htdocs/fuload/media/
<Directory "/home/dantezhu/htdocs/fuload/media/">
Order allow, deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>

3. Install fastcgi dependencies

You need to download the installation at http://trac.saddi.com/flupand fastcgiwill be able to start normally.

4. Start django

We will not talk about the process of creating a django project. We will only list the start/stop commands:

Start:

# Python manage. py runfcgi daemonize = true pidfile = 'pwd'/django. pid host = 127.0.0.1 port = 9001 maxrequests = 1 &
Python manage. py runfcgi daemonize = true pidfile = 'pwd'/django. pid socket =/home/dantezhu/nginx/sbin/django. sock maxrequests = 1 &

Stop:

Kill-9 'cat django. pid'

5. Start nginx

Start:

./Nginx-p/home/dantezhu/nginx/

Stop:

Kill-QUIT 'cat ../logs/nginx. Pi'

Reload the configuration:

./Nginx-t-c 'pwd'/../conf/nginx. conf kill-HUP 'cat ../logs/nginx. Pi'

./Nginx-t-c 'pwd'/../conf/nginx. conf
Kill-HUP 'cat ../logs/nginx. Pi'

The django background interface is displayed successfully:




Deployment summary of nginx + python + fastcgi in linux (web. py)

In fact, the official website on web. py has already said more clearly, the original article is as follows: http://webpy.org/cookbook/fastcgi-nginx

Here we will focus on some areas where the original documents are not taken care.

1. Install dependencies

Spawn-cgi
Flup

2. Configure nginx

Add under server configuration

Location /{
# Either method works, except that the spawn-cgi startup method is different.
# Fastcgi_pass 127.0.0.1: 9002;
Fastcgi_pass unix: webpy. sock;
 
Fastcgi_param REQUEST_METHOD $ request_method;
Fastcgi_param QUERY_STRING $ query_string;
Fastcgi_param CONTENT_TYPE $ content_type;
Fastcgi_param CONTENT_LENGTH $ content_length;
Fastcgi_param GATEWAY_INTERFACE CGI/1.1;
Fastcgi_param SERVER_SOFTWARE nginx/$ nginx_version;
Fastcgi_param REMOTE_ADDR $ remote_addr;
Fastcgi_param REMOTE_PORT $ remote_port;
Fastcgi_param SERVER_ADDR $ server_addr;
Fastcgi_param SERVER_PORT $ server_port;
Fastcgi_param SERVER_NAME $ server_name;
Fastcgi_param SERVER_PROTOCOL $ server_protocol;
Fastcgi_param SCRIPT_FILENAME $ fastcgi_script_name;
Fastcgi_param PATH_INFO $ fastcgi_script_name;
}

3. A simple index. py

#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
 
Import web
 
Urls = ("/. *", "hello ")
App = web. application (urls, globals ())
 
Class hello:
Def GET (self ):
Return 'Hello, world! '
 
If _ name _ = "_ main __":
Web. wsgi. runwsgi = lambda func, addr = None: web. wsgi. runfcgi (func, addr)
App. run ()

And execute:

Chmod + x index. py

4. Start web. py

Start:

# Spawn-fcgi-P 'pwd'/webpy. pid-f/home/dantezhu/htdocs/ngx_web/index. py-a 127.0.0.1-p 9002 &
Spawn-fcgi-P 'pwd'/webpy. pid-f/home/dantezhu/htdocs/ngx_web/index. py-s/home/dantezhu/nginx/sbin/webpy. sock &

Stop:

Kill-9 'cat webpy. Pi'

5. Start nginx


Like the previous article, I will not go into details here.

6. Add to rc. local and start automatically

/Home/dantezhu/nginx/sbin/start. sh
Sudo-u dantezhu/home/dantezhu/htdocs/ngx_django/mysite/start. sh
Sudo-u dantezhu/home/dantezhu/htdocs/ngx_web/start. sh

OK, that's it ~

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.