You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

管理基于Django的网站:本地代码高效部署方法咨询

Efficient Deployment Workflows for Your Django/HTML/JS Project on DigitalOcean Ubuntu Droplet

Hey there! Let's walk through practical, efficient ways to get your local code deployed to your DigitalOcean Droplet (running Ubuntu 16.04, Apache 2.4.18, and Django 1.11). These methods will cut down on manual work and let you iterate faster on your features.

1. Git-Based Deployment (Best for Version-Controlled Workflows)

This is my go-to for most projects—it combines version control with one-click deployments using Git hooks. Here's how to set it up:

  • Server-side setup:

    1. SSH into your Droplet and create a bare Git repository:
      mkdir ~/myproject-deploy.git && cd ~/myproject-deploy.git
      git init --bare
      
    2. Create a post-receive hook script to automate deployment after a push:
      nano hooks/post-receive
      
      Paste this (adjust paths to match your project):
      #!/bin/bash
      GIT_WORK_TREE=/var/www/myproject git checkout -f
      cd /var/www/myproject
      # Install/update dependencies
      pip install -r requirements.txt
      # Run Django migrations (if needed)
      python manage.py migrate
      # Collect static files
      python manage.py collectstatic --noinput
      # Restart Apache to apply changes
      sudo systemctl restart apache2
      
    3. Make the script executable:
      chmod +x hooks/post-receive
      
  • Local setup:
    Add the server as a Git remote and push your code:

    git remote add production ssh://your-username@your-droplet-ip/~/myproject-deploy.git
    git push production main
    

Pros: Ties directly to your version control, deployments are fast and traceable.
Cons: Initial setup takes a few minutes, requires basic Git hook knowledge.

2. IDE-Integrated SFTP (Great for Quick Frontend Tweaks)

If you're working heavily on HTML/JS and want to see changes immediately, use an IDE with SFTP sync. Tools like VS Code or PyCharm make this trivial:

  • VS Code example:
    1. Install the SFTP extension.
    2. Right-click your project folder and select "SFTP: Config".
    3. Update the sftp.json config to match your server details:
      {
        "name": "My DigitalOcean Droplet",
        "host": "your-droplet-ip",
        "username": "your-username",
        "privateKeyPath": "path/to/your/ssh-key",
        "remotePath": "/var/www/myproject",
        "uploadOnSave": true
      }
      
    4. Save the file—now every time you save a local file, it auto-uploads to the server.

Pros: Super intuitive, no command line needed for quick changes.
Cons: No version control integration, risk of overwriting files accidentally on large projects.

3. Fabric (Automated Scripted Deployments)

For fully customizable, repeatable deployments, use Fabric—a Python tool that lets you write scripts to run server commands from your local machine.

  • Setup steps:
    1. Install Fabric locally:
      pip install fabric
      
    2. Create a fabfile.py in your project root with deployment steps:
      from fabric.api import env, run, cd
      
      # Configure server details
      env.hosts = ["your-droplet-ip"]
      env.user = "your-username"
      env.key_filename = "path/to/ssh-key"
      
      def deploy():
          # Navigate to project directory
          with cd("/var/www/myproject"):
              # Pull latest code from Git
              run("git pull origin main")
              # Update dependencies
              run("pip install -r requirements.txt")
              # Apply database migrations
              run("python manage.py migrate")
              # Collect static files
              run("python manage.py collectstatic --noinput")
              # Restart Apache
              run("sudo systemctl restart apache2")
      
    3. Run the deployment with one command:
      fab deploy
      

Pros: Fully automated, easy to adjust steps as your project grows.
Cons: Requires basic Python knowledge to write scripts.

4. Docker Containerization (For Consistent Environments)

If you want to avoid "it works on my machine" issues and simplify environment management, Docker is a solid choice. Note: Ubuntu 16.04 supports Docker, but you'll need to install it first.

  • Basic setup:
    1. Write a Dockerfile for your Django project:
      # Use Python 3.6 (compatible with Django 1.11)
      FROM python:3.6-slim
      WORKDIR /app
      # Copy requirements and install dependencies
      COPY requirements.txt .
      RUN pip install --no-cache-dir -r requirements.txt
      # Copy project files
      COPY . .
      # Run Gunicorn (Apache will reverse proxy to this)
      CMD ["gunicorn", "--bind", "0.0.0.0:8000", "yourproject.wsgi:application"]
      
    2. Build the image locally, push it to a registry (like Docker Hub), then pull and run it on your server.
    3. Update your Apache config to proxy requests to the Docker container's port (8000 in this example).

Pros: Consistent environment across local and server, easy to scale later.
Cons: Steeper learning curve, overkill for small projects.

Quick Tips to Avoid Headaches

  • Always back up your database and project files before deploying.
  • Ensure Apache has mod_wsgi enabled and your virtual host config points to Django's wsgi.py file.
  • For static files, set STATIC_ROOT in your Django settings, then run python manage.py collectstatic to gather them into a single directory for Apache to serve.

内容的提问来源于stack exchange,提问作者Lasse Liimatta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:37:29