Django 是一个流行的 Python Web 框架,以下是几种常见的部署方式:
准备服务器
pip install virtualenv
设置项目环境
mkdir /var/www/myproject
cd /var/www/myproject
virtualenv venv
source venv/bin/activate
pip install django gunicorn
配置 Gunicorn
gunicorn.service
文件 (Systemd 服务)[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/myproject
ExecStart=/var/www/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/www/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
配置 Nginx
server {
listen 80;
server_name yourdomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/myproject/myproject.sock;
}
}
启动服务
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx
创建 Dockerfile
FROM python:3.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml 示例
version: '3'
services:
web:
build: .
command: gunicorn myproject.wsgi:application --bind 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- ./static:/code/static
depends_on:
- web
Procfile
web: gunicorn myproject.wsgi --log-file -
bash
heroku create
git push heroku master
heroku run python manage.py migrate
bash
eb init -p python-3.8 django-app
eb create django-env
DEBUG = False
ALLOWED_HOSTS
STATIC_ROOT
和 collectstatic
)希望这些信息对您有帮助!根据您的具体需求选择最适合的部署方式。