如何将现有Django应用迁移至virtualenv虚拟环境?
Hey there! Great call moving your Django app into a virtual environment—it’s the perfect way to shield it from system-wide software changes, especially since you plan to upgrade your LTS down the line. Let’s walk through this step-by-step for your specific setup (Ubuntu 16.04.3, Apache2, Python 3.5, Django 2.0):
1. Prepare the Virtual Environment Tool
Python 3.5 comes with the venv module built-in, but Ubuntu might have split it into a separate package. First, make sure it’s installed:
sudo apt-get update && sudo apt-get install python3-venv
2. Create Your Virtual Environment
Pick a directory to store your virtual environments (I like keeping them separate from projects for clarity, but you can put it in your project root too):
# Create a folder for envs (if it doesn't exist) mkdir ~/django_virtual_envs && cd ~/django_virtual_envs # Create the virtual environment (name it something meaningful, like myapp_env) python3 -m venv myapp_env
3. Activate the Virtual Environment
Activate it to start using the isolated Python environment:
source myapp_env/bin/activate
You’ll see (myapp_env) at the start of your command line—this means you’re working inside the virtual environment.
4. Migrate Your App’s Dependencies
First, we need to replicate your current dependency setup in the virtual environment:
- If you don’t already have a
requirements.txtfile, export your current system dependencies first (run this outside the virtual environment, or use the full path to system Python):pip3 freeze > /path/to/your/django/project/requirements.txt - Then, back in your activated virtual environment, install those dependencies:
This will install Django 2.0 and all other packages your app uses, exactly as they are now, into the isolated environment.pip install -r /path/to/your/django/project/requirements.txt
5. Configure Apache to Use the Virtual Environment
Since you’re using Apache with mod_wsgi, you need to update your app’s Apache config to point to the virtual environment’s Python interpreter.
Open your app’s Apache config file (usually in /etc/apache2/sites-available/your_app.conf) and modify it with one of these setups:
Option 1: Basic Setup
Add these lines to your config (adjust paths to match your setup):
# Point to the virtual environment's Python home WSGIPythonHome /home/your_username/django_virtual_envs/myapp_env # Point to your Django project's root directory WSGIPythonPath /path/to/your/django/project
Option 2: Daemon Mode (Recommended)
For better performance and isolation, use daemon mode:
# Define a daemon process tied to your virtual environment WSGIDaemonProcess myapp python-home=/home/your_username/django_virtual_envs/myapp_env python-path=/path/to/your/django/project # Tell Apache to use this process for your app WSGIProcessGroup myapp
After updating the config, restart Apache to apply changes:
sudo systemctl restart apache2
6. Verify the Setup Worked
To make sure your app is using the virtual environment, add a quick test view to your Django project:
# In one of your app's views.py from django.http import HttpResponse import sys import django def check_env(request): env_details = f"Python Executable: {sys.executable}<br>" env_details += f"Django Version: {django.get_version()}" return HttpResponse(env_details)
Add a URL pattern for this view, then visit it in your browser. You should see the Python path pointing to myapp_env/bin/python and Django version 2.0—confirming the virtual environment is active.
Key Notes for Future LTS Upgrades
- When you upgrade your Ubuntu LTS, your virtual environment will remain untouched—all your dependencies are stored locally, so system updates won’t break your app.
- If you need to update dependencies later, just activate the virtual environment and use
pipas usual—no impact on the system’s global Python setup. - Make sure the Apache user (
www-data) has read access to your virtual environment and project directories. If you run into permission issues, adjust permissions with:sudo chmod -R 755 ~/django_virtual_envs/myapp_env sudo chown -R your_username:www-data /path/to/your/django/project
That’s it! Your Django app is now safely isolated from system changes. If you hit any hiccups (like mod_wsgi compatibility or permission issues), feel free to troubleshoot those specific bits—this core process should get you 90% of the way there.
内容的提问来源于stack exchange,提问作者4dummies

