如何在远程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:
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.pylives):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:
You’ll seevenv\Scripts\activate(venv)prepended to your command prompt when it’s active. - Install your project dependencies. If you don’t have a
requirements.txtyet, generate one locally (from your dev environment) withpip freeze > requirements.txt, then copy it to the server and run:pip install -r requirements.txt - Update IIS FastCGI Configuration:
- Open IIS Manager → Go to the server-level
FastCGI Settings - Find your existing Django app entry → Right-click →
Edit - Change the
Executablepath to point to your virtual environment’s Python:C:\path\to\your\django-project\venv\Scripts\python.exe - Keep all your existing environment variables (the sensitive ones you set earlier) intact
- Restart your IIS site and app pool to apply changes:
iisreset
- Open IIS Manager → Go to the server-level
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-receivescript:copy post-receive.sample post-receive - Open
post-receivein 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-receivefile →Properties→Security→ GrantIIS_IUSRSand your Git user (if using remote Git) Read & Execute permissions.
Step 2.3: Link Your Local Repo to the Server
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.
- 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.
- Static Files: Ensure your Django
STATIC_ROOTis 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_IUSRShas read/write access to your deployment directory, virtual environment, andSTATIC_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.envfiles in your project.
内容的提问来源于stack exchange,提问作者binaryEcon

