Skip to content

Instantly share code, notes, and snippets.

@taras-d
Created December 11, 2017 20:23
Show Gist options
  • Save taras-d/e4af664bd452ec9cd824c165e1251d67 to your computer and use it in GitHub Desktop.
Save taras-d/e4af664bd452ec9cd824c165e1251d67 to your computer and use it in GitHub Desktop.
Deploying Django on Apache web server

Deploying Django on Apache web server

(Tested on Ubuntu 14.04 and Apache 2.4.7)

1. Install and enable mod_wsgi

  • sudo apt-get install libapache2-mod-wsgi
  • sudo a2enmod wsgi

2. Static files

Go to project folder and collect static files (setting STATIC_ROOT must be specified) - python manage.py collectstatic

3. Site config

Add site config to the folder etc/apache2/sites-available:

Listen 8030

<VirtualHost *:8030>

    WSGIScriptAlias / /path/to/Project/Project/wsgi.py

    WSGIDaemonProcess mysite python-path=/path/to/Project:/path/to/Project-Env
    WSGIProcessGroup mysite

    Alias /static /path/to/Project/static
    Alias /media /path/to/Project/media

    <Directory /path/to/Project/static>
        Require all granted
    </Directory>

    <Directory /path/to/Project/media>
        Require all granted
    </Directory>

    <Directory /path/to/Project>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

</VirtualHost>

4. Enable site and restart Apache:

  • sudo a2ensite.conf
  • sudo service apache2 restart

5. Open localhost:8040 in browser

Possible errors:

  • Attempt to write a readonly database chmod 664 db.sqlite3 sudo chown :wwww-data db.sqlite3

  • Unable to open database file sudo chown :www-data .

  • Permission denied for media folder sudo chown :www-data ./media/ -R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment