Djevops:轻松自托管 Django
Djevops: Self-Host Django Easily

原始链接: https://github.com/mherrmann/djevops

**djevops** 是一款专业的命令行工具,旨在简化 Django 项目在 Linux VPS(Ubuntu/Debian)上的部署流程,且无需使用 Docker。由于专注于 Django,它比 Ansible 等通用自动化工具提供了更快、更精简的体验。 主要功能包括: * **快速设置:** 通过 `djevops init` 初始化,使用 `djevops deploy` 进行部署。 * **集成服务:** 管理数据库(SQLite 或 PostgreSQL)、Redis 和 Celery 工作进程,并支持自动数据库备份(SQLite 通过 Litestream 实现)。 * **安全与维护:** 支持自动生成 SSL 证书、通过本地 `secrets.py` 进行安全的凭据管理、限制端口访问以及自动更新操作系统安全补丁。 * **开发者体验:** 内置日志轮转功能,支持远程访问 `manage.py shell`,并可轻松集成错误报告。 使用前,请通过 `pip` 在本地安装该工具,确保拥有 VPS 的 SSH root 访问权限,并在生成的 `djevops.yml` 配置文件中定义您的基础设施。之后,Git 仓库的更新可以在几秒钟内完成部署,对于追求 Django 工作流简单和高效的开发者来说,djevops 是一个理想的选择。

```Hacker News 最新 | 往期 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Djevops:轻松实现 Django 自托管 (github.com/mherrmann) 7 分,mherrmann 发布于 2 小时前 | 隐藏 | 往期 | 收藏 | 2 条评论 | 帮助 spwa4 27 分钟前 [–] 有时我不禁会想,为什么人们不回归到 apache mod_php 或 mod_python 的方式呢?即使应用没有做到完美隔离,你也知道它们依然可以部署。一切都能正常运行,而且如果你有很多小型网站,那种效率是无可比拟的。 回复 ranger_danger 23 分钟前 | 父评论 [–] 有些人确实还在用……但这意味着当你不可避免地需要身份验证、CRUD 视图、数据库支持等功能时,你得重新发明轮子……我认为这就是 Django 的用武之地;它能处理所有这些功能,而且在我看来,它比其他框架更不会干扰你的开发。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:```
相关文章

原文

djevops is a command-line tool for deploying your Django web app to a Linux VPS. It runs and manages all necessary components (database, Redis, etc.) on your server.

Unlike other tools, djevops does not use Docker. This makes it possible to "push to prod" in seconds. Compared to Ansible, djevops' specialization on Django lets you write significantly less code. The flip side is that djevops is less general.

To get started with djevops, all you need is SSH root access to a Linux VPS running Ubuntu or Debian. Install djevops on your local machine with pip install djevops. Then, execute djevops init in your Django app's Git repository. You get a config file that looks similar to the following:

server: 1.2.3.4

git:
  repo: githubuser/mydjangoapp
  branch: main

services:
  web:
    type: django
    env:
      clear:
        ALLOWED_HOSTS: your.website.com
      secret:
        - DJANGO_SECRET_KEY

db:
  type: sqlite

mail:
  host: smtp.gmail.com
  user: SMTP_USER
  password: SMTP_PASSWORD

Secrets such as DJANGO_SECRET_KEY or SMTP_PASSWORD can be specified as constants in file deploy/secrets.py.

Most config values are optional. Fill in the ones you want and run djevops deploy. djevops then clones your Git repo on the server and starts all services. As you work on your Django app and push new commits to Git, simply run djevops deploy again to apply them to your server.

Automatic SSL certificates

djevops generates and automatically renews SSL certificates for any domains you specify in Django setting ALLOWED_HOSTS. The domains need to be tied to your server's IP address.

Error emails

If you filled in the mail section in the config file, then you can make Django email you when errors occur. To do so, set ADMINS in Django's settings.py as follows:

ADMINS = [('Your Name', '[email protected])]

Error emails require Django setting DEBUG to be False.

Automatic database backups

You can set up automatic database backups by adding a backup element to the db section in the djevops config file. For example:

db:
  type: sqlite
  backup:
    type: s3
    bucket: mybackup
    access-key-id: S3_BACKUP_ACCESS_KEY
    secret-access-key: S3_BACKUP_SECRET_KEY
    path: db
    region: us-east-1

Backups are created continuously while your server is running. If you ever re-install your server, then the latest backup is automatically restored.

For database type sqlite, djevops uses Litestream for backups. Litestream can store backups in S3, Azure Blob Storage and many others. The keys you add to the backup element above get copied into a replica element in Litestream's config. For more information about the available options, please see Litestream's documentation.

Djevops also supports database type postgres. For more information about this, please see below.

PostgreSQL

Instead of SQLite, you can use PostgreSQL by setting the database type to postgres:

You then configure the connection yourself in your settings.py:

import os

DATABASES['default'] = {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'myapp',
    'USER': 'myapp',
    'PASSWORD': os.environ['DB_PASSWORD'],
    'HOST': 'localhost'
}

djevops reads these settings and installs PostgreSQL on the server, creating the database and user with the password you specified. Keep the password out of Git by storing it in deploy/secrets.py. For example:

DB_PASSWORD = "<some strong password>"

Then reference it as a secret in the Django service's environment:

services:
  web:
    type: django
    env:
      secret:
        - DB_PASSWORD

You also need to add psycopg[binary] to your pyproject.toml or requirements.txt file.

As with SQLite, you can add a backup element to enable automatic backups. By default, PostgreSQL backups are taken once per day. You can customize this by setting sync-interval in the backup element, for example to 1h for hourly backups.

Background tasks via Celery and Redis

If your Django app uses the celery Python package, then you can add a Celery worker by adding the following item to the djevops config:

services:
  web:
    # as before
  celery:
    type: celery
    env:
      inherit: web

To install Redis on the server (which many Django apps use as Celery's backend), add an empty top-level redis block:

This setup lets you run Python functions asynchronously and on a schedule such as "every five hours". The service of type celery also runs the necessary beat scheduler.

Easy access to log files

djevops writes the log file for each service to /var/log/<service>.log. To read it, simply SSH into the server and do less, tail -f, etc. To prevent log files from filling up your server's disk space, djevops also rotates and compresses log files.

Secret handling

Very often, you have secrets that you need on the server but should not commit to Git. djevops lets you specify such values in the file deploy/secrets.py, and refer to them from your config file. The way this works is that secrets.py gets executed on your local machine, and the produced values then get uploaded as constants to the server. This gives you a lot of flexibility. You can hardcode values in secrets.py and not commit that file to Git. Or you can for example make secrets.py read from environment variables that are available when you do djevops deploy:

import os
MY_SECRET = os.environ['MY_SECRET']

You can also invoke password managers in secrets.py, etc.

Secure defaults

djevops uses secure defaults whenever possible. For example, each service runs as a separate user. This means that environment variables cannot leak from one service to another. djevops also makes sure that no unintended ports are open, such as for example port 25 when using Postfix for sending emails.

Automatic OS updates

djevops sets up automatic OS updates to keep your server up-to-date and secure. This does not apply major version upgrades, which could introduce potentially breaking changes.

Easy access to `manage.py shell` on the server

Just type djevops shell to be dropped into a remote Django shell on your server. This uses the environment variables and user of the first service of type django in deploy/djevops.yml.

Install the test dependencies from pyproject.toml. The easiest way I know for doing this is with uv:

uv venv
source .venv/bin/activate
uv sync --no-install-project --extra test

Then, you can do python -m unittest to run tests. This requires several API keys specified in environment variables.

联系我们 contact @ memedata.com