iknwosu3
By:
iknwosu3

Server error during upload(500)

July 23, 2017 1.1k views
Django Storage DigitalOcean Ubuntu 16.04

Hello fellow humans.

I finally deployed my app after following the tutorial how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04 but when i try to upload a file on the site as a user, i get
server error (500). Wht do I have to do to remedy the problem as soon as possible??

will appreciate your assistance

4 Answers

Hi @iknwosu3

Can you show the log both Gunicorn and Nginx?

tail -20 /var/log/nginx/error.log
tail -20 /path/to/gunicorn.log
  • hey hansen

    Just checked the nginx log and it says

    (env) kingiyk@stylplus:~/stylplus$ sudo tail -20 /var/log/nginx/error.log
    [sudo] password for kingiyk:
    2017/07/24 08:23:44 [error] 11154#11154: *118 open() "/home/kingiyk/stylplus/static/base.css" failed (2: No such file or directory), client: 88.128.81.132, server: 67.207.87.22, request: "GET /static/base.css HTTP/1.1", host: "67.207.87.22", referrer: "http://67.207.87.22/account/"
    2017/07/24 08:24:05 [error] 11154#11154: *122 open() "/home/kingiyk/stylplus/static/base.css" failed (2: No such file or directory), client: 88.128.81.132, server: 67.207.87.22, request: "GET /static/base.css HTTP/1.1", host: "67.207.87.22", referrer: "http://67.207.87.22/images/create/"
    2017/07/24 08:24:05 [error] 11154#11154: *122 open() "/home/kingiyk/stylplus/static/base.css" failed (2: No such file or directory), client: 88.128.81.132, server: 67.207.87.22, request: "GET /static/base.css HTTP/1.1", host: "67.207.87.22", referrer: "http://67.207.87.22/images/create/"
    2017/07/24 08:31:00 [error] 11154#11154: *140 open() "/home/kingiyk/stylplus/static/base.css" failed (2: No such file or directory), client: 88.128.81.132, server: 67.207.87.22, request: "GET /static/base.css HTTP/1.1", host: "67.207.87.22", referrer: "http://67.207.87.22/images/create/"
    2017/07/24 08:31:00 [error] 11154#11154: *140 open() "/home/kingiyk/stylplus/static/base.css" failed (2: No such file or directory), client: 88.128.81.132, server: 67.207.87.22, request: "GET /static/base.css HTTP/1.1", host: "67.207.87.22", referrer: "http://67.207.87.22/images/create/"
    
    
    67.207.87.22/images/create is the url to uplod so i am guessing it encountered an issue. 
    
    • @iknwosu3

      But that's just a missing CSS file - it shouldn't create the 500 failure.

      You need to activate error logging in Gunicorn, so we can see what's going on:
      http://docs.gunicorn.org/en/stable/settings.html#errorlog

      • I am totally knew to this concepts so pls bear with me.

        (env) kingiykC@stylplus:~/stylplus$ sudo --error-logfile FILE, --log-file FILE
        sudo: unrecognized option '--error-logfile'
        usage: sudo -h | -K | -k | -V
        usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
        usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user]
        [command]
        usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
        prompt] [-u user] [VAR=value] [-i|-s] [<command>]
        usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
        prompt] [-u user] file ...
        (env) kingiyk@stylplus:~/stylplus$

        • @iknwosu3

          It's not a command, it's a configuration parameter you need to set in your SystemD script that executes Gunicorn.
          You would add it to the ExecStart of the file /etc/systemd/system/gunicorn.service and the FILE should be an actual path like /var/log/gunicorn.log

          • Whats up hensen

            This is what i did to the execstart: How do i check the error log after this?

            ExecStart=/home/kingiyk/stylplus/env/bin/gunicorn --error-logfile - --access-logfile - --workers 3  --bind unix:/home/kingiyk/stylplus/stylplus.sock stylplus.wsgi:application
            

            Or maybe there is an alternative way to check the gunicorn log because I still get server error (500) during upload.

          • hey hansen. I finally activated error logging for gunicorn and this is what it says after i got the server error 500 error. Not much but here it is

            
            [2017-07-25 08:57:54 +0000] [30293] [INFO] Starting gunicorn 19.7.1
            [2017-07-25 08:57:54 +0000] [30293] [INFO] Listening at: unix:/home/kingiyk/stylplus/stylplus.sock (30293)
            [2017-07-25 08:57:54 +0000] [30293] [INFO] Using worker: sync
            [2017-07-25 08:57:54 +0000] [30299] [INFO] Booting worker with pid: 30299
            [2017-07-25 08:57:54 +0000] [30300] [INFO] Booting worker with pid: 30300
            [2017-07-25 08:57:54 +0000] [30301] [INFO] Booting worker with pid: 30301
            
  • ..And nginx log says

    2017/07/24 22:29:59 [crit] 18930#18930: *8 connect() to unix:/home/kingiyk/stylplus/stylplus.sock failed (2: No such file or directory) while connecting to upstream, client: 197.211.63.139, server: 67.207.87.22, request: "GET /images/create/ HTTP/1.1", upstream: "http://unix:/home/kingiyk/stylplus/stylplus.sock:/images/create/", host: "www.styllplus.com"
    2017/07/24 22:30:03 [crit] 18930#18930: *8 connect() to unix:/home/kingiyk/stylplus/stylplus.sock failed (2: No such file or directory) while connecting to upstream, client: 197.211.63.139, server: 67.207.87.22, request: "GET /images/create/ HTTP/1.1", upstream: "http://unix:/home/kingiyk/stylplus/stylplus.sock:/images/create/", host: "www.styllplus.com"
    2017/07/24 22:30:06 [crit] 18930#18930: *8 connect() to unix:/home/kingiyk/stylplus/stylplus.sock failed (2: No such file or directory) while connecting to upstream, client: 197.211.63.139, server: 67.207.87.22, request: "GET /images/create/ HTTP/1.1", upstream: "http://unix:/home/kingiyk/stylplus/stylplus.sock:/images/create/", host: "www.styllplus.com"
    2017/07/24 22:30:19 [crit] 18930#18930: *8 connect() to unix:/home/kingiyk/stylplus/stylplus.sock failed (2: No such file or directory) while connecting to upstream, client: 197.211.63.139, server: 67.207.87.22, request: "GET /images/create/ HTTP/1.1", upstream: "http://unix:/home/kingiyk/stylplus/stylplus.sock:/images/create/", host: "www.styllplus.com"
    

    this gives more info to the issue i think. because the upload url is stated there and it says stylplus.sock failed.

    uploading is the only issue i have got as every other thing works just fine

@iknwosu3

Change it do this:

ExecStart=/home/kingiyk/stylplus/env/bin/gunicorn --error-logfile /var/log/gunicorn.log --access-logfile - --workers 3  --bind unix:/home/kingiyk/stylplus/stylplus.sock stylplus.wsgi:application

Restart Gunicorn sudo service gunicorn restart
Try to upload, so you get the error and then check /var/log/gunicorn.log

  • do I create a file gunicorn.log in the /var/log/ directory or it will create it self??

  • I get a 502 bad gateway when i use

     ExecStart=/home/kingiyk/stylplus/env/bin/gunicorn --error-logfile /var/log/gunicorn.log --access-logfile - --workers 3  --bind unix:/home/kingiyk/stylplus/stylplus.sock stylplus.wsgi:application
    

    But works fine when i use

    ExecStart=/home/kingiyk/stylplus/env/bin/gunicorn --error-logfile - --access-logfile - --workers 3  --bind unix:/home/kingiyk/stylplus/stylplus.sock stylplus.wsgi:application
    

    Your patience is highly appreciated hansen.

    • @iknwosu3

      Okay, then first stop Gunicorn

      service gunicorn stop
      

      And then run it manually:

      /home/kingiyk/stylplus/env/bin/gunicorn --error-logfile - --access-logfile - --workers 3  --bind unix:/home/kingiyk/stylplus/stylplus.sock stylplus.wsgi:application
      

      Then visit your website and upload a file. It should print errors directly in the console. Use CTRL+C to exit manual Gunicorn when done.

      If this doesn't work, then ask in StackOverflow, since there's a lot more people there using Gunicorn and programming in Django:
      https://stackoverflow.com/questions/tagged/gunicorn

@iknwosu3

Okay, change current log level to debug, by adding this parameter:

--log-level debug

http://docs.gunicorn.org/en/stable/settings.html#loglevel

There's something wrong with your upload-code or maybe you try save the uploaded file to a place where there's no permissions, which somehow is crashing Gunicorn.

  • Hey hansen

    I finally figured out what the problem was. you where right. It had to do with my upload code in settings file. I was using the development server to server the files
    MEDIAURL = '/media/'
    MEDIA
    ROOT = os.path.join(BASE_DIR, 'media/')

    then in my projects urls.py i had
    if settings.DEBUG:
    urlpatterns += static(settings.MEDIAURL,
    document
    root=settings.MEDIA_ROOT)
    which resulted to the server error (500).

    The thrill of fixing a bug is secong to none.

    But i got one more bug.

    Now my files upload but do not show

    In my settings file i have this

    STATICROOT = '/home/kingiyk/stylplus/static'
    STATIC
    URL = '/static/'

    STATICROOT = os.path.join(BASEDIR, 'static/')

    and in nginx i got this

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/kingiyk/stylplus;
    }
    
    location /media/ {
          alias /home/kingiyk/stylplus/media/;
    }
    
    location ~ /.well-known {
       allow all;
    }
    
    location / {
    include proxy_params;
        proxy_pass http://unix:/home/kingiyk/stylplus/stylplus.sock;
    

    I am guessing i got something screwed up.

    • i meant STATICROOT and STATICURL (with an under-score in between)

      • @iknwosu3 Use the </> button the preserve formatting. If you select a word(s), it will show up like this, but if you leave the triple-back-ticks on their own line

        it will show up as this
        
    • @iknwosu3

      But I'm not sure if that's what you're asking about. Do you see the uploaded files somewhere on the server within /home/kingiyk/stylplus or it's sub-directories?

      You need to make sure that Gunicorn and Nginx share the same user/group permissions. That will lead to 404-errors, but that's not quite the same as "do not show".

      • what i mean is, I dont see the uploaded file on the webpage but upload was successful

        • @iknwosu3

          You need to make sure the rights are set correctly.
          Run this command:

          ls -l /home/kingiyk/stylplus/media/name-of-uploaded-file.ext
          

          What's your User and Group in /etc/systemd/system/gunicorn.service?
          And what's user in /etc/nginx/nginx.conf?

          • gunicorn.servive

            User=kingiyk
            Group=www-data

            while nginx.conf is

            user www-data;
            worker_processes auto;
            pid /run/nginx.pid;
            
            events {
                worker_connections 768;
                # multi_accept on;
            }
            
            http {
            
                ##
                # Basic Settings
                ##
            
                sendfile on;
                tcp_nopush on;
                tcp_nodelay on;
                keepalive_timeout 65;
                types_hash_max_size 2048;
                # server_tokens off;
            
                # server_names_hash_bucket_size 64;
                # server_name_in_redirect off;
            
                include /etc/nginx/mime.types;
                default_type application/octet-stream;
            
                ##
                # SSL Settings
                ##
            
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
                ssl_prefer_server_ciphers on;
            
                ##
                # Logging Settings
                ##
            
                access_log /var/log/nginx/access.log;
                error_log /var/log/nginx/error.log;
            
                ##
                # Gzip Settings
                ##
            
                gzip on;
                gzip_disable "msie6";
            
                # gzip_vary on;
                # gzip_proxied any;
                # gzip_comp_level 6;
                # gzip_buffers 16 8k;
                # gzip_http_version 1.1;
                # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
            
                ##
                # Virtual Host Configs
                ##
            
                include /etc/nginx/conf.d/*.conf;
                include /etc/nginx/sites-enabled/*;
            }
            
            

@iknwosu3

Do you get any errors? If yes, is it from Nginx or Gunicorn? Show the last lines from the log.
What's the output of this?

ls -l /home/kingiyk/stylplus/media/name-of-some-file-you-just-uploaded
  • ls: cannot access '/home/kingiyk/stylplus/media/and.jgp': No such file or directory
    kingiyk@stylplus:~$

    is what i keep on getting. plus i cant find the file on the server. /home/kingiyk/stylplus/media/

    or

    /home/kingiyk/stylplus

    • @iknwosu3

      I don't know then. Either activate debug-logging to your entire project or even better would probably be to start over. It's not bad to start over, you will get much better at setting up the server.
      https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

      Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...
      • cool. thanks for the feedbacks mate

        • hey hansen

          Still on the matter but i think i got a lead but still dont know where to go.

          would really appreciate it if you could point me to the right direction

          so here is my image model(upload_to)

          class Image(models.Model):
              user = models.ForeignKey(settings.AUTH_USER_MODEL,
                                       related_name='images_created')
              title = models.CharField(max_length=2000)
              slug = models.SlugField(max_length=200,
                                      blank=True)
              url = models.URLField()
              image = models.ImageField(upload_to='images/%Y/%m/%d')
              description = models.TextField(blank=True, null=True)
              created = models.DateField(auto_now_add=True,
                                                          db_index=True)
              users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
                                                              related_name='images_liked', blank=True)
          
          
          and here is my nginx conf
          
          server {
              listen 80;
              server_name 67.207.87.22;
          
              location = /favicon.ico { access_log off; log_not_found off; }
          
              location /static/ {
                  alias /home/kingiyk/stylplus/static/;
              }
          
              location /media/ {
              alias /home/kingiyk/stylplus/media/;
              }
          
          
          
          
              location / {
              include proxy_params;
                  proxy_pass http://unix:/home/kingiyk/stylplus/stylplus.sock;
              }
          }
          

          When i upload an image, it gets saved to /home/kingiyk/stylplus/images/2017/07/26

          I think the '2017/07/26' part is as a result of my upload_to being 'images/%Y/%m/%d (year/month/day).

          i have no problem with my css and and javascrispt. Just images are not displaying

          Now i am guessing there is a conflict between my nginx conf and the path to where the images are stored. please could you lend a helpling hand. cheers

          • @iknwosu3

            Can you add this line just below server_name in your Nginx:

            root /home/kingiyk/stylplus;
            

            And then I think you can remove the two location-blocks which only contains an alias, but wait with that - only test one thing at the time.

  • Greetings,

    After going back and forth several times, I still havent made my uploaded images to display. but i think its because my uploaded files arent being recognised as static files because when i enter the command python manage.py collectstatic to retrieve all static files and store them in /home/kingiyk/stylplus/static, css, js, and some unknown png files get collected but my images do not. any clue as to why that is??

    cheers

    • @iknwosu3

      I'm not sure, but it seems like you should modify your development platform, so it matches your production.

      I would recommend that ask your question at StackOverflow, since there are some really good programmers, who might just spot the problem.

      • hi hansen,

        i think I know what the problem is. I think its from the template.

        i tried img src="{% static "rango.jpg" %}" alt="Picture of Rango"> and it displayed the rango.jpg image I think i need the {% static %} tag to accomplish this with image variable but still have not gotten it right.
        ```all images are saved in the static directory. My nginx conf is not the cause.

        here is html code

        {% thumbnail image.user.profile.photo "40" as in %}
             <a href="{{ image.user.get_absolute_url }}">
                 <img src="{{ in.url }}" class="gametime"></a>
                 {% endthumbnail %}
         <a href="{{ image.user.get_absolute_url }}" 
        
        class="time">{{image.user.username }}
        
         <div id="details">
         {% thumbnail image.image "551" as im %}
        
             <a href="{{ image.image.url }}">
                 <img src="{{ im.url }}" alt="{{ image.title }}"class="image-detail">
             </a>
        
         {% endthumbnail %}
        
        I am trying to figure out how to make it reference the specific file in the static directory. could you lend a hand?..
        
        thanks
        
Have another answer? Share your knowledge.