Report this

What is the reason for this report?

Django Getting apache default page using ssl certificate

Posted on February 21, 2023

I have my Django project with an SSL certificate from Let’s Encrypt, deployed on an Ubuntu 22.04 (LTS) x64 virtual machine from Digital Ocean.

When I navigate to the page using my domain, it redirects me to my page correctly, but when I use the IP address, it redirects me to the apache default page.

I have a PTR record active on the Digital Ocean site:

IPv4: ip address ....... 
AddressPTR Record: [www.mydomain.com](http://www.mydomain.com/).

The ip address added to the ALLOWED_HOSTS var on my settings.py:

ALLOWED_HOSTS = ['www.mydomain.com', 'my ip address']

What I have in /etc/apache2/sites-available/my_project.conf:

<VirtualHost *:80>

ServerName mydomain.com

ServerAlias [www.mydomain.com](http://www.mydomain.com/)

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

RewriteEngine on

RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]

RewriteCond %{SERVER_NAME} =mydomain.com

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

The traffic allowed in my server:

Status: active

---

22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
Apache Full ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)

Before getting the SSL certificate, it worked using the ip address as well but, when I set the certificate, it just returns the apache default page. I set the certificate following this five step tutorial:https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04

I hope that’s the right information in order to get an answer, I’m learning Django and Web Development in general, I hope this question makes sense.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

What you could do here is to update your default Apache virtual host which is the virtual host that servers the traffic for your IP and add the rewrite rule in there as well, eg:

RewriteEngine on

RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]

RewriteCond %{SERVER_NAME} =mydomain.com

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

Once you make the change, do a quick config test with sudo apachectl -t and if you get Syntax Ok restart Apache.

That way when someone visits your IP, they will be redirected to your domain which would serve the correct content ovar HTTPS.

Let me know how it goes!

Best,

Bobby

Hello,

It sounds like the issue you’re encountering is related to Apache’s configuration for handling requests made directly to the IP address of your server, especially after setting up SSL with Let’s Encrypt. When you access your site using the domain name, Apache correctly redirects you to the HTTPS version of your site. However, when accessing it via the IP address, it shows the default Apache page, which indicates that there is no specific configuration for handling requests to the IP address.

Here are some steps you can take to troubleshoot and resolve this issue:

  1. Check VirtualHost Configuration for SSL: Ensure that you have a <VirtualHost> block for port 443 (SSL) in your Apache configuration. This should be in addition to your existing block for port 80 (HTTP). The configuration for port 443 should include the SSLCertificateFile and SSLCertificateKeyFile directives pointing to your Let’s Encrypt certificates. It might look something like this:
<VirtualHost *:443>
    ServerName mydomain.com
    ServerAlias www.mydomain.com

    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /path/to/certificate.crt
    SSLCertificateKeyFile /path/to/private.key

    # Other directives...
</VirtualHost>
  1. Default VirtualHost for IP Address: You may need to set up a default VirtualHost for handling requests made directly to the IP address. This can be a separate <VirtualHost> block that either redirects to your domain or serves the same content. For example:
<VirtualHost *:80>
    ServerName default
    Redirect permanent / https://www.mydomain.com/
</VirtualHost>

And similarly for port 443 if you want to handle HTTPS requests to the IP address.

  1. Restart Apache: After making any changes to the Apache configuration files, remember to restart Apache to apply these changes. You can do this with the command:
sudo systemctl restart apache2
  1. Check for Conflicting Configurations: Make sure there are no conflicting configurations in other Apache sites-enabled files. Sometimes, a default configuration file (like 000-default.conf) can interfere with your custom settings.

  2. Examine Apache Logs: If the problem persists, check the Apache error logs for any relevant messages. These logs can provide valuable insights into what might be going wrong. The error logs are usually located in /var/log/apache2/error.log.

  3. Firewall and Network Configuration: Ensure that your firewall and network settings on Digital Ocean are correctly configured to allow traffic to both ports 80 and 443.

  4. DNS Configuration: While it seems like your DNS is set up correctly, it’s always good to double-check that your domain’s DNS settings are pointing to the correct IP address.

By following these steps, you should be able to configure Apache to handle requests made directly to the IP address in the same way it handles requests made to the domain. Remember, it’s generally a good practice to redirect IP address requests to the domain name, especially for public-facing websites.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.