博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Django】Django web项目部署(Nginx+uwsgi)
阅读量:4308 次
发布时间:2019-06-06

本文共 2471 字,大约阅读时间需要 8 分钟。

一、安装uwsgi

 通过pip安装uwsgi。

pip
install
uwsgi

测试uwsgi,创建test.py文件:

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

通过uwsgi运行该文件。

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

常用选项:

http : 协议类型和端口号

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

sudo
apt-get
install
nginx 
#安装

  启动Nginx:

/etc/init
.d
/nginx
start 
#启动
/etc/init
.d
/nginx
stop 
#关闭
/etc/init
.d
/nginx
restart 
#重启

三、Django部署

在我们用python manager.py startproject myproject创建项目时,会自动为我们生成wsgi文件,所以,我们现在之需要在项目目录下创建uwsgi的配置文件即可,我们采用ini格式:

# myweb_uwsgi.ini file
[uwsgi]
 
# Django-related settings
 
socket = :8000
 
# the base directory (full path)
chdir           =
/mnt/myproject
 
# Django s wsgi file
module          = myproject.wsgi
 
# process-related settings
# master
master          =
true
 
# maximum number of worker processes
processes       = 4
 
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          =
true
 
daemonize       =
/mnt/myproject/uwsgi_log
.log
 
pidfile =
/mnt/myproject/uwsgi_pid
.log

再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,http中添加如下内容。

server {
    
listen         8099;
    
server_name    127.0.0.1
    
charset UTF-8;
    
access_log     
/var/log/nginx/myweb_access
.log;
    
error_log      
/var/log/nginx/myweb_error
.log;
 
    
client_max_body_size 75M;
 
    
location / {
        
include uwsgi_params;
        
uwsgi_pass 127.0.0.1:8000;
        
uwsgi_read_timeout 2;
    
}  
    
location
/static
{
        
expires 30d;
        
autoindex on;
        
add_header Cache-Control private;
        
alias
/mnt/myproject/static/
;
     
}
 
}
listen 指定的是nginx 对外的端口号。

server_name  设置为域名或指定的到本机ip。

nginx通过下面两行配置uwsgi产生关联:

include uwsgi_params;  
uwsgi_pass 127.0.0.1:8000;
//
必须与uwsgi中配置的端口一致

最后我们在项目目录下执行下面的命令来启动关闭我们的项目:

1 #启动2 uwsgi --ini uwsgi.ini 3 /etc/init.d/nginx start  4 5 #停止6 uwsgi --stop uwsgi_pid.log7 /etc/init.d/nginx stop

好了 ,现在我们可以访问127.0.0.1:8099即可看到我们自己的项目了

转载于:https://www.cnblogs.com/perfe/p/6196854.html

你可能感兴趣的文章
vnpy学习_04回测评价指标的缺陷
查看>>
ubuntu终端一次多条命令方法和区别
查看>>
python之偏函数
查看>>
vnpy学习_06回测结果可视化改进
查看>>
读书笔记_量化交易如何建立自己的算法交易01
查看>>
设计模式03_工厂
查看>>
设计模式04_抽象工厂
查看>>
设计模式05_单例
查看>>
设计模式06_原型
查看>>
设计模式07_建造者
查看>>
设计模式08_适配器
查看>>
设计模式09_代理模式
查看>>
设计模式10_桥接
查看>>
设计模式11_装饰器
查看>>
设计模式12_外观模式
查看>>
设计模式13_享元模式
查看>>
设计模式14_组合结构
查看>>
设计模式15_模板
查看>>
海龟交易法则01_玩风险的交易者
查看>>
CTA策略02_boll
查看>>