AWS Ubuntu Server 16.04部署Django作品集遇内部服务器错误求助
Hey there, let's dig into that frustrating Internal Server Error you're hitting with your Django portfolio deployment. I’ll walk you through targeted checks and fixes based on the steps you’ve already shared.
1. First, Check the Logs (Most Critical!)
When DEBUG = False in production, Django hides detailed error messages—so server logs are your best bet to find the root cause:
- Check Gunicorn logs (reveals issues with your Django app itself):
Thesudo journalctl -u gunicorn -f-fflag lets you watch logs in real time. Try accessing your site while this runs, and you’ll likely see specific errors (like missing dependencies, database misconfigs, or incorrect file paths). - Check Nginx error logs (for communication issues between Nginx and Gunicorn):
Common issues here include missing socket files or permission conflicts between services.sudo tail -f /var/log/nginx/error.log
2. Verify File & Directory Permissions
Permissions are one of the most frequent culprits for 500 errors:
- Ensure your project directory is accessible by both the
ubuntuuser andwww-datagroup (as specified in your Gunicorn config):sudo chown -R ubuntu:www-data /home/ubuntu/portfolio - Give read access to your static files directory:
sudo chmod -R 755 /home/ubuntu/portfolio/static - Check if the Gunicorn socket file exists and has correct permissions:
If the socket doesn’t exist, Gunicorn failed to start—go back to the Gunicorn logs to diagnose why. If it does exist, confirm it’s owned byls -l /home/ubuntu/portfolio/portfolio.sockubuntu:www-datawith permissions likesrw-rw----(660) so Nginx can interact with it.
3. Validate Django Configuration
Double-check your settings.py for easy-to-miss mistakes:
- ALLOWED_HOSTS: While
['52.14.89.55']is correct, you can temporarily set it to['*']to rule out host-related issues (don’t leave this in production!). - STATIC_ROOT: Confirm the path is accurate by running:
This should match yourcd /home/ubuntu/portfolio && python -c "import os; print(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'static/'))"STATIC_ROOTvalue. Also, verifycollectstaticpopulated the static directory:
If it’s empty, re-runls /home/ubuntu/portfolio/staticpython manage.py collectstaticand check for errors during execution. - Database Setup: If using a database (like SQLite), ensure the database file exists and has read/write permissions for the
ubuntuuser.
4. Test Gunicorn Manually
Bypass Nginx entirely to confirm your Django app can run with Gunicorn:
- Activate your virtual environment:
source /home/ubuntu/portfolio/venv/bin/activate - Start Gunicorn directly on a port:
gunicorn --bind 0.0.0.0:8000 portfolio.wsgi:application - Try accessing
http://52.14.89.55:8000in your browser. If this fails, you’ll see detailed Django errors (since you’re running it directly) that will point you to the exact problem (e.g., missing packages, syntax errors insettings.py).
5. Check Nginx Configuration Details
Even though nginx -t passed, there might be subtle issues:
- Verify the
proxy_paramsfile exists (it should be default on Ubuntu 16.04):
If it’s missing, create it with these contents:ls /etc/nginx/proxy_paramsproxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; - Ensure your Nginx site configuration doesn’t conflict with the default site. Confirm the default is removed:
Then restart Nginx again:sudo rm /etc/nginx/sites-enabled/defaultsudo service nginx restart
6. Ubuntu 16.04 Specific Checks
- Confirm your virtual environment is using Python 3 (since you specified
--python=python3):
The output should show a Python 3.x version (Ubuntu 16.04’s default is 3.5). If not, recreate your virtual environment withsource /home/ubuntu/portfolio/venv/bin/activate && pip --versionvirtualenv venv --python=python3.5.
Start with the log checks—they’ll almost always tell you exactly what’s broken. Once you resolve that issue, your portfolio should load up smoothly!
内容的提问来源于stack exchange,提问作者Andy Nguyen

