[Answer]-Deploy django webserver on apache

1👍

Here is a possible apache config:

WSGIDaemonProcess blog processes=2 threads=15
WSGIScriptAlias / /path/to/project/blog/blogC/wsgi.py

<Directory /path/to/project/>
            WSGIProcessGroup blog
            WSGIApplicationGroup %{GLOBAL}
            Options All
            AllowOverride All
            Require all granted
</Directory>

Alias /media/ /path/to/project/media/
<Directory /path/to/project/media/>
            Options FollowSymLinks MultiViews
            Order deny,allow
            Allow from all
</Directory>

Alias /static/ /path/to/project/static/
<Directory /path/to/project/static/>
            Options FollowSymLinks MultiViews
            Order allow,deny
            Allow from all
</Directory>

Also create a static and media folders in your projects root folder and set them in settings.py:

import os

BASE_PATH = os.path.join(os.path.dirname(__file__), '..')
MEDIA_ROOT = os.path.join(BASE_PATH, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_PATH, 'static/')
STATIC_URL = '/static/'

Then collect all static files into provided directory (this will link static files from all apps into one location):

./manage.py collectstatic --link

Also make sure apache has permissions to write into media folder, so files can be uploaded:

sudo chown -R www-data:www-data /path/to/project/media

Now you gotta make sure your wsgi.py is configured correctly:

import os, sys

## apache/mod_wsgi cannot find the path without it!
path = os.path.split(os.path.dirname(__file__))[0]
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blogC.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

That should be it.

👤lehins

Leave a comment