centos7 搭建django3.1.4+nginx

django+nginx搭建服务器,要使用uwsgi

安装nginx和uwsgi

yum install nginx

pip3 install uwsgi

安装uwsgi后测试

在项目目录中新建文件 test.py

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

使用如下的命令运行

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

这样就可以访问服务器/本机的8000号端口看到hello world!这里uwsgi便安装成功。

执行命令

curl http://127.0.0.1:8000   

后打印Hello World说明成功了

 

确保django安装成功并测试过runserver命令可以访问后,

1.使用uwsgi启动项目.

第一步:进入django项目

第二步:命令测试启动

uwsgi --http ip(换成自己IP):8000 --file yunpf/wsgi.py --static-map=/static=static

参数说明:

--http 这个就和runserver一样指定IP 端口
--file 这个文件就里有一个反射,如果你在调用他的时候没有指定Web Server就使用默认的
-- static 做一个映射,指定静态文件

  此时,访问http://xxx.xxx.xxx.xxx:8000/,如果成功,表示项目启动成功

 

2.使用uwsgi配置文件启动项目

第一步:在django项目同级目录创建script目录,用于存放配置脚本等等

第二步:进入/script目录,创建一个xxx.ini文件

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/home/wwwroot/yunpf
# 指定项目的application
module=yunpf.wsgi:application
# 指定sock的文件路径       
socket=/home/wwwroot/script/uwsgi.sock
# 进程个数       
workers=5
pidfile=/home/wwwroot/script/uwsgi.pid
# 指定IP端口  换成你自己的IP:端口  
http=192.168.2.108:8080
# 指定静态文件
static-map=/static=/home/wwwroot/yunpf/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/proj/script/uwsgi.log

启动项目:

访问  http://xxxxx:8000也是成功。至此,uwsgi+django就完美结合了.

安装nginx后能正常访问。再配置 /etc/nginx/nginx.conf

配置内容如下:

server { # 这个server标识我要配置了
listen 80; # 我要监听那个端口
server_name 192.168.2.108 ; # 你访问的路径前面的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 unix:/home/wwwroot/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
}

# 指定静态文件路径
location /static/ {
alias /home/wwwroot/yunpf/static/;
index index.html index.htm;
}

}

再重启nginx服务,访问 http://192.168.2.108

正常访问就表示搭建成功了。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值