uwsgi+nginx在服务器上部署django项目

环境配置

此项目部署采用uwsgi+nginx+djagno,首先先确保python、mysql、redis、nginx等都已在服务器安装完毕,具体安装步骤就不再详细说明。

安装虚拟环境

pip install virtualenv

新建虚拟环境
mkvirtualenv xxx

进入虚拟环境
workon xxx

然后将requirements.txt文件上传到服务器之后运行:

pip install -r requirements.txt             # 安装依赖包

安装uwsgi

pip install uwsg

uwsgi和nginx配置

我的项目文件目录是这样的,下面都是直接以此项目为例进行部署。
在这里插入图片描述

测试uwsgi
uwsgi --http :8000 --module Onteac.wsgi

运行成功后可以通过:

http://ip:端口号

能访问到项目,说明uwsgi和项目都是没有问题的。但是这时候的静态文件样式是不生效的

配置nginx
  1. 新建uc_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name  118.31.61.2; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
    alias /home/hzy/onteach/media;  # 指向django的media目录
}

location /static {
    alias /home/hzy/onteach/teach_static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}
  1. 将该配置文件加入到nginx的启动配置文件中
sudo ln -s /home/hzy/onteach/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
  1. 拉取所有需要的static file 到同一个目录
    在django的setting文件中,添加下面一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, "teach_static/")

注意:这里的文件STATIC_ROOT的名称是项目部署时收集静态文件用的,名字不要和STATICFILES_DIRS的文件名一样

运行命令

python manage.py collectstatic
  1. 运行nginx
sudo /usr/sbin/nginx

这里需要注意是直接用nginx命令启动, 不要用systemctl启动nginx不然会有权限问题

配置uwsgi
  1. 新建uwsgi.ini 配置文件, 内容如下:
    [uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/hzy/onteach
# Django's wsgi file
module          = Onteach.wsgi
# the virtualenv (full path)

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
virtualenv = /home/hzy/.virtualenvs/teach_env

# logto = /tmp/mylog.log
chdir: 表示需要操作的目录,也就是项目的目录
module: wsgi文件的路径
processes: 进程数
virtualenv:虚拟环境的目录
  1. 运行uwsgi
workon teach_env

uwsgi -i home/hzy/onteach/conf/uwsgi.ini
访问

部署完成后,nginx会监听80端口,所以不用再输入端口号,直接通过ip访问即可,下面是我部署好的项目

http://118.31.61.2/

如果对源码感兴趣的可以私聊或者留言获取哦
也可以关注我的个人公众号,里面干货多多哦!
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值