Ubuntu 17下通过WSGI运行Django应用报错求助
Hey there, let's break down what's going on here. That AH01276 error means Apache isn't routing requests to your Django application correctly—it's trying to serve the contents of /var/cardsite/cardsite/ directly instead of passing requests through the WSGI handler. Since running manage.py runserver works perfectly, your Django app itself is healthy; the problem lies in your Apache/WSGI configuration.
Here are the key steps to diagnose and fix this:
1. Verify Your WSGI Script Alias Configuration
First, make sure your Apache VirtualHost config properly points to Django's wsgi.py file. This is the most common culprit.
Add or confirm this line in your VirtualHost block:
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.pyThis tells Apache to route all root requests to your Django WSGI entry point.
If you're using a virtual environment, add the
WSGIPythonPathto ensure Apache can find your Python packages:WSGIPythonPath /var/cardsite:/path/to/your/virtualenv/lib/python3.6/site-packagesReplace
/path/to/your/virtualenvwith the actual path to your Python 3.6 virtual environment.
2. Fix Directory Permissions and Apache Directory Settings
Apache needs access to your project files, and you need to prevent it from trying to list directory contents.
Set the correct ownership and permissions for your project directory:
sudo chown -R www-data:www-data /var/cardsite sudo chmod -R 755 /var/cardsiteThis ensures the Apache user (
www-data) can read and execute your Django files.Update your Apache Directory block for the project to explicitly allow access to
wsgi.pyand disable directory indexing:<Directory /var/cardsite/cardsite> <Files wsgi.py> Require all granted </Files> Options -Indexes +FollowSymLinks AllowOverride None </Directory>The
-Indexesflag prevents Apache from generating directory listings, which aligns with the error message's note that directory indexes are forbidden.
3. Ensure You're Using the Python 3.6 WSGI Module
Ubuntu 17 might have the Python 2 WSGI module enabled by default. You need to switch to the Python 3 version:
Enable the Python 3 WSGI module (and disable Python 2's if needed):
sudo a2dismod wsgi # Only if Python 2's wsgi is enabled sudo a2enmod wsgi_python3Restart Apache to apply the module change:
sudo systemctl restart apache2
4. Check Apache Error Logs for Hidden Issues
Sometimes the AH01276 error is a symptom of another problem (like missing Python packages or errors in wsgi.py). Check the error logs for more details:
sudo tail -f /var/log/apache2/error.log
Look for lines about missing modules or permission denied errors—these will point you to any underlying issues preventing the WSGI script from running.
After making these changes, restart Apache and test your site again. The core issue here is that Apache wasn't triggering the WSGI handler for your Django app, so it fell back to trying to serve the directory contents. Double-checking the WSGIScriptAlias path and ensuring wsgi.py is accessible to Apache should resolve this.
内容的提问来源于stack exchange,提问作者olexii.lavrov

