nginx + uwsgi + flask环境搭建
1.下载所需要的软件包
1wget http://nginx.org/download/nginx-1.5.9.tar.gz
2wget http://projects.unbit.it/downloads/uwsgi-2.0.4.tar.gz
3wget http://exim.mirror.fr/pcre/pcre-8.34.tar.gz
2.编译安装
1#安装pcre ,Nginx的HTTP Rewrite模块会用到
2tar zxvf pcre-8.34.tar.gz
3cd pcre-8.34/
4./configure
5sudo make
6sudo make install
7cd ..
8#安装nginx------------------------------------------
9tar -zxvf nginx-1.5.9.tar.gz
10cd nginx-1.5.9
11./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-cc-opt='-O3' --with-cpu-opt=opteron
12sudo make && sudo make install
13cd ..
14#安装uwsgi-------------------------------------------
15tar -zxvf uwsgi-2.0.4.tar.gz
16cd uwsgi-2.0.4
17sudo make
18vi /etc/ld.so.conf #添加动态链接库目录/usr/local/lib
19#添加行:/usr/local/lib
20ldconfig #使之生效
21cp uwsgi /usr/bin
22cd ..
23#--------------------------------------------------
24#Flask,安装方式很多:yum、apt-get、pip
25sudo pip install flask
3.编辑nginx和uwigi配置文件
nginx.conf配置
1$ cat /usr/local/nginx/conf/nginx.conf
2user nobody;
3worker_processes 2;
4events {
5 worker_connections 1024;
6}
7http {
8 include mime.types;
9 default_type application/octet-stream;
10 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
11 # '$status $body_bytes_sent "$http_referer" '
12 # '"$http_user_agent" "$http_x_forwarded_for"';
13 #access_log logs/access.log main;
14 sendfile on;
15 #tcp_nopush on;
16 #keepalive_timeout 0;
17 keepalive_timeout 65;
18 gzip on;
19 server {
20 listen 80;
21 server_name #记得做下本地hosts
22 location / {
23 uwsgi_pass 127.0.0.1:9000;
24 include uwsgi_params;
25 uwsgi_param UWSGI_CHDIR /data/www/flask/yw; #网站目录可以选,因为uwsgi那还是要配置的
26 uwsgi_param UWSGI_SCRIPT run; #run就是flask/yw目录下运行文件run.py
27 access_log off;
28 }
29 error_page 500 502 503 504 /50x.html;
30 location = /50x.html {
31 root html;
32 }
33 #location ~ \.php$ {
34 # root html;
35 # fastcgi_pass 127.0.0.1:9000;
36 # fastcgi_index index.php;
37 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
38 # include fastcgi_params;
39 #}
40 }
41}
uwsgi.ini配置
方法1:
1$ cat /usr/local/nginx/conf/uwsgi_ini
2 [uwsgi]
3 socket = 0.0.0.0:9000
4 master = true
5 pidfile = /usr/local/nginx/uwsgi.pid
6 processes = 8
7 workers = 2
8 chdir = /data/www/flask/yw #网站目录
9 callable = app #这个就是run.py里的app,you know!
10 pythonpath = /data/www/flask
11 profiler=true
12 memory-report=true
13 enable-threads = true
14 logdate=true
15 limit-as=6048
16 daemonize=/data/logs/flask.log #run.py运行后产生的信息都记录在这的flask.log日志文件里
方法2:
1[uwsgi]
2 socket = 0.0.0.0:9000
3 pidfile = /usr/local/nginx/uwsgi.pid
4 processes = 8
5 master = true
6 chdir = /data/www/flask/yw #网站目录
7 #module = run #这个就是run.py
8 callable = app #这个就是run.py里的app,you know!
9 pythonpath = /data/www/flask
4.运行和停止uwsgi和nginx
1sudo /usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
2sudo sudo /usr/local/nginx/sbin/nginx
3#kill
4sudo killall nginx
5sudo killall -9 uwsgi
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/nginx-uwsgi-flask/4149.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.