@


nginx官网:http://nginx.org/en/

nginx有关uwsgi模块的介绍:http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html



安装使用uWSGI

安装uWSGI

# 进入虚拟环境,安装uWSGI
(venv) [root@master ~]# pip3.6 install uwsgi # 检查uWSGI版本
(venv) [root@master ~]# uwsgi --version
2.0.17.1 # 检查uWSGI Python版本
(venv) [root@master ~]# uwsgi --python-version
3.6.7

运行简单的uWSGI

test.py文件如下:

def application(env, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [b'Hello World'] # Python3

运行命令如下:

uwsgi --http :8000 --wsgi-file test.py

"""参数详解:
--http :8000 -> 使用http协议,端口8000
--wsgi-file test.py -> 加载test.py文件
"""

运行后,可从浏览器访问:

【使用uWSGI和Nginx来设置Django和你的Web服务器】-LMLPHP



配置Nginx结合uWSGI

这里我们只讲解Nginx配置文件部分

worker_processes 1;
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;
keepalive_timeout 65;
# 负载集群
upstream load {
server 192.168.1.100:9999;
server 192.168.1.200:9999;
server 192.168.1.300:9999;
}
server {
listen 80;
server_name 192.168.43.149;
location / {
# nginx自带的ngx_http_uwsgi_module模块,起到nginx和uwsgi交互的作用
# 通过uwsgi_pass指定服务器地址和协议,将动态请求转发给uwsgi处理
uwsgi_pass load;
include /usr/local/nginx1.12/conf/uwsgi_params;
}
# nginx处理静态页面资源
location /static {
alias /data/static/;
}
# nginx处理媒体资源
location /media {
alias/data/media/;
}
}
}

配置完后重启Nginx,即可实现其功能.


supervisor

supervisor是基于Python的任务管理工具,用于自动运行各种后台任务。当然我们也能直接使用Linux的nohup命令是任务自动后台运行,但如果要重启任务,就得手动去kill掉任务进程。这样很繁琐,而且一旦程序错误导致进程退出的话,系统也无法自动重载任务。

下载

# 由于supervisor在python3下无法使用,因此只能用python2去下载
yum install python-setuptools
easy_install supervisor

通过下面的命令生成supervisor的配置文件

echo_supervisord_conf > /etc/supervisord.conf

然后在/etc/supervisord.conf末尾添加如下代码

[program:django_test]  # [program:项目名称]
command=/root/Envs/djang1.11.11/bin/uwsgi --ini /root/django_test/uwsgi.ini
# 程序启动命令 autostart=true
# 在supervisord启动的时候也自动启动,可使uWSGI程序被杀掉后自动运行 # 这里我们只使用到了上面两个参数⬆️ # startsecs=10
# 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒 # autorestart=true
# 程序退出后自动重启,可选值有:[unexpected,true,false],默认为unexpected,表示进程被意外杀死后才重启 # startretries=3
# 启动失败自动重试次数,默认是3 # user=django1.11.11
# 用哪个用户启动进程,默认为root # priority=999
# 进程启动优先级,默认999,值小的优先启动 # redirect_stderr=true
# 把stderr重定向到stdout,默认false # stdout_logfile_maxbytes=20MB
# stdout 日志文件大小,默认50MB # stdout_logfile_backups = 20
# stdout 日志文件备份数,默认是10 # stdout
# 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) # stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out # stopasgroup=false
# 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程 # killasgroup=false
# 默认为false,向进程组发送kill信号,包括子进程

其中command是结合virtualenv的命令和supervisor的精髓:

command=/root/Envs/djang1.11.11/bin/uwsgi --ini /root/django_test/uwsgi.ini

command=/root/Envs/djang1.11.11/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /root/django_test --home=/root/venv --module django_test.wsgi
# --chdir:指定项目的根
# --home:指的是虚拟环境目录
# --module:找到Django项目环境中的wsgi.py文件

启动supervisor

# 启动supervisor
supervisord -c /etc/supervisord.conf # 重启my项目
supervisorctl -c /etc/supervisord.conf restart my supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

重新加载supervisor

supervisorctl update
# 更新新的配置到supervisord supervisorctl reload
# 重新启动配置中的所有程序 supervisorctl start program_name
# 启动某个进程(program_name=你配置中写的程序名称) supervisorctl
# 查看正在守候的进程 pervisorctl stop program_name
# 停止某一进程 (program_name=你配置中写的程序名称) supervisorctl restart program_name
# 重启某一进程 (program_name=你配置中写的程序名称) supervisorctl stop all
# 停止全部进程 # 注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。

Django静态文件与Nginx配置

mysite/settings.py

# 此参数会将将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制到指定的路径下
STATIC_ROOT='/data/static' STATIC_URL='/static'
STATICFILES_DIRS=[
os.path.join(BASE_DIR, 'static'),
]

配置完毕后,运行命令:

python3.6 manage.py collectstatic

上面的命令会收集所有我们项目中所有的静态文件并保存到STATIC_ROOT指定的路径下.

把这些文件放到一起是为了用nginx等部署的时候更方便.


参考文献:https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html

05-10 22:53
查看更多