Nginx + Python web. py and Django framework Environment on Linux

Source: Internet
Author: User
This article mainly introduces the Nginx + Python web. py and Django framework Environment on Linux systems. fastcgi is used as the connection in this article. If you need it, refer 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 find. -name "* .tar.gz" | xargs-IX tar zxvf X #==================== ========================================================== ==========## compile nginxcd $ src_nginx_dirchmod 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_dirmake & make install

2. Configure nginx
Add under server Configuration

Location/{# both methods are acceptable, except that the spawn-cgi startup method is different # fastcgi_pass 127.0.0.1: 9002; fastcgi_pass unix: webpy. sock; required REQUEST_METHOD $ request_method; required QUERY_STRING $ query_string; required CONTENT_TYPE $ response; required CONTENT_LENGTH $ content_length; required GATEWAY_INTERFACE CGI/1.1; required SERVER_SOFTWARE nginx/$ nginx_version; required bytes $ response; incluremote_port $ REMOTE_PORT; fastcgi_param SERVER_ADDR $ server_addr; incluserver_port $ SERVER_PORT; incluserver_name $ SERVER_NAME; incluserver_protocol $ SERVER_PROTOCOL; incluscript_filename $ scheme; inclupath_info $ scheme ;}

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

3. Install fastcgi dependency
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`

Start nginx
Start:

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

Stop:

kill -QUIT `cat ../logs/nginx.pid`

Reload the Configuration:

./nginx -t -c `pwd`/../conf/nginx.confkill -HUP `cat ../logs/nginx.pid`

The django background interface is displayed successfully:
Ppppppppppppppppppp1

5. Deploy web. py
Install dependency
Spawn-cgi
Flup
Configure nginx
Add under server Configuration

Location/{# both methods are acceptable, except that the spawn-cgi startup method is different # fastcgi_pass 127.0.0.1: 9002; fastcgi_pass unix: webpy. sock; required REQUEST_METHOD $ request_method; required QUERY_STRING $ query_string; required CONTENT_TYPE $ response; required CONTENT_LENGTH $ content_length; required GATEWAY_INTERFACE CGI/1.1; required SERVER_SOFTWARE nginx/$ nginx_version; required bytes $ response; incluremote_port $ REMOTE_PORT; fastcgi_param SERVER_ADDR $ server_addr; incluserver_port $ SERVER_PORT; incluserver_name $ SERVER_NAME; incluserver_protocol $ SERVER_PROTOCOL; incluscript_filename $ scheme; inclupath_info $ scheme ;}

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

. 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.pid`

Start nginx
Add to rc. local and start automatically

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

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.