11

I am using Apache 2.2 on 12.04. I have activated ssl connection with a self-signed certificate which works fine, but now I'd like to disable any non-ssl connection.

I used a2dissite default but the server is still accessible on port 80 even after restarting the server.

Please help me on this.

Peachy
  • 7,117
  • 10
  • 38
  • 46
user87954
  • 251

2 Answers2

14

I finally have it working:

In addition to disabling the default page with: a2dissite default, I edited /etc/apache2/ports.conf and commented the following lines:

NameVirtualHost *:80  
Listen 80
user87954
  • 251
  • I had a snap application listening on port 80. This solution stopped apache from listening on port 80 so that requests could go through to the app. – Edwin Chua Jan 21 '21 at 13:47
12

A better idea is to keep "non-ssl connection" (http), but permanently redirected to your SSL Virtual Host (https). In this case the .conf file could look like:

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

    # Redirect Requests to SSL
    Redirect permanent &quot;/&quot; &quot;https://www.example.com/&quot;

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

</VirtualHost>

<IfModule mod_ssl.c>

    &lt;VirtualHost _default_:443&gt;

            ServerName www.example.com
            ServerAdmin admin@example.com

            DocumentRoot /var/www/html/www.example.com

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

            SSLEngine on

            # other configuration directives...

    &lt;/VirtualHost&gt;

</IfModule>

Related topics:

pa4080
  • 29,831
  • May you elaborate why is a better idea to not disable HTTP? I'm investigating pros and cons of disabling port 80. – Marco Marsala Apr 11 '18 at 07:39
  • 5
    @MarcoMarsala, in most cases, when HTTP (port 80) is disabled and you are type in the browser http://your.domain.com (or just your.domain.com) you will receive "page not found" - unless you type https://your.domain.com ... – pa4080 Apr 11 '18 at 07:51