centos下配置nginx+uwsgi部署多站点python以及静态文件的加载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LunaW/article/details/50546440

其实不限于centos,个人觉得所有的linux都一样,就好像你喜欢把钱放在左边的口袋,我喜欢把钱放右边的口袋,还有的人喜欢把钱放里面的口袋,无非是配置文件的地方不一样

首先安装nginx,嗯,这个自己装。然后配置好配置文件

配置文件的路径可以用nginx -t来查看:

nginx: the configuration file /alidata/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /alidata/server/nginx/conf/nginx.conf test is successful

嗯,对,我用的是阿里云的一键部署,这不是重点
简单的配置文件就是下面这个样子:

user  www www;
worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /alidata/www;
            index  index.html index.htm index.php index.asp;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
 } 

这样可以访问就可以直接访问地址了,前提是你的root目录(/alidata/www/)下面有index.*的文件,就是网站首页


然后安装uwsgi,需要先安装python开发环境 python-devel或者python-dev

可以使用源码安装或者pip安装,最简单的就是

easy_install pip && pip install uwsgi

当然,在这之前我们可以测试一下uwsgi是否可以正常工作

如果用源码安装,很有可能是502 Bad或者连接被重置,可以运行,但是连接不上

先kill掉uwsgi,或者不kill 记得换个端口就行了,写一个测试的test.py:

#!/usr/bin/env python
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World 8088"
然后运行:
uwsgi --http :8088 -w test.py

在网页上输入地址加端口号,如果显示Hello.....就说明uwsgi是装成功了

如果uwsgi显示如下类似的信息,可能是buffer-size不够:

invalid request block size: 28418 (max 4096)...skip
再上面的命令后面加上 --buffer-size 32789 试试

uwsgi的配置文件在/etc/下新建uwsgi-8088.ini

[uwsgi]
socket = 127.0.0.1:8088
master = false
vhost = true
no-stie = true
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 10000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi8088.pid
daemonize = /tmp/uwsgi8088.log
解释下为什么这样取名字,这样可以配置成多端口多服务,下面配置启动脚本

/etc/init.d/下面新建uwsgi-8088(记得给可执行权限chmod +x):

#!/bin/sh                                                                     

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi-8088
DAEMON=/usr/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
   $DAEMON $CONFIGFILE || echo -n "uwsgi already running"
}

do_stop() {
   $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
   rm -f $PIDFILE
   echo "$DAEMON STOPED."
}

do_reload() {
   $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
}

do_status() {
   ps aux|grep $DAEMON
}

case "$1" in
 status)
   echo -en "Status $NAME: \n"
   do_status
 ;;
 start)
   echo -en "Starting $NAME: \n"
   do_start
 ;;
 stop)
   echo -en "Stopping $NAME: \n"
   do_stop
 ;;
 reload|graceful)
   echo -en "Reloading $NAME: \n"
   do_reload
 ;;
 *)
   echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
   exit 3
 ;;
esac

exit 0
注意上面的uwsgi的路径,你可以用whereis uwsgi 来查看

然后现在可以启动uwsgi-8088了,运行/etc/init.d/uwsgi-8088 start

然后cat /tmp/uwsgi-8088.log看是否运行成功了,或者有什么错误

*** Operational MODE: preforking ***
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 7385, cores: 1)
spawned uWSGI worker 2 (pid: 7394, cores: 1)
最后面类似这样就说明运行成功了


最后我们需要配置nginx使它通过uwsgi来处理python的http请求

nginx.conf里面再加一个server就行了,如下:

server {
        listen       8000;
        server_name  localhost;

        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8088;
            uwsgi_param UWSGI_CHDIR /alidata/www/Test2/;
            uwsgi_param UWSGI_SCRIPT test;
        }
    }
格式就是上面这样,和80的那个server同层就行了

listen的端口是你在网页上访问的端口,比如IP:8000就可以返回test.py的处理

8088的这个端口是nginx和uwsgi交互的端口

UWSGI_CHDIR是项目的根目录最后要带/,UWSGI_SCRIPT是运行的py程序,带.py


如果完全按照上面的配置,service nginx restart以后,访问IP:8000就可以了

具体可以看我的测试地址:http://101.200.209.126:8000 


往往一个服务并不能满足我们的需求,那么我们可以再新建一个/etc/uwsgi-8099.ini

/etc/init.d/uwsgi-8099的脚本然后启动

然后在nginx里面再加一个server就行了,具体仿照上面配置


贴上另一个测试地址:http://101.200.209.126:8001


关于nginx静态文件的加载,假如我的test.py运行需要加载js,jpg的图片怎么办?

这个时候可以配置nginx加载静态文件给py使用,具体在server里面加localtion:

server {
        listen       8000;
        server_name  localhost;

        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8088;
            uwsgi_param UWSGI_CHDIR /alidata/www/Test/;
            uwsgi_param UWSGI_SCRIPT test;
        }

        location ~* .*\.(ico|gif|bmp|jpg|jpeg|png)$ {
            root /alidata/www/Test/;
            expires 4d;
        }
    }

测试地址:http://101.200.209.126:8000/images/bg.jpg

是可以访问到这个图片的,说明加载成功。


最后贴一个这篇文章里面简单完整的nginx.conf:

user  www www;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /alidata/www;
            index  index.html index.htm index.php index.asp;
        }

        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  /alidata/www$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    server {
        listen       8000;
        server_name  localhost;

        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8088;
            uwsgi_param UWSGI_CHDIR /alidata/www/Test/;
            uwsgi_param UWSGI_SCRIPT test;
        }

        location ~* .*\.(ico|gif|bmp|jpg|jpeg|png)$ {
            root /alidata/www/Test/;
            expires 4d;
        }
    }
    server {
        listen       8001;
        server_name  localhost;

        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8099;
            uwsgi_param UWSGI_CHDIR /alidata/www/Test2/;
            uwsgi_param UWSGI_SCRIPT test;
        }
    }

}

上面有个php的那一块是支持php解析的,基本上就是这样子了

猜你喜欢

转载自blog.csdn.net/LunaW/article/details/50546440
今日推荐