Nginx + uwsgi @ubuntu

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"]

运行

常用选项

  1. http : 协议类型和端口号
  2. socket: 协议类型和端口号
  3. processes : 开启的进程数量
  4. workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
  5. chdir : 指定运行目录(chdir to specified directory before apps loading)
  6. wsgi-file : 载入wsgi-file(load .wsgi file)
  7. stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
  8. threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
  9. master : 允许主进程存在(enable master process)
  10. daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
  11. pidfile : 指定pid文件的位置,记录主进程的pid号。
  12. 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

转载于:https://my.oschina.net/cttmayi/blog/1832465

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值