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

Ubuntu 17下通过WSGI运行Django应用报错求助

Fixing AH01276 Error When Running Django 2.0 via WSGI on Apache2 (Ubuntu 17)

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.py
    

    This tells Apache to route all root requests to your Django WSGI entry point.

  • If you're using a virtual environment, add the WSGIPythonPath to ensure Apache can find your Python packages:

    WSGIPythonPath /var/cardsite:/path/to/your/virtualenv/lib/python3.6/site-packages
    

    Replace /path/to/your/virtualenv with 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/cardsite
    

    This 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.py and disable directory indexing:

    <Directory /var/cardsite/cardsite>
        <Files wsgi.py>
            Require all granted
        </Files>
        Options -Indexes +FollowSymLinks
        AllowOverride None
    </Directory>
    

    The -Indexes flag 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_python3
    
  • Restart 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:05:22