How to Enable HTTPS for Your Django Project on an Ubuntu VPS with Apache: A Comprehensive Guide
T o secure your Django application with HTTPS, clean up your Apache configuration file by removing unrelated code. Then, move your Django settings to a Virtual Host configuration for port 443, including SSL directives. This ensures your application is served securely over HTTPS.
Step-by-Step Guide:
1. Access Your Remote Server via SSH:
ssh -p PORT USERNAME@HOSTIP
2. Update Your Package List:
sudo apt update
3. Install Certbot and Apache Plugin: Certbot automates SSL certificate installation. Use:
sudo apt install certbot python3-certbot-apache
4. Configure Apache Virtual Host for HTTP:
To enhance the security of your Django application by enabling HTTPS, follow these steps to modify your Apache configuration:
sudo vi /etc/apache2/sites-available/django.amankhalsa.in.conf
<VirtualHost *:80>
ServerAdmin admin@django.amankhalsa.in
ServerName django.amankhalsa.in
DocumentRoot /var/www/django.amankhalsa.in/django_personal
RewriteEngine on
RewriteCond %{SERVER_NAME} =django.amankhalsa.in
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
5. Configure Apache Virtual Host for HTTPS: Add the following SSL configuration:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin@django.amankhalsa.in
ServerName django.amankhalsa.in
DocumentRoot /var/www/django.amankhalsa.in/django_personal
Alias /static /var/www/django.amankhalsa.in/django_personal/staticfiles
<Directory /var/www/django.amankhalsa.in/django_personal/staticfiles>
Require all granted
</Directory>
Alias /media /var/www/django.amankhalsa.in/django_personal/media
<Directory /var/www/django.amankhalsa.in/django_personal/media>
Require all granted
</Directory>
<Directory /var/www/django.amankhalsa.in/django_personal/postfolio>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django_personal python-path=/var/www/django.amankhalsa.in/django_personal python-home=/var/www/django.amankhalsa.in/django_personal/my_env
WSGIProcessGroup django_personal
WSGIScriptAlias / /var/www/django.amankhalsa.in/django_personal/postfolio/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/django_amankhalsa_error.log
CustomLog ${APACHE_LOG_DIR}/django_amankhalsa_access.log combined
SSLCertificateFile /etc/letsencrypt/live/django.amankhalsa.in-0001/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/django.amankhalsa.in-0001/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
sudo ufw allow 'Apache Full'
sudo ufw delete allow 'Apache'
6. Test Apache Configuration and Reload:
sudo apache2ctl configtest sudo systemctl reload apache2
7. Obtain and Install the SSL Certificate: Run Certbot to configure your SSL certificate:
sudo certbot --apache
Follow the prompts for email and domain information.
8. Verify SSL Certificate Renewal:
Check Certbot status and perform a dry run:
sudo systemctl status certbot.timer
sudo certbot renew --dry-run
Working after this below command 1. Update Database File Permissions Assuming your SQLite database is named db.sqlite3: bash Copy code sudo chmod 664 /var/www/django.amankhalsa.in/django_personal/db.sqlite3 sudo chown www-data:www-data /var/www/django.amankhalsa.in/django_personal/db.sqlite3 2. Update Directory Permissions Set the correct permissions on the project directory: bash Copy code sudo chmod 775 /var/www/django.amankhalsa.in/django_personal sudo chown www-data:www-data /var/www/django.amankhalsa.in/django_personal After adjusting the permissions, try accessing the admin panel again to see if the error is resolved.
0 Comments