Ubuntu16.04部署Django时Supervisor启动异常致502错误排查
Let's break down the issues here step by step—you're hitting classic environment mismatches and configuration misalignments between running uWSGI manually vs via Supervisor.
1. Critical Socket Type & Path Mismatch
First, the biggest red flag in your setup:
- NGINX is configured to communicate with
unix:///etc/uwsgi/saleor.sockusing the uWSGI protocol - But your uWSGI config uses
http-socket = /etc/uwsgi/saleor.sock—this is incorrect!
http-socket makes uWSGI act as a standalone HTTP server (like when you ran uwsgi --http-socket :8080 manually), but NGINX expects to talk via the uWSGI protocol, not raw HTTP. Your logs also show uWSGI fell back to /tmp/saleor.sock—this means it couldn't create the socket in /etc/uwsgi (likely missing directory or permission issues).
Fix:
Update saleor_uwsgi.ini:
# Replace http-socket with socket (uses uWSGI protocol for NGINX communication) socket = /etc/uwsgi/saleor.sock # Add these to run uWSGI as a non-root user (fixes security warnings and permission issues) uid = www-data gid = www-data
Then create the socket directory and set proper permissions:
sudo mkdir -p /etc/uwsgi sudo chown www-data:www-data /etc/uwsgi
2. Supervisor vs Terminal Environment Gap
When you run uWSGI manually, your shell loads your virtualenv's environment variables, but Supervisor runs in a stripped-down environment by default. Using the system-wide /usr/bin/uwsgi instead of your virtualenv's uWSGI binary can lead to missing dependencies, even if you installed the Python plugin.
Fix:
Update supervisor.conf to use the uWSGI binary from your virtualenv and run as a non-root user:
[program:saleor] autostart=true autorestart=true stopsignal=QUIT # Use virtualenv's uWSGI instead of system-wide to ensure correct dependency path command = /webapps/bin/uwsgi --ini /webapps/saleor/saleor_uwsgi.ini stderr_logfile = /webapps/saleor/saleor/logs/supervisor_err.log stdout_logfile = /webapps/saleor/saleor/logs/uwsgi_out.log environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 # Run as non-root user for security and proper file permissions user=www-data group=www-data
3. Static File Mapping Misconfiguration
Your uWSGI config has static-map = /static=/app/static, but your actual static files live at /webapps/saleor/saleor/static. Since NGINX is already configured to handle static files directly (which is more efficient), you have two options:
- Recommended: Remove the
static-mapline entirely and let NGINX handle static assets - Or update it to the correct path:
static-map = /static=/webapps/saleor/saleor/static
4. Final Checks & Restarts
After making these changes, restart all services to apply updates:
# Reload Supervisor to pick up new config sudo supervisorctl reload # Restart NGINX to apply socket path changes sudo systemctl restart nginx
Verify the socket exists and has correct permissions:
ls -l /etc/uwsgi/saleor.sock # Should show permissions like srw-rw-rw- owned by www-data:www-data
If you still see issues, check the latest logs for more clues:
- NGINX error log:
/webapps/saleor/saleor/logs/nginx-error.log - uWSGI log:
/webapps/saleor/saleor/logs/uwsgi_out.log
内容的提问来源于stack exchange,提问作者Henry Lynx

