2

After many hours I finally managed to configure Apache to use my SSL certificate but i just found out that there is something wrong in my configuration. If I try to reach my domain from Google Chrome by just entering the following address I get the following warning:

 erichermansson.com

 Forbidden
 You don't have permission to access / on this server. 

But if I enter the following address in Google Chrome I can access the server and its working with my certificate:

 https://erichermansson.com

What do I do wrong?

Here is my VirtualHost:

 <VirtualHost *:443>
ServerAdmin admin@erichermansson.com
ServerName erichermansson.com
ServerAlias www.erichermansson.com
SSLEngine ON
SSLCertificateFile /www/erichermansson.com/ssl/erichermansson.com.crt
SSLCertificateKeyFile /www/erichermansson.com/ssl/erichermansson.com.key
DocumentRoot /www/erichermansson.com/html/
ErrorLog /www/erichermansson.com/logs/error.log
CustomLog /www/erichermansson.com/logs/access.log combined
<Directory /www/erichermansson.com/html/ >
     Options FollowSymLinks
     AllowOverride All
     Order deny,allow
     Allow from all
 </Directory>
</VirtualHost>
  • Have you defined a VirtualHost: entry for port 80 somewhere ? – Sree Jun 8 '16 at 17:02
  • And does Apache have read permission on /var/erichermansson.com/html? – garethTheRed Jun 8 '16 at 17:02
  • @Sreeraj No I have not done that, where do I do that? – Eric Hermansson Jun 8 '16 at 17:14
  • @garethTheRed Yes, Apache have read permission on the HTML folder. – Eric Hermansson Jun 8 '16 at 17:15
3

For HTTP to redirect to HTTPS you need to (the first 3 steps you probably already did):

  1. Allow port 80 in your router.

  2. Forward port 80 to your server.

  3. Punch hole in your firewall:

    sudo iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    
  4. Define VirtualHost for port 80 (as well as for 443):

    <VirtualHost *:80>
      ServerName             erichermansson.com
      ServerAlias            www.erichermansson.com
      RewriteEngine          on
      RewriteCond            %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteRule            ^(.*)$ https://%1/$1 [R]
      RewriteCond            %{HTTPS} !on
      RewriteRule            ^/?(.*) https://%{SERVER_NAME}/$1 [R=301]
    </VirtualHost>
    
    <IfModule mod_ssl.c>
    
      <VirtualHost *:443>
    
      ... your code here ...
    
      </VirtualHost>
    
    </IfModule>
    
  5. Activate mod_rewrite:

    sudo a2enmod rewrite
    
  6. Define a redirect from HTTP to HTTPS, something like:

    RewriteCond            %{HTTPS} !on
    RewriteRule            ^/?(.*) https://%{SERVER_NAME}/$1 [R=301]
    

    which we have already done step 4.

  7. And finally restart Apache:

    sudo service apache2 restart
    
  • Thanks for your answer. Can you be more specific how I define a VirtualHost for port 80 and how I use mod_rewrite? Thanks! – Eric Hermansson Jun 8 '16 at 17:16
  • Take your time! :) – Eric Hermansson Jun 8 '16 at 17:17
  • 1
    if he is getting that error, port 80 is not firewalled, as Apache is answering. The problem at the moment is that the vhost is not listening to port 80. – Rui F Ribeiro Jun 8 '16 at 17:17
  • Would not one RewriteCond rule be enough? It if is a new site, a RedirectPermanent is simpler and does the job. – Rui F Ribeiro Jun 8 '16 at 17:56
  • @RuiFRibeiro This code I use on my server. I am no expert on rewrites, always been busy not to learn it properly. If you are able to improve it, I would appretiate it. – LinuxSecurityFreak Jun 8 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.