管理基于Django的网站:本地代码高效部署方法咨询
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:
- SSH into your Droplet and create a bare Git repository:
mkdir ~/myproject-deploy.git && cd ~/myproject-deploy.git git init --bare - Create a
post-receivehook script to automate deployment after a push:
Paste this (adjust paths to match your project):nano hooks/post-receive#!/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 - Make the script executable:
chmod +x hooks/post-receive
- SSH into your Droplet and create a bare Git repository:
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:
- Install the SFTP extension.
- Right-click your project folder and select "SFTP: Config".
- Update the
sftp.jsonconfig 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 } - 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:
- Install Fabric locally:
pip install fabric - Create a
fabfile.pyin 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") - Run the deployment with one command:
fab deploy
- Install Fabric locally:
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:
- Write a
Dockerfilefor 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"] - Build the image locally, push it to a registry (like Docker Hub), then pull and run it on your server.
- Update your Apache config to proxy requests to the Docker container's port (8000 in this example).
- Write a
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_wsgienabled and your virtual host config points to Django'swsgi.pyfile. - For static files, set
STATIC_ROOTin your Django settings, then runpython manage.py collectstaticto gather them into a single directory for Apache to serve.
内容的提问来源于stack exchange,提问作者Lasse Liimatta

