pythonサーバ実行環境nginx+virtualenv+uwsgi+supervisorの構成

2544 ワード

nginxは説明しません.virtualenvはpythonが実行する仮想環境を作成します.uwsgiはpythonとnginxポートがインタラクションを監視する橋渡しであり、supervisorはプロセスを守るために使用されます.
構成環境はnginx+virtualenv+uwsgi+supervisorであり、pythonスクリプトは/data/webフォルダに配置され、仮想環境もこのディレクトリにインストールされ、すべての実行コマンドはこのディレクトリで実行されます.1、pipインストールvirtualenv 2、仮想環境の構成(1)仮想仮想実行環境を作成するフォルダvirtualenv venv(2)インストールに必要なpythonパッケージ、venv/bin/pip install Flask、venv/bin/pip install uWSDiなど
3、uWSDIプロファイルapp_の作成uwsgi.ini
[uwsgi]
	#application's base folder
	base = /data/web

	#python module to import
	app = app
	module = %(app)
	#processes = 2 
	home = %(base)/venv
	pythonpath = %(base)

	#socket file's location
	socket = /data/web/%n.sock

	#permissions for the socket file
	chmod-socket    = 666

	#the variable that holds a flask application inside the module imported at line #6
	callable = app

	#location of log files
	logto = /data/web/%n.log
4、supervisorをインストールし、起動を設定する
具体的な参考:http://blog.csdn.net/kissxia/article/details/79226410中にはuWSDIの構成があります
[program:uwsgi]  
	command=/data/web/venv/bin/uwsgi --ini /data/web/app_uwsgi.ini

5、nginxの配置
server
    {
        listen 80;
        server_name c.kung.com;
        client_max_body_size 75m;

        location / { try_files $uri @app; }

        location @app {
                include uwsgi_params;
                uwsgi_pass unix:/data/web/app_uwsgi.sock;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/client.dh.cx.log;
    }

uwsgi_params構成内容:
uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

nginxとsupervisorを再起動すればいいです.