[Fixed]-Configure SSL Certificate on Apache for Django Application (mod_wsgi)

31👍

Your problem is that your apache is only configured for port 80, hence it doesn’t serve pages over https (port 443).

For this example I assume you want to serve your website only over https, so here is how your config should approximately look like.

Here is your: 000-default.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin admin@example.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # This is optional, in case you want to redirect people 
    # from http to https automatically.
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]

</VirtualHost>

Now here is default-ssl.conf:

<VirtualHost *:443>
    ServerName example.com
    ServerAdmin admin@example.com

    # Django Application
    Alias /static /home/Django/professor/static_root
    <Directory /home/Django/professor/static_root>
        Require all granted
    </Directory>

    <Directory /home/Django/professor/professor>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess professor python-path=/home/Django/professor:/home/Django/professor-vm/lib/python2.7/site-packages
    WSGIProcessGroup professor
    WSGIScriptAlias / /home/Django/professor/professor/wsgi.py


    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/server.crt
    SSLCertificateKeyFile /etc/apache2/ssl/server.key
    SSLCACertificateFile /etc/apache2/ssl/intermediate.crt

</VirtualHost>

After configuration is done, we need to turn on the ssl site and (optionally) rewrite mod:

> sudo a2ensite default-ssl
> sudo a2enmod rewrite
> sudo service apache2 reload
👤lehins

1👍

to complete Alexey’s answer, when i faced the problem i had to disable the 000-default.conf site and use only ssl configuration

👤leila

Leave a comment