zh

Python VPS托管:Django、Flask和FastAPI部署指南

Python凭借Django、Flask、FastAPI等成熟框架,成为Web开发领域最受欢迎的语言之一。将Python应用部署到离岸VPS不仅能获得完全的配置控制权,还能享受不受地域限制的数据主权保护。Anubiz Host冰岛VPS为Python开发者提供预装Python 3的Ubuntu环境、高性能NVMe存储和充足带宽,是生产级Python应用的可靠选择。

Need this done for your project?

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

Start a Brief

Python虚拟环境与依赖管理

良好的Python项目管理始于虚拟环境的隔离。在VPS上创建独立的Python虚拟环境可以避免系统Python版本冲突和依赖污染:
apt update && apt install -y python3-pip python3-venv git
mkdir /var/www/myproject && cd /var/www/myproject
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
上传或克隆项目后安装依赖:
git clone https://github.com/youruser/yourproject.git .
pip install -r requirements.txt
对于Django项目,执行数据库迁移和静态文件收集:
python manage.py migrate
python manage.py collectstatic --noinput
python manage.py createsuperuser
建议使用.env文件管理敏感配置(SECRET_KEY、数据库密码等),通过python-dotenv或django-environ在运行时加载,避免将凭证写入代码库。

Gunicorn WSGI服务器配置

Gunicorn是Python WSGI应用的标准生产服务器,比Django内置的开发服务器更稳定高效。安装并配置Gunicorn:
pip install gunicorn
gunicorn --workers 3 --bind 127.0.0.1:8000 myproject.wsgi:application
创建systemd服务单元以实现自动启动和进程守护:
nano /etc/systemd/system/gunicorn.service
写入配置:
[Unit]
Description=Gunicorn daemon for myproject
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/myproject
ExecStart=/var/www/myproject/venv/bin/gunicorn     --workers 3     --bind 127.0.0.1:8000     myproject.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target
systemctl enable gunicorn
systemctl start gunicorn
对于FastAPI应用,使用Uvicorn替代Gunicorn:pip install uvicorn[standard],并在服务配置中将ExecStart指向uvicorn命令。

Nginx与SSL配置

Nginx作为反向代理将外部请求转发给Gunicorn,同时处理静态文件服务和SSL终止:
apt install -y nginx certbot python3-certbot-nginx
nano /etc/nginx/sites-available/myproject
Django项目的典型Nginx配置:
server {
    listen 80;
    server_name yourdomain.com;

    location /static/ {
        alias /var/www/myproject/staticfiles/;
    }

    location /media/ {
        alias /var/www/myproject/media/;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
certbot --nginx -d yourdomain.com
systemctl reload nginx

PostgreSQL数据库集成

大多数Django和FastAPI项目使用PostgreSQL作为主数据库。在同一VPS上安装PostgreSQL:
apt install -y postgresql postgresql-contrib
sudo -u postgres psql
在PostgreSQL中创建数据库和用户:
CREATE DATABASE myproject_db;
CREATE USER myproject_user WITH PASSWORD 'strong_password_here';
GRANT ALL PRIVILEGES ON DATABASE myproject_db TO myproject_user;
q
安装Python数据库驱动并更新Django配置:
pip install psycopg2-binary
在Django的settings.py中配置数据库连接:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'myproject_db',
        'USER': 'myproject_user',
        'PASSWORD': 'strong_password_here',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
Anubiz Host的VPS NVMe存储确保PostgreSQL读写操作的高速响应,适合数据密集型的Python应用场景。

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