1

I followed amazon docs on how to Deploy A Django Project

My settings.py

DEBUG = False

ALLOWED_HOSTS = ['X.XXX.XXX.XXX']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'HOST': 'localhost',
        'PORT': '5432',
        'USER': 'postgres',
        'PASSWORD': 'mypass'
    }
}

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'cms/static')
]

I used 'Host': 'localhost' because 'HOST': '/opt/bitnami/postgresql/', gives me this error which I have asked a question about here:

django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/opt/bitnami/postgresql/.s.PGSQL.5432/.s.PGSQL.5432"?

and python manage.py collectstatic --noinput works fine just like in my laptop.

I added static root in urls.py:

urlpatterns = [
    path('', include('myapp.urls')),
    .
    .
    
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

under /opt/bitnami/apache2/conf/vhosts I created two files:

cms-http-vhost.conf :

<IfDefine !IS_CMS_LOADED>
  Define IS_CMS_LOADED
  WSGIDaemonProcess cms python-home=/home/bitnami/py37-venv/ python-path=/home/bitnami/project/cms
</IfDefine>
<VirtualHost 127.0.0.1:80 _default_:80>
  ServerAlias *
  WSGIProcessGroup cms
  Alias /robots.txt /home/bitnami/project/cms/static/robots.txt
  Alias /static/ /home/bitnami/project/cms/static/
  <Directory /home/bitnami/project/cms/static>
    Require all granted
  </Directory>
  WSGIScriptAlias / /home/bitnami/project/cms/cms/wsgi.py
  <Directory /home/project/cms/cms>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

and cms-https-vhost.conf:

<IfDefine !IS_CMS_LOADED>
  Define IS_CMS_LOADED
  WSGIDaemonProcess cms python-home=/home/bitnami/py37-venv/ python-path=/home/bitnami/project/cms
</IfDefine>
<VirtualHost 127.0.0.1:443 _default_:443>
  ServerAlias *
  SSLEngine on
  SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/server.crt"
  SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/server.key"
  WSGIProcessGroup APPNAME
  Alias /robots.txt /home/bitnami/project/cms/static/robots.txt
  Alias /static/ /home/project/cms/static/
  <Directory /home/project/cms/static>
    Require all granted
  </Directory>
  WSGIScriptAlias / /home/project/cms/cms/wsgi.py
  <Directory /home/project/cms/cms>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

I run the project with python manage.py runserver 0.0.0.0:8000 and I get 404 error for every stataic file in the terminal:

"GET /static/img/showcase.jpg HTTP/1.1" 404 179

If I change debug mode to True every static file would load correctly.

I also check this answer, and this one, but they didn't help.

Shahriar.M
  • 818
  • 1
  • 11
  • 24

1 Answers1

1

Found my answer in Why does DEBUG=False setting make my django Static Files Access fail?

In urls.py I added this line:

from django.views.static import serve 

add those two urls in urlpatterns:

url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

and both static and media files were accesible when DEBUG=FALSE.

Shahriar.M
  • 818
  • 1
  • 11
  • 24