centos uwsgi配置_centos下配置nginx+uwsgi运行py以及静态文件的加载

本文介绍了如何在 CentOS 系统中配置 Nginx 和 uWSGI 以运行 Python 应用,并处理静态文件。首先安装 Nginx,然后通过 easy_install 和 pip 安装 uWSGI。接着创建 uWSGI 配置文件和启动脚本,确保多服务多端口运行。最后,配置 Nginx 配置文件,使它通过 uWSGI 处理 HTTP 请求并加载静态文件。
摘要由CSDN通过智能技术生成

其实不限于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,不能用源码安装,源码安装会出现访问不了的问题,最简单的就是

easy_install pip && pip install uwsgi

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

先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的配置文件在/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-sizi = 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就可以了

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

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

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

关于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;

}

}

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

最后贴一个这篇文章里面简单完整的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解析的,基本上就是这样子了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值