linux平台下nginx部署webpy

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   sp   strong   on   

需要安裝的工具:

  • nginx
  • webpy
  • spawn-fcgi
  • flup

注意:

  • You may replace index.py with your own file name.
  • /path/to/www Is the path to the directory where your webpy application is located.
  • /path/to/www/index.py is the full path to your python file.
  • Do not run anything until you are at Run.

nginx配置:

這裡在vhosts目錄下建立一個webpy.conf設定檔

添加以下配置資訊:

location / {        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;        fastcgi_pass 127.0.0.1:9002;}


添加靜態規則:

location /static/ {    root /path/to/www;    if (-f $request_filename) {        rewrite ^/static/(.*)$  /static/$1 break;    }}    

 

Spawn-fcgi

You can start a process with:

spawn-fcgi -d /path/to/www -f /path/to/www/index.py -a 127.0.0.1 -p 9002
Start and shutdown script

Start:

#!/bin/shspawn-fcgi -d /path/to/www -f /path/to/www/index.py -a 127.0.0.1 -p 9002

Shutdown:

#!/bin/shkill `pgrep -f "python /path/to/www/index.py"`

Note: You‘re free to choose which address, port, directory and filename to use, but be sure to adjust the Nginx configuration.

Hello world!

Save the following code in your www directory and call the file index.py (or whatever you like). The following line is required: web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr).

#!/usr/bin/env python# -*- coding: utf-8 -*-import weburls = ("/.*", "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()

Note: make your file executable by doing chmod +x index.py. You‘ll get errors if it isn‘t executable.

Run
  1. Start a process with spawn-fcgi.
  2. Start Nginx.

To check if it runs do ps aux | grep index.py or simply visit the page in your browser.

To reload your configuration:

/path/to/nginx/sbin/nginx -s reload

And to stop:

/path/to/nginx/sbin/nginx -s stop
NOTESproblem child exited with 2

solution: insert #!/usr/bin/env python into header of main.py

problem spawn-fcgi child exited with 126

solution: chmod +x main.py

linux平台下nginx部署webpy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.