Nginx+python web.py and Django Framework environment on Linux systems

Source: Internet
Author: User
1. Compiling Nginx
On the internet bought a "real nginx-to replace Apache high-performance Server", write relatively shallow, mainly some configuration aspects of things, but it is what I need now. Because of the need to support HTTPS and rewrite, so in addition to Nginx source code, but also downloaded 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.
In order to facilitate the compilation, the author wrote a script, the code is as follows:

#!/bin/bash #============================================================================= #脚本所在绝对目录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} #======================================================== ===================== #依赖的目录src_base_dir =$ (Abs_path $) 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 ' #======================================= ====================================== #目标的目录dest_base_dir = $src _base_dir '/release ' dest_nginx_dir= $dest _base_ Dir '/nginx ' #============================================================================= #把所有的tar. GZ extract Find. -name "*.tar.gz" | Xargs-ix tar zxvf X #============================================================================= #编译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 item

Location/{#这两种方法都可以, except that the method of spawn-cgi startup 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;}

The 3 location configurations here address the issue of communication with Python processes, the Django backend style store, and site style storage. It's easy to see the Apache configuration.

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. Installing fastcgi Dependencies
Need to download the installation to Http://trac.saddi.com/flup, then the fastcgi can start normally.

4. Start the Django
The process of creating a Django project we will not say, only the start/Stop commands are listed:
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 it:

Kill-9 ' Cat django.pid '

Start Nginx
Start:

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

Stop it:

Kill-quit ' Cat. /logs/nginx.pid '

Reload configuration:

./nginx-t-C ' pwd '/. /conf/nginx.confkill-hup ' Cat. /logs/nginx.pid '

The Django background interface was successfully displayed:
PPPPPPPPPPPPPPPPPPPPP1

5. Deploying version web.py
Installation dependencies
spawn-cgi
Flup
Configure Nginx
Add under Server Configuration item

Location/{#这两种方法都可以, except that the method of spawn-cgi startup 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;}

A simple index.py.

#!/usr/bin/python#-*-coding:utf-8-*-Import web  urls = ("/.*", "Hello") app = Web.application (URLs, Globals ()) Clas S hello:def GET (self):  return ' Hello, world! ' if __name__ = = ' __main__ ': Web.wsgi.runwsgi = lambda func, addr=none:w Eb.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 '/we Bpy.pid-f/home/dantezhu/htdocs/ngx_web/index.py-s/home/dantezhu/nginx/sbin/webpy.sock &

Stop it:

Kill-9 ' Cat webpy.pid '

Start Nginx
Add to Rc.local, auto start

/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.