Question

Server return 500 after gunicorn restart

Gulp! I had a working site (exhibit.technology), running on a drolet. The site was written in django and used gunicorm and nginx. I need to restart django so I restarted gunicorn (sudo systemctl restart gunicorn) and the web site now return “Server Error 500”. I have restarted nginx, gunicorn, the droplet (from the command line and via a hardware reset from the DO console but this thing is a brick!

Anyone seen such a thing or have suggestions of how to diagnose/fix the problem. Thanks.

A couple of status files…nginx, and then gunicorn

 djangoadmin@ubuntu1:~/pyapps/Exhibit/Exhibit.1/Exhibit$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2024-03-17 03:06:49 UTC; 5s ago
     Docs: man:nginx(8)
  Process: 1453 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 1465 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 1454 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 1466 (nginx)
    Tasks: 2 (limit: 1150)
   CGroup: /system.slice/nginx.service
           ├─1466 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─1468 nginx: worker process

Mar 17 03:06:49 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 17 03:06:49 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Mar 17 03:06:49 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
dja

AND…

djangoadmin@ubuntu1:~/pyapps/Exhibit/Exhibit.1/Exhibit$ sudo systemctl status gunicorn
[sudo] password for djangoadmin: 
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2024-03-17 02:49:49 UTC; 10min ago
 Main PID: 861 (gunicorn)
    Tasks: 4 (limit: 1150)
   CGroup: /system.slice/gunicorn.service
           ├─861 /home/djangoadmin/pyapps/venv/bin/python3 /home/djangoadmin/pyapps/venv/bin/gunicorn --access-logfile - --error-logfile gunicorn.error --log-level deb
           ├─964 /home/djangoadmin/pyapps/venv/bin/python3 /home/djangoadmin/pyapps/venv/bin/gunicorn --access-logfile - --error-logfile gunicorn.error --log-level deb
           ├─965 /home/djangoadmin/pyapps/venv/bin/python3 /home/djangoadmin/pyapps/venv/bin/gunicorn --access-logfile - --error-logfile gunicorn.error --log-level deb
           └─967 /home/djangoadmin/pyapps/venv/bin/python3 /home/djangoadmin/pyapps/venv/bin/gunicorn --access-logfile - --error-logfile gunicorn.error --log-level deb

Mar 17 02:49:49 ubuntu1 systemd[1]: Started gunicorn daemon.
Mar 17 02:55:03 ubuntu1 gunicorn[861]:  - - [17/Mar/2024:02:55:03 +0000] "GET / HTTP/1.0" 500 0 "-" "-"
Mar 17 02:58:03 ubuntu1 gunicorn[861]:  - - [17/Mar/2024:02:58:03 +0000] "GET / HTTP/1.0" 500 0 "-" "-"
Mar 17 02:58:06 ubuntu1 gunicorn[861]:  - - [17/Mar/2024:02:58:06 +0000] "GET / HTTP/1.0" 500 0 "-" "-"


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 17, 2024
Accepted Answer

Hey!

The information you’ve provided shows that both Nginx and Gunicorn are active and running, which is good. However, the HTTP 500 errors in your Gunicorn logs indicate that there’s an issue with your Django application. I could suggest checking a few things here:

  1. Check Gunicorn Logs: You can view the Gunicorn logs with more context using commands like journalctl. For a more detailed view, try:

    journalctl -u gunicorn.service --since today --no-pager
    

    This command will show you the logs for the Gunicorn service from today. Look for any error messages or stack traces that provide clues about the issue.

  2. Django Application Logs: If you have configured logging in your Django settings, check the designated log files. The location and name of these files depend on your configuration in settings.py. If you’re not sure where the logs are, search your settings.py for the LOGGING configuration.

  3. Enable Debug Mode Temporarily (if it’s not a production environment):

    nano ~/pyapps/Exhibit/Exhibit.1/Exhibit/settings.py
    

    Find the DEBUG setting and change it to True. Remember, do not do this on a production site.

  4. Check Database Connectivity: Use Django’s shell to test database connectivity.

    ~/pyapps/venv/bin/python3 manage.py shell
    

    Then, in the shell, try:

    from django.db import connections
    connections['default'].cursor()
    

    If there’s an issue connecting to the database, this will throw an error.

  5. Inspect Nginx Configuration: Ensure Nginx is correctly set up to forward requests to Gunicorn. You can check the configuration with:

    sudo nginx -t
    

    This command will test the configuration files for syntax errors.

Let me know how it goes!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
March 17, 2024

Heya @andrewchalk,

Can you check your Nginx logs as well, what do they say? They might also be pointing you to your App but still it’s worth a try to check them as well:

/var/log/nginx/error_log

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel