zh

Django VPS托管:Python Web应用部署指南

Django是Python生态中最成熟的全栈Web框架,以其内置ORM、认证系统、管理后台和完善的安全特性著称。将Django应用部署到专属离岸VPS可以避免PaaS平台(如Heroku、Railway)的定价限制和数据主权问题。Anubiz Host冰岛VPS为Django生产环境提供完整的Python运行时支持和高性能存储基础设施。

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

Django生产环境部署

在Anubiz Host VPS上部署Django应用的完整流程:
apt update && apt install -y python3-pip python3-venv postgresql postgresql-contrib nginx git

# 创建PostgreSQL数据库
sudo -u postgres createdb django_db
sudo -u postgres createuser django_user
sudo -u postgres psql -c "ALTER USER django_user WITH PASSWORD 'strong_password';"
sudo -u postgres psql -c "GRANT ALL ON DATABASE django_db TO django_user;"

# 克隆并配置项目
cd /var/www
git clone https://github.com/youruser/django-project.git
cd django-project
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt gunicorn psycopg2-binary
配置生产settings:
# settings/production.py
DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'django_db',
        'USER': 'django_user',
        'PASSWORD': 'strong_password',
        'HOST': 'localhost',
    }
}
python manage.py migrate
python manage.py collectstatic --noinput

Gunicorn服务配置

创建Gunicorn systemd服务实现自动启动和进程守护:
nano /etc/systemd/system/gunicorn-django.service
[Unit]
Description=Gunicorn Django Application
After=network.target postgresql.service

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/django-project
Environment="PATH=/var/www/django-project/venv/bin"
Environment="DJANGO_SETTINGS_MODULE=myproject.settings.production"
ExecStart=/var/www/django-project/venv/bin/gunicorn     --workers 3     --bind unix:/run/gunicorn.sock     --access-logfile /var/log/gunicorn/access.log     --error-logfile /var/log/gunicorn/error.log     myproject.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
mkdir /var/log/gunicorn
chown www-data:www-data /var/log/gunicorn
systemctl enable gunicorn-django
systemctl start gunicorn-django
Nginx配置通过Unix socket连接Gunicorn(比TCP端口更高效):
proxy_pass http://unix:/run/gunicorn.sock;

Celery异步任务配置

Django复杂应用通常需要Celery处理异步任务(发邮件、数据处理、定时任务)。安装并配置Celery:
source venv/bin/activate
pip install celery redis django-celery-beat
在Django settings中配置Celery:
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
创建Celery worker的systemd服务:
nano /etc/systemd/system/celery-worker.service
[Unit]
Description=Celery Worker
After=network.target redis.service

[Service]
User=www-data
WorkingDirectory=/var/www/django-project
Environment="PATH=/var/www/django-project/venv/bin"
ExecStart=/var/www/django-project/venv/bin/celery     -A myproject worker --loglevel=info
Restart=always

[Install]
WantedBy=multi-user.target
apt install -y redis-server
systemctl enable redis-server celery-worker
systemctl start redis-server celery-worker

Django安全生产检查清单

部署Django到生产VPS前,运行Django内置安全检查:
python manage.py check --deploy
关键生产安全设置(settings/production.py):
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']  # 从环境变量读取
DEBUG = False
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
X_FRAME_OPTIONS = 'DENY'
配置Django日志记录到文件:
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': '/var/log/django/error.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}
将Django部署在Anubiz Host冰岛VPS上,结合上述安全配置和隐私友好的法律环境,为Python Web应用提供了理想的生产运行环境。

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online