在项目同名路径下的settings中设置
DEBUG = False
ALLOWED_HOSTS = ['*'] #允许所有地址访问
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static') # 生产环境静态文件目录
将项目的所有应用下的静态文件收集到‘static’目录下,如 js,css等。
python manage.py collectstatic
在项目同名路径下的settings中设置
DATABASES = {
'default':
{'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db_name',
'USER': 'your_db_user',
'PASSWORD': 'your_db_password',
'HOST': 'localhost',
'PORT': '3306',
}}
数据库的迁移
python manage.py makemigrations
python manage.py migrate
创建文件 /etc/systemd/system/gunicorn.service
:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=your_username
Group=your_groupname
WorkingDirectory=/path/to/your/django/project
Environment="DJANGO_SETTINGS_MODULE=your_project.settings"
Environment="PYTHONPATH=/opt/your_project"
ExecStart=/path/to/gunicorn --workers 3 --bind unix:/path/to/your/project.sock your_project.wsgi:application
[Install]
WantedBy=multi-user.target
使用systemctl管理启动
sudo systemctl start gunicorn
sudo systemctl enable gunicorn # 开机自启
创建 /etc/nginx/conf.d/my_django_project.conf
:
server {
listen 80;
server_name your_server_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/your_username/my_django_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/your_username/my_django_project/myproject.sock;
}
}
sudo nginx -t # 检查配置
sudo systemctl restart nginx
sudo systemctl enable nginx
根据your_server_ip和端口号访问地址
journalctl -u -f gunicorn
#或
sudo systemctl status gunicorn
通过status运行状态查看是否成功安装运行,根据报错结果调试
https://download.csdn.net/download/m0_46621783/90439394