nginx + uwsgi + flask环境搭建

2015年2月25日 发表评论 阅读评论

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/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/flask/yw   #网站目录
    callable = app    #这个就是run.py里的app,you know!
    pythonpath = /data/www/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/flask/yw   #网站目录
    #module = run     #这个就是run.py
    callable = app    #这个就是run.py里的app,you know!
    pythonpath = /data/www/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




本站的发展离不开您的资助,金额随意,欢迎来赏!

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

  1. sky
    2015年4月30日22:50 | #1

    uwsgi我使用的是第二种配置,但是遇到*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
    chdir(): No such file or directory [core/uwsgi.c line 2490]
    错误,这是什么问题呢?网上查了,没找到解决办法

  2. admin
    2015年5月4日17:33 | #2

    1、我上面的操作是在ubuntu环境下通过源安装的,请确认你使用的环境?
    2、从报错内容上看,是某相目录不存在,请确认配置文件中所使用的目录都存在。

  1. 本文目前尚无任何 trackbacks 和 pingbacks.