Django项目部署(Nginx+uwsgi+django)

一、Django配置

1.settings.py配置

复制全局settings.py配置文件,创建一个副本命名为/pro_settings.py,修改DEBUG为False。

DEBUG = False

# 填写你自己的ip和域名
ALLOWED_HOSTS = []  
# 此处设置可以访问服务器的IP地址,*为允许所以地址

2.wsgi.py配置

# 修改first_pro/wsgi.py文件 first_pro为自己的本地项目文件夹

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_pro.pro_settings')

application = get_wsgi_application()

3.生成requirements.txt文件

在xshell中生成requirements.txt文件(将项目中安装的包,存放到requirements.txt文件中)

  • pip freeze > requirements.txt

4,项目上传到服务器

将项目本地目录上传至服务器(可以是阿里云ECS服务器)

方法一:

  • 可以使用xshell连接阿里云服务器,通过rz命令将本地项目目录压缩为zip之后,上传至服务器
  • 在阿里云服务器上,使用unzip 解压项目压缩文件
  • unzip 你的项目压缩文件.zip

方法二:

  • 可以使用提供ssh连接的工具,将项目目录发送到服务器家目录中
  • scp -r 你的项目目录 服务器用户名@服务器IP:~/ -p ssh服务端口

5.安装python3以及虚拟环境

# 创建虚拟环境
mkvirtualenv -p python3 first_pro
# 进入虚拟环境
# 导出安装包
# 需要把requirements.txt文件中的fdfs-client-py删除
pip install -r requirements.txt
uwsgi 安装
# 安装uwsgi
pip install uwsgi

#  测试uwsgi是否安装成功:
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2
    
# 运行uwsgi: 
uwsgi --http :8000 --wsgi-file demo.py
    
# 测试uwsgi运行是否正常: 
curl 127.0.0.1:8000
    
测试访问
http://127.0.0.1:8000/
uwsgi 配置
在项目根目录中创建deploy目录,新建uwsgi_conf.ini文件。

[uwsgi]

# 使用nginx连接时使用,Django程序所在服务器地址

# 选择云服务器的内网IP和端口,如果是vituralbox是127也可以是10.0
socket=127.0.0.1:8001

#项目根目录

chdir=/home/summer/first_django

# 项目中wsgi.py文件的相对目录

wsgi-file=first_pro/wsgi.py

# 进程数 根据云服务器的核数来看
processes=2
# 线程数
threads=2
# uwsgi服务器的角色
master=True
# 存放进程编号的文件
pidfile=uwsgi.pid
# 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。以前的runserver是依赖终端的
daemonize=logs/uwsgi.log
# 指定虚拟环境所在目录,不能填相对目录 !!!
virtualenv=/home/summer/.envirtualenvs/pro_server


创建uwsgi_conf.ini 配置文件

[uwsgi]
# 使用nginx连接时使用,Django程序所在服务器地址
# 选择内网IP和端口
socket=192.168.216.137:8001

# 项目根目录
chdir=/home/pyvip/test-git

#项目中wsgi.py文件的相对目录
wsgi-file=dj/wsgi.py

# 进程数
processes=2

# 线程数
threads=2

# uwsgi服务器的角色
master=True

# 存放进程编号的文件
pidfile=uwsgi.pid

# 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。以前的runserver是依赖终端的
daemonize=logs/uwsgi.log

# 指定虚拟环境所在目录,不能填相对目录 注意是虚拟环境 最后一个写的是虚拟环境的名称
virtualenv=/home/pyvip/.virtualenvs/first_pro

2,启动uwsgi

切换到deploy目录中,创建logs文件夹,用于存放日志文件

启动uwsgi

uwsgi --ini uwsgi_conf.ini &

停止uwsgi

uwsgi --stop uwsgi.pid

2、nginx配置(设置端口转发)

1, ubuntu安装 nginx

sudo apt install nginx

1.2 强制修改用户名

cd /etc/nginx/
vim nginx.conf
把第一行用户名改为自己的服务器名字默认为www
强制保存
:w !sudo tee %

2,启动nginx,查看启动状态,如果启动状态未active,则代表启动成功

sudo systemctl start nginx && sudo systemctl status nginx

3,默认开启80端口,可以查看一下是否提供web服务

curl -I 127.0.0.1

4,管理命令

To stop your web server, type:

sudo systemctl stop nginx

To start the web server when it is stopped, type:

sudo systemctl start nginx

To stop and then start the service again, type:

sudo systemctl restart nginx

If you are simply making configuration changes, Nginx can often reload without dropping connections. To do this, type:

sudo systemctl reload nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:

sudo systemctl disable nginx

To re-enable the service to start up at boot, you can type:

sudo systemctl enable nginx

项目配置

创建/etc/nginx/conf.d/nginx_conf.conf文件:所以文件名改为自己的项目文件名
upstream first_django {
    # 此处为uwsgi运行的ip地址和端口号 写公网ip
    server 127.0.0.1:8005;
}

server {
    # 监听端口
    listen      80;

    # 服务器域名或者ip地址
    server_name 127.0.0.1;

    # 编码
    charset     utf-8;

    # 文件最大上传大小
    client_max_body_size 1024M;

    # 媒体文件 文件改为项目运行文件
    location /media  {
        alias /home/summer/first_pro/media;
    }

    # 静态文件 文件改为项目运行文件
    location /static {
        alias /home/summer/first_pro/static;
    }

    # 主目录
    location / {
        uwsgi_pass  first_pro;
        include    /etc/nginx/uwsgi_params;
    }
}



/etc/nginx/conf.d/   把配置的nginx配置文件复制进去  

cp iginx_conf.conf /etc/nginx/conf.d

    # 测试nginx配置文件是否正确,
    sudo nginx -t -c /etc/nginx/nginx.conf
    # 打印如下内容,则没问题
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    # 重新加载配置
    sudo nginx -s reload -c /etc/nginx/nginx.conf

基本命令

1, 查看nginx 进程

ps -e | grep nginx

# 杀进程PID
sudo pkill -9 nginx

# 查端口
netstat -a

# 查看指定端口
netstat -ap | grep 8000

如果想获取更多有关python的信息,和想玩python制作的小程序,可以关注微信公众号(dreamspy)。我们一起用python改变世界,一起用python创造梦想。在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值