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

Ubuntu服务器部署Django应用遭遇403权限错误求助

Hey Vikram, let's work through this 403 Forbidden error step by step—this is super common when setting up Django with Apache, so we'll get it sorted out.

1. The #1 Culprit: /root Directory Permissions

Apache runs as the www-data user, and the /root directory is locked down to only allow access by the root user by default. That's almost certainly why you're getting the forbidden error. Here's how to fix it:

  • First, move your Django project out of /root to a directory Apache can access, like /var/www/:
    sudo cp -r /root/enoticerepo1.2 /var/www/
    
  • Next, set the correct ownership and permissions so www-data can read/write the project files:
    sudo chown -R www-data:www-data /var/www/enoticerepo1.2
    sudo chmod -R 755 /var/www/enoticerepo1.2
    
  • Update every path in your 000-default.conf file to point to the new /var/www/enoticerepo1.2 location (replace all instances of /root/enoticerepo1.2).
2. Fix Incomplete WSGI Configuration

It looks like your Apache config cuts off at WSGIDaemonP...—you need the full WSGI setup to map your Django app to Apache. Here's what the complete config should look like (adjust paths to match your new project location):

Alias /static /var/www/enoticerepo1.2/etenderprj/static
<Directory /var/www/enoticerepo1.2/etenderprj/static>
    Require all granted
</Directory>

# If you're using a virtual environment, add the python-home parameter
WSGIDaemonProcess etenderprj python-path=/var/www/enoticerepo1.2/etenderprj python-home=/path/to/your/virtualenv
WSGIProcessGroup etenderprj
# This maps the root URL to your wsgi.py file
WSGIScriptAlias / /var/www/enoticerepo1.2/etenderprj/etenderprj/wsgi.py

<Directory /var/www/enoticerepo1.2/etenderprj/etenderprj>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Note: If you're using a virtual environment for your Django dependencies, don't skip the python-home line—this ensures Apache uses your venv's Python and packages instead of the system default.

3. Apply Changes and Restart Apache

After updating your config, you need to restart Apache for changes to take effect:

# Ensure the site is enabled (it's usually enabled by default, but double-check)
sudo a2ensite 000-default.conf
# Restart Apache
sudo systemctl restart apache2
4. Check Apache Error Logs for Clues

If you still get the error, dig into Apache's error logs to see the exact issue—this will save you guessing:

sudo tail -f /var/log/apache2/error.log

You'll see messages like "Permission denied: access to / denied" or "File does not exist: /var/www/.../wsgi.py" that point directly to the problem.

5. Extra Check: SELinux (Rare for Ubuntu, But Possible)

If you've manually enabled SELinux on your Ubuntu server (uncommon by default), you'll need to set the correct file contexts for your project:

sudo chcon -R -t httpd_sys_content_t /var/www/enoticerepo1.2
# If you have a media directory for user uploads, add this too
sudo chcon -R -t httpd_sys_rw_content_t /var/www/enoticerepo1.2/media

Most likely, moving the project out of /root and fixing permissions will resolve the forbidden error. Let me know if you hit any snags with the logs or config!

内容的提问来源于stack exchange,提问作者Vikram Singh Chandel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:24:24