uwsgi
安装
sudo apt-get install python3-pip # 注意 ubuntu python3默认没有安装pip
python3 -m pip install uwsgi
代码(test.py)
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
运行
常用选项
- http : 协议类型和端口号
- socket: 协议类型和端口号
- processes : 开启的进程数量
- workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
- chdir : 指定运行目录(chdir to specified directory before apps loading)
- wsgi-file : 载入wsgi-file(load .wsgi file)
- stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
- threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
- master : 允许主进程存在(enable master process)
- daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
- pidfile : 指定pid文件的位置,记录主进程的pid号。
- vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
命令行运行
# 配合Nginx ,需要用socket参数
uwsgi --socket :8080 --wsgi-file test.py
# 用于直接访问
uwsgi --http :8080 --wsgi-file test.py
Nginx
配置(/etc/nginx/nginx.conf & /etc/nginx/conf.d)
特别注意uwsgi_pass参数, 必须和uwsgi端口一致
server {
# 这个server标识我要配置了
listen 80; # 我要监听那个端口
server_name www.gcsjj.cn ; # 你访问的路径前面的url名称
access_log /var/log/nginx/access.log main; # Nginx日志配置
charset utf-8; # Nginx编码
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型
error_page 404 /404.html; # 错误页面
error_page 500 502 503 504 /50x.html; # 错误页面
# 指定项目路径uwsgi
location / { # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
uwsgi_pass 127.0.0.1:8080; # 指定uwsgi的端口
}
# 指定静态文件路径
location /static/ {
alias /opt/proj/teacher/static/;
index index.html index.htm;
}
}
运行
# 重启Nginx
/etc/init.d/nginx restart
# 关闭&启动Nginx
/etc/init.d/nginx stop
/etc/init.d/nginx start
# 这里有个命令configtest,Nginx配置是重启生效的,如果你修改完了,不知道对 不对又担心影响其他人可以使用它测试
/etc/init.d/nginx configtest
# 如果是生产环境的话Nginx正在运行,就不要直接stop start 或者 restart 直接reload就行了
# 对线上影响最低
/etc/init.d/nginx reload