Django Nginx+Gunicorn 部署教程

在部署前

  1. 导出requirements.txt 文件:运行pip freeze > requirements.txt命令来生成requirements.txt文件,以便在安装依赖项时使用。

  2. 收集静态文件

在settings.py里面最后一行添加

STATIC_URL = '/static/'  --nginx 会通过static为开头访问指定静态资源文件
STATIC_ROOT  = os.path.join(BASE_DIR, 'static') #指定样式收集目录 
或者 
STATIC_ROOT = '/datadrive/workspace/django_site/static/' -- 我们手动指定目录

运行命令

python manage.py collectstatic
  1. 配置数据库:配置生产环境所需的数据库。常见的数据库是MySQL

  2. 修改setting.py
DEBUG = False

ALLOWED_HOSTS = ['*']

如果你使用了数据库,那么修改为服务器数据库信息

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database',
        'USER': 'root',
        'PASSWORD': '****',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

在Linux上使用Nginx和Gunicorn部署Django应用程序,需要以下步骤:

  1. 安装Django、Gunicorn和Nginx 在Linux服务器上安装Django、Gunicorn和Nginx。您可以使用以下命令来安装:
复制代码
pip install django gunicorn
sudo apt-get install nginx
  1. 创建Django项目 创建一个新的Django项目或使用现有的Django项目。

创建和激活虚拟环境 在进行Django项目开发时,最好使用虚拟环境来隔离各种依赖项。您可以使用以下命令创建和激活一个虚拟环境:

python3 -m venv myvenv
source myvenv/bin/activate
  1. 配置Gunicorn 在Django项目的根目录中创建一个名为gunicorn.conf.py的Gunicorn配置文件,并添加以下内容:

复制代码
bind = "0.0.0.0:8000"
workers = 3

这里的“bind”指定Gunicorn将在所有网络接口上监听端口8000,而“workers”指定Gunicorn将启动3个工作进程来处理传入的请求。可以根据需求调整这些值。

  1. 测试Gunicorn 在Django项目的根目录中运行以下命令以测试Gunicorn是否能够正确启动:
复制代码
gunicorn project_name.wsgi:application -c gunicorn.conf.py

这里“project_name”是您的Django项目的名称。

  1. 配置Nginx 在Nginx的配置文件中添加以下内容:
复制代码
server {
    listen 80;
    server_name yourdomain.com;
    location /static/ {
        alias /path/to/static/files/;
    }

    location /media/ {
        alias /path/to/media/files/;
    }
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

该配置文件将所有静态资源请求(如CSS、JavaScript和图像)都代理到本地文件系统上的对应目录。其中,“/static/”和“/media/”是静态文件和媒体文件的URL前缀,而“/path/to/static/files/”和“/path/to/media/files/”是对应的本地路径。这里的“yourdomain.com”应替换为您的域名,而“127.0.0.1:8000”应替换为Gunicorn监听的IP地址和端口。此外,proxy_set_header指令将一些HTTP头信息发送到后端服务器,包括主机名和客户端IP地址。

  1. 使用systemd将Gunicorn配置为系统服务

将Gunicorn配置为系统服务,可以确保它在Linux服务器启动时自动启动,并在意外关闭或崩溃时自动重启。以下是如何使用systemd创建Gunicorn服务的步骤:

  1. 创建一个.service文件 在/etc/systemd/system目录下创建一个新文件,命名为gunicorn.service,并添加以下内容:
复制代码
[Unit]
Description=Gunicorn daemon for Django Project
After=network.target

# 定义虚拟环境路径
Environment="PATH=/home/wwwroot/abc.com/myvenv/bin"

# 激活虚拟环境
ExecStartPre=/bin/bash -c 'source /home/wwwroot/abc.com/myvenv/bin/activate'

[Service]
User=www
Group=www-data
WorkingDirectory=/home/wwwroot/abc.com/
ExecStart=/home/wwwroot/abc.com/myvenv/bin/gunicorn name.wsgi:application -c /home/wwwroot/abc.com/gunicorn.conf.py

[Install]
WantedBy=multi-user.target

这里,“Description”指定服务的描述性名称,“User”和“Group”指定以哪个用户身份运行Gunicorn进程。而“WorkingDirectory”指向项目的根目录,“ExecStart”指定Gunicorn的启动命令和参数。

  1. 重新加载systemd守护程序配置文件 使用以下命令重新加载systemd守护进程配置文件:
复制代码
sudo systemctl daemon-reload
  1. 启动Gunicorn服务 使用以下命令启动Gunicorn服务:
复制代码
sudo systemctl start gunicorn
  1. 自动启动 使用以下命令将Gunicorn配置为系统自动启动:
复制代码
sudo systemctl enable gunicorn

完成以上步骤后,Gunicorn服务将以systemd的方式在Linux服务器上运行。您可以使用以下命令来管理Gunicorn服务:

  • 启动服务:sudo systemctl start gunicorn
  • 停止服务:sudo systemctl stop gunicorn
  • 重启服务:sudo systemctl restart gunicorn
  • 禁用自动启动:sudo systemctl disable gunicorn

注意,对于新配置的systemd服务,必须重新加载systemd守护程序配置文件才能使更改生效。

猜你喜欢

转载自blog.csdn.net/muzihuaner/article/details/131389416
今日推荐