配置python服务器运行环境 nginx+virtualenv+uwsgi+supervisor

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 uWSGI 等

3、创建uWSGI配置文件 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 其中有uWSGI的配置

[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即可。


你可能感兴趣的:(python)