nginx+uwsgi+django+systemd部署django项目

 

   1.将本地项目打包

    • 导出依赖模块
1
pip3 freeze > requirements.txt
    • 修改settings文件中数据库的配置
1
2
3
4
5
6
7
8
9
10
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '192.168.31.62',
        'PORT': '3306',
    }
}
    • 关闭debug模式和白名单
1
2
DEBUG = False# 调试模式
ALLOWED_HOSTS = ['*'# 白名单,只允许列表中的ip访问,*代表所有

  2.服务器环境准备

    • 安装python环境
1
2
3
4
5
yum install zlib-devel libffi-devel mysql-devel -y
tar zxvf Python-3.8.6.tgz
cd Python-3.8.6
./configure --enable-optimizations
make && make install
    • 安装依赖模块
1
pip3 install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
    • 在安装过程中,twisted指定的版本比较新,在仓库里没有,需要单独安装
1
2
3
4
yum install bzip2
tar jvxf Twisted-20.3.0.tar.bz2
cd Twisted-20.3.0
python3 setup.py install

  3.uwsgi

    • 安装
1
pip3 install uwsgi -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
    • 配置uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[uwsgi]
# 监听的地址
http = 127.0.0.1:8000
 
# 工作目录
chdir = /data/ops/
 
# django入口文件
wsgi-file = ops/wsgi.py
 
# 进程个数
processes = 4
 
# 静态文件位置
static-map = /static=/data/ops/static
 
# 后台运行模式,日志输出位置
#daemonize = uwsgi.log
# 前台运行模式,日志输出位置,如果交给systemd管理,必须用logto方式启动
logto = uwsgi.log
 
# 大日志文件大小
log-maxsize = 5000000
 
# 启动用户
user=devops
 
# 启动组
group=devops
 
# pid文件,为了结束用的
pidfile=uwsgi.pid
 
# 启动主进程,来管理其他进程,
# 其它的uwsgi进程都是这个master进程的子进程
# 如果kill这个master进程,相当于重启所有的uwsgi进程
master=true
    • 启动测试
1
uwsgi --ini uwsgi.ini

  4.nginx

    • 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
upstream django {
        server 127.0.0.1:8000;
    }
server {
        listen       80;
        location / {
            include     uwsgi_params;
            #uwsgi_pass  django;
            proxy_pass  http://django;
        }
        location /static {
            alias /data/ops/static;
        }
}
    • 启动测试

  5.systemd接管uwsgi,(uwsgi的配置文件中,要注明以前台方式启动!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=devops
Documentation=http://www.xxx.com
After=network.target
 
[Service]
User=devops
Group=devops
WorkingDirectory=/data/ops
ExecStart=/usr/local/bin/uwsgi --ini /data/ops/uwsgi.ini
ExecReload=/usr/local/bin/uwsgi --reload /data/ops/uwsgi.pid
ExecStop=/usr/local/bin/uwsgi --stop /data/ops/uwsgi.pid
Restart=on-failure
RestartSec=20s
 
[Install]
WantedBy=multi-user.target

  

 

posted @   ForLivetoLearn  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示