将 Django 项目部署到服务器的步骤如下,涵盖主流的部署方案(以 Linux 服务器为例):

一、服务器准备

购买服务器选择云服务器(如阿里云 ECS、腾讯云 CVM、AWS EC2 等),系统推荐 Ubuntu 20.04/22.04 或 CentOS 7/8。

初始化服务器

登录服务器(通过 SSH:ssh 用户名@服务器IP) 更新系统:sudo apt update && sudo apt upgrade(Ubuntu)或 sudo yum update(CentOS) 安装必要工具:sudo apt install python3 python3-pip python3-venv nginx git(Python、虚拟环境、Nginx、Git)

二、上传项目代码

克隆代码(推荐)在服务器上通过 Git 拉取项目(需提前在 GitHub/GitLab 等平台托管代码):

git clone https://你的仓库地址.git
cd 项目目录

手动上传(备选)用scp或 FTP 工具(如 FileZilla)将本地项目文件传到服务器。

三、配置 Python 环境

创建虚拟环境

python3 -m venv venv  # 创建虚拟环境
source venv/bin/activate  # 激活虚拟环境(Windows用 venv\Scripts\activate)

安装依赖确保项目根目录有requirements.txt(可通过pip freeze > requirements.txt生成):

pip install -r requirements.txt
pip install gunicorn  # 安装WSGI服务器(用于运行Django)

四、Django 项目配置

修改 settings.py

# 关闭DEBUG模式(生产环境必须)
DEBUG = False

# 允许访问的域名(服务器IP或域名)
ALLOWED_HOSTS = ['你的服务器IP', '你的域名']

# 静态文件配置(收集静态文件用)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # 新增
STATIC_URL = '/static/'

收集静态文件

python manage.py collectstatic  # 按提示输入yes,会将静态文件集中到staticfiles目录

迁移数据库若使用 SQLite 以外的数据库(如 MySQL),需先在服务器安装并配置数据库,再执行:

python manage.py migrate

创建超级用户(可选)

python manage.py createsuperuser

五、配置 Gunicorn(WSGI 服务器)

Gunicorn 用于运行 Django 应用,替代开发时的runserver。 测试 Gunicorn确保项目能通过 Gunicorn 启动:

gunicorn --bind 0.0.0.0:8000 项目名.wsgi:application
(项目名是wsgi.py所在的目录名,如myproject.wsgi)此时访问 服务器IP:8000 应能看到项目(测试后按Ctrl+C停止)。

创建系统服务(开机自启)新建服务文件:

sudo nano /etc/systemd/system/gunicorn.service

内容如下(需替换路径):

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=你的用户名
Group=www-data
WorkingDirectory=/path/to/你的项目目录
ExecStart=/path/to/你的项目目录/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/path/to/你的项目目录/myproject.sock 项目名.wsgi:application

[Install]
WantedBy=multi-user.target

workers:推荐设置为 2×CPU核心数 + 1 myproject.sock:用于和 Nginx 通信的套接字文件

启动并设置自启

sudo systemctl start gunicorn
sudo systemctl enable gunicorn  # 开机自启
sudo systemctl status gunicorn  # 检查状态(需显示active (running))

六、配置 Nginx(反向代理)

Nginx 用于处理静态文件、转发动态请求到 Gunicorn,同时提供端口 80/443 访问。 创建 Nginx 配置文件

sudo nano /etc/nginx/sites-available/myproject

内容如下(替换路径和域名):

server {
    listen 80;
    server_name 你的服务器IP 你的域名;  # 如 example.com www.example.com

    location = /favicon.ico { access_log off; log_not_found off; }

    # 静态文件交给Nginx处理
    location /static/ {
        root /path/to/你的项目目录;  # 指向项目根目录(staticfiles的上级目录)
    }

    # 动态请求转发给Gunicorn
    location / {
        include proxy_params;
        proxy_pass http://unix:/path/to/你的项目目录/myproject.sock;
    }
}

启用配置并测试

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled  # 建立软链接
sudo nginx -t  # 检查配置是否有误(显示successful为正常)
sudo systemctl restart nginx  # 重启Nginx

开放防火墙端口允许 80 端口访问(以 UFW 为例):

sudo ufw allow 'Nginx Full'

七、配置 HTTPS(可选但推荐)

通过 Let’s Encrypt 免费获取 SSL 证书,配置 HTTPS: 安装 Certbot

sudo apt install certbot python3-certbot-nginx
```获取证书并自动配置 Nginx
```bash
sudo certbot --nginx -d 你的域名 -d www.你的域名

按提示操作,完成后 Nginx 会自动跳转 HTTPS。

八、后续维护

代码更新:拉取最新代码后,重启Gunicorn: sudo systemctl restart gunicorn 日志查看: Gunicorn 日志:sudo journalctl -u gunicorn Gunicorn 日志:sudo journalctl -u gunicorn Nginx 日志:/var/log/nginx/access.logerror.log

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://jiangyudong.top/subject/article/Gunicorn_Django/

许可协议:署名-非商业性使用 4.0 国际许可协议