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

如何在远程Windows服务器上用virtenv与Git Hooks部署Django至IIS

Awesome that you’ve already got your Django 2.0.2 app up and running on IIS 8.5 (Windows Server 2012 R2) with SQL Server, and you’ve secured sensitive configs like DJANGO_SECRET_KEY and DATABASE_PASSWORD in FastCGI environment variables—great foundation! Let’s walk through adding virtualenv isolation and Git Hooks to automate your deployment workflow. Here’s a step-by-step breakdown tailored to your setup:

1. Set Up virtualenv for Dependency Isolation

Virtualenv keeps your project’s dependencies separate from the system Python, preventing version conflicts and making deployments more consistent.

  • First, install virtualenv using your server’s Python 3.6.4:
    pip install virtualenv
    
  • Navigate to your Django project’s root directory (where manage.py lives):
    cd C:\path\to\your\django-project
    
  • Create a virtual environment (specify the Python path explicitly to ensure it uses 3.6.4):
    virtualenv --python=C:\Python36\python.exe venv
    
  • Activate the virtual environment:
    venv\Scripts\activate
    
    You’ll see (venv) prepended to your command prompt when it’s active.
  • Install your project dependencies. If you don’t have a requirements.txt yet, generate one locally (from your dev environment) with pip freeze > requirements.txt, then copy it to the server and run:
    pip install -r requirements.txt
    
  • Update IIS FastCGI Configuration:
    1. Open IIS Manager → Go to the server-level FastCGI Settings
    2. Find your existing Django app entry → Right-click → Edit
    3. Change the Executable path to point to your virtual environment’s Python:
      C:\path\to\your\django-project\venv\Scripts\python.exe
      
    4. Keep all your existing environment variables (the sensitive ones you set earlier) intact
    5. Restart your IIS site and app pool to apply changes:
      iisreset
      
2. Configure Git Hooks for Automated Deployment

Git Hooks let you automate tasks like pulling code, updating dependencies, and restarting your app whenever you push changes to the server. We’ll set up a server-side post-receive hook (triggers after code is pushed) and optionally a local pre-push hook (to validate code before pushing).

Step 2.1: Create a Bare Git Repository on the Server

A bare repo acts as the central hub for pushing code—it doesn’t have a working directory by default.

  • Pick a directory for the bare repo (e.g., C:\GitRepos\your-project.git)
  • Initialize the bare repo:
    git init --bare C:\GitRepos\your-project.git
    

Step 2.2: Set Up the Server-Side post-receive Hook

This hook will pull the latest code into your deployment directory, update dependencies, run migrations, collect static files, and restart the app pool.

  • Navigate to the bare repo’s hooks folder:
    cd C:\GitRepos\your-project.git\hooks
    
  • Copy the sample hook file to create your post-receive script:
    copy post-receive.sample post-receive
    
  • Open post-receive in Notepad or your preferred editor, replace the contents with this batch script (update paths to match your setup):
    @echo off
    setlocal enabledelayedexpansion
    
    :: Define your paths
    set DEPLOY_DIR=C:\path\to\your\django-project
    set VENV_DIR=!DEPLOY_DIR!\venv
    set APP_POOL_NAME=YourDjangoAppPoolName
    
    :: Pull latest code into deployment directory
    echo Pulling latest code...
    cd !DEPLOY_DIR!
    git pull origin master
    
    :: Activate virtual environment and update dependencies
    echo Activating virtual environment...
    call !VENV_DIR!\Scripts\activate.bat
    echo Installing/updating dependencies...
    pip install -r requirements.txt
    
    :: Run Django migrations and collect static files
    echo Running database migrations...
    python manage.py migrate
    echo Collecting static files...
    python manage.py collectstatic --noinput
    
    :: Restart IIS app pool to apply changes
    echo Restarting app pool...
    %systemroot%\system32\inetsrv\appcmd recycle apppool "!APP_POOL_NAME!"
    
    echo Deployment completed successfully!
    endlocal
    
  • Set Permissions: Right-click the post-receive file → Properties → Security → Grant IIS_IUSRS and your Git user (if using remote Git) Read & Execute permissions.

From your local development machine, add the server’s bare repo as a remote:

git remote add production user@your-server-ip:C:\GitRepos\your-project.git

(If you’re working directly on the server, use the local path: git remote add production C:\GitRepos\your-project.git)

Step 2.4 (Optional): Local pre-push Hook

Add this to your local repo’s .git/hooks/pre-push file to run tests or linting before pushing code, catching issues early:

@echo off
echo Running tests before push...
python manage.py test
if %errorlevel% neq 0 (
    echo Tests failed! Aborting push.
    exit /b 1
)
echo Tests passed! Proceeding with push.
3. Test the Deployment Workflow
  • Make a small code change locally, commit it:
    git add .
    git commit -m "Test deployment hook"
    
  • Push to the server:
    git push production master
    
  • Check the command output for success messages, then visit your website to confirm the changes are live.
Key Notes to Keep in Mind
  • Static Files: Ensure your Django STATIC_ROOT is correctly configured, and IIS is set to serve static files directly from that directory (to reduce load on the FastCGI process).
  • Permissions: Double-check that IIS_IUSRS has read/write access to your deployment directory, virtual environment, and STATIC_ROOT—this prevents errors during migrations or static file collection.
  • Sensitive Configs: Keep using FastCGI environment variables for secrets like DATABASE_PASSWORD—don’t commit these to Git or store them in .env files in your project.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:25:50