The server encountered an unexpected internal server error


#1

I keep getting this error in my browser after deploying to heroku.

Internal Server Error

The server encountered an unexpected internal server error

(generated by waitress)

I ran to see if it was to do with staticfiles.

$ heroku run python manage.py collectstatic --noinput

And it came up with: OSError: [Errno 2] No such file or directory: '/app/static'

By removing the below, I was able to get passed OSError: [Errno 2] No such file or directory: '/app/static' and it copied my static files somewhere but obviously heroku needs the below code to be there.

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

I added the above back and changed the root to the below

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')`

This fixed the OSError: [Errno 2] No such file or directory: '/app/static' error. However when running collectstatic I get this

Here is all the code used for my deployment.

0 static files copied to '/app/staticfiles', 1347 unmodified, 80 post-processed.

Which tells me no static files where copied.

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')`
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

settings_production.py

from test.settings import *

import dj_database_url
DATABASES['default'] = dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

DEBUG = False

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

wsgi.py

import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings_production")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Procfile

web: waitress-serve --port=$PORT test.wsgi:application

Folder structure.

test
-collection
 - migrations  
 - static
 - templates
-test
-static (contains robot.txt)
-venv
Procfile
manage.py
requirements.txt

Any help would be greatly appreciated. This has happened before on another app I am unsure what fixed it.

Thanks


#2

Do me a favor and change your code back to what the book says (static, not staticfiles as your STATIC_ROOT). Then do me a favor and create a static directory in the directory that has your settings.py file (like this thread talks about): http://stackoverflow.com/questions/19323513/heroku-django-oserror-no-such-file-or-directory-app-myappname-static

Iā€™m wondering what behavior changes when this happens! Donā€™t think itā€™ll be a solution but itā€™ll help me narrow down whatā€™s going on.


#3

This is the logs when changing to

STATIC_ROOT = 'static'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

I then ran

$ heroku run python manage.py collectstatic --noinput

which gave me the same ā€œ0 static files copiedā€¦ā€ response

and

$ python manage.py collectstatic 

Which gave me the same again

After updating the repository and pushing to heroku this still gave me the same error

Internal Server Error

The server encountered an unexpected internal server error

(generated by waitress)

And no static files were copied to the static folder placed in the same directory containing settings.py.

I then tried the commands below which copied the static files to the static directory.

$ python manage.py collectstatic
$ git add .
$ git commit -a -m "Updates to static directory"
$ git push heroku master

I changed to this

STATIC_ROOT = 'test/static' /* "test" being the name of the app */
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Which worked and the site is live. I am still unsure why it didnā€™t work originally - other than the static files not being copied.

But for anyone else having the same problem I hope this helps!


#4

Hi Tracy, I have same error shown on heroku when I run my app, finally, I find that there might be a line of code missing in your book for settings.py>> BASE_DIR = os.path.dirname(os.path.abspath(file))
Compared between the guide on heroku and page 125 (pdf format, section ā€œsetting up your static files for productionā€), thatā€™s the difference, I add it in settings.py and then it works.
Could you confirm this? if yes, please have this udpated for next version in Leanpub.
Best,
A


#5

Sorry for the delayed response here, Iā€™ve gotten super busy. :( Iā€™ll try to check this soon!


#6

Just so others are aware.

I had this same issue. I tried what both ā€˜friendā€™ and ā€˜Albertā€™ said. I didnā€™t have any success though.

So, if following the book (I followed the PDF version) exactly, the only change I made was to this:

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'hellowebapp/static'),)

This caused the upload to work. (I also had to remember to recommit everything on git every time I made a change). I know in the stackoverflow someone mention to change this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

which I had already in my settings.py, but it was still unsuccessful.

Just thought Iā€™d share.


#7

I ran into a similar issue today. I received the error:

Internal Server Error

The server encountered an unexpected internal server error

(generated by waitress)

When looking at the logs with ā€œheroku logs --tailā€, I saw the following error at the end of the log file:

ValueError: The file ā€˜css/style.cssā€™ could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f3e80f4e3d0>.

To fix this problem, I simply created a file called style.css under collection/static/css

I then did the following commands:
git add .
git commit -a -m "Added style.css"
git push heroku master

After this it all worked out :)


#8

Thanks for both of your responses!


#9

I spent a few hours trying to solve this, and this was the thing that fixed it - thanks for posting!