How to fix the bug 502 Bad Gateway: webpage error due to Gunicorn issue
Published in
2 min readNov 15, 2023
Want to visit your website all the time, even if you close the terminal? Here is the way:
But sometimes, it doesn’t work. Here are some ways to check the reason.
Check the systemd
service that manage Gunicorn.
sudo nano /etc/systemd/system/myapp.service
The code are similar to:
[Unit]
Description=Gunicorn instance to serve myapp
After=network.target
[Service]
User=ec2-user
Group=nginx
WorkingDirectory=/home/ec2-user/firstweb
Environment="PATH=/home/ec2-user/firstweb/venv_winjob/bin"
ExecStart=/home/ec2-user/firstweb/venv_winjob/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 app:app
[Install]
WantedBy=multi-user.target
Note: the following part tells the program the location of gunicorn:
ExecStart=/home/ec2-user/firstweb/venv_winjob/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 app:app
Then reload and restart the service:
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service
Then check the status
sudo systemctl start firstweb.service
If it is still not work, check the journal logs for detailed error messages:
sudo journalctl -u firstweb.service
Sometimes, it may due to Unicorn Executable Path:
source /home/ec2-user/firstweb/venv_winjob/bin/activate
which gunicorn
For example, if the result
~/.local/bin/gunicorn
and it is not as same as the code in the setting file:
ExecStart=/home/ec2-user/firstweb/venv_winjob/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 app:app
we need to change it to keep the same.
ExecStart=/home/ec2-user/firstweb/.local/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 app:app
Or reinstall gunicorn:
Reload and Restart the Service:
sudo systemctl daemon-reload
sudo systemctl restart firstweb.service