Add SSL Certificate for Django Deployed with Apache on Ubuntu
Get (free or not free) a SSL certificate and scp the files into the server
# We will use a free certificate from ZeroSSL as an example
scp peng.works.zip user@ip_address:/etc/ssl/
ssh user@ip_address
cd /etc/ssl
unzip peng.works.zip # You will get 3 files: certificate.crt, ca_bundle.crt, private.key
rm peng.works.zip
Configure the apache server
# Suppose we have 2 files under /etc/apache2/sites-available
# 000-default.conf default-ssl.conf
cp 000-default.conf mdblog.conf
cp mdblog.conf mdblog-le-ssl.conf
sudo a2dissite 000-default.conf default-ssl.conf
sudo ennable mdblog-le-ssl.conf mdblog.conf
# mdblog.conf
<VirtualHost *:80>
ServerName peng.works
ServerAlias www.peng.works
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RedirectMatch permanent ^/(.*)$ https://%{SERVER_NAME}/$1
</VirtualHost>
# mdblog-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName peng.works
ServerAlias www.peng.works
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
alias /static /home/ubuntu/mdblog/static
<Directory /home/ubuntu/mdblog/static>
Require all granted
</Directory>
alias /media /home/ubuntu/mdblog/media
<Directory /home/ubuntu/mdblog/media>
Require all granted
</Directory>
<Directory /home/ubuntu/mdblog/blog>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/ubuntu/mdblog/blog/wsgi.py
WSGIDaemonProcess mdblog python-path=/home/ubuntu/mdblog python-home=/home/ubuntu/mdblog/venv
WSGIProcessGroup mdblog
SSLEngine on
SSLCertificateFile /etc/ssl/certificate.crt
SSLCertificateKeyFile /etc/ssl/private.key
SSLCertificateChainFile /etc/ssl/ca_bundle.crt
</VirtualHost>
</IfModule>
apachectl configtest
systemctl reload apache2
systemctl restart apache2
Copied to clipboard
Share this post
Related Posts
Linux Commands Cheat Sheet
Linux commands cheat sheet: comprehensive reference covering basic commands, man pages, and help options.
Linux Commands Use Cases
Linux commands: zip/unzip operations, kernel version checking, GUI system control, head/tail file viewing.
How to install custom fonts on Linux
Install custom fonts on Linux: fc-list command, OTF/TTF file installation, font cache updates, verification steps.
Deploy Django on Ubuntu with Caddy && Gunicorn
Django deployment on Ubuntu with Caddy and Gunicorn: Gunicorn installation, worker configuration, Caddy reverse proxy.