linux中快速搭建nginx+uwsgi+flask环境

作者:简简单单 2015-02-28

1.下载所需要的软件包

wget http://nginx.org/download/nginx-1.5.9.tar.gz
wget http://projects.unbit.it/downloads/uwsgi-2.0.4.tar.gz
wget http://exim.mirror.fr/pcre/pcre-8.34.tar.gz

2.编译安装

#安装pcre ,Nginx的HTTP Rewrite模块会用到
tar zxvf pcre-8.34.tar.gz
cd pcre-8.34/
./configure
sudo make
sudo make install
cd ..
#安装nginx------------------------------------------
tar -zxvf nginx-1.5.9.tar.gz
cd nginx-1.5.9
./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-cc-opt='-O3' --with-cpu-opt=opteron
sudo  make && sudo make install
cd ..
#安装uwsgi-------------------------------------------
tar -zxvf uwsgi-2.0.4.tar.gz
cd uwsgi-2.0.4
sudo make
vi /etc/ld.so.conf    #添加动态链接库目录/usr/local/lib
#添加行:/usr/local/lib
ldconfig    #使之生效
cp uwsgi /usr/bin
cd ..
#--------------------------------------------------
#Flask,安装方式很多:yum、apt-get、pip
sudo   pip  install  flask

3.编辑nginx和uwigi配置文件

nginx.conf配置

$ cat /usr/local/nginx/conf/nginx.conf
user  nobody;
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name    #记得做下本地hosts
        location / {
            uwsgi_pass 127.0.0.1:9000;
            include uwsgi_params;
            uwsgi_param UWSGI_CHDIR  /data/www.111com.net/flask/yw;  #网站目录可以选,因为uwsgi那还是要配置的
            uwsgi_param UWSGI_SCRIPT run;  #run就是flask/yw目录下运行文件run.py
            access_log off;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    }
}

uwsgi.ini配置

方法1:

$ cat /usr/local/nginx/conf/uwsgi_ini
    [uwsgi]
    socket = 0.0.0.0:9000
    master = true
    pidfile = /usr/local/nginx/uwsgi.pid
    processes = 8
    workers = 2
    chdir = /data/www.111com.net/flask/yw   #网站目录
    callable = app    #这个就是run.py里的app,you know!
    pythonpath = /data/www.111com.net/flask
    profiler=true
    memory-report=true
    enable-threads = true
    logdate=true
    limit-as=6048
    daemonize=/data/logs/flask.log   #run.py运行后产生的信息都记录在这的flask.log日志文件里

方法2:

[uwsgi]
    socket = 0.0.0.0:9000
    pidfile = /usr/local/nginx/uwsgi.pid
    processes = 8
    master = true
    chdir = /data/www.111com.net/flask/yw   #网站目录
    #module = run     #这个就是run.py
    callable = app    #这个就是run.py里的app,you know!
    pythonpath = /data/www.111com.net/flask


4.运行和停止uwsgi和nginx

sudo /usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
sudo sudo /usr/local/nginx/sbin/nginx
#kill
sudo killall nginx
sudo killall -9 uwsgi

相关文章

精彩推荐