How do I allow HTTPS for Apache on localhost?

Solution 1:

I've just attempted this - I needed to test some development code on my localhost Apache on Windows. This was WAAAY more difficult than it should be. But here are the steps that managed to work after much hairpulling...

I found that my Apache install comes with openssl.exe which is helpful. If you don't have a copy, you'll need to download it. My copy was in Apache2\bin folder which is how I reference it below.

Steps:

  1. Ensure you have write permissions to your Apache conf folder
  2. Open a command prompt in Apache2\conf folder
  3. Type
    ..\bin\openssl req -config openssl.cnf -new -out blarg.csr -keyout blarg.pem
  4. You can leave all questions blank except:

    • PEM Passphrase: a temporary password such as "password"
    • Common Name: the hostname of your server

  5. When that completes, type
    ..\bin\openssl rsa -in blarg.pem -out blarg.key

  6. Generate your self-signed certificate by typing:
    ..\bin\openssl x509 -in blarg.csr -out blarg.cert -req -signkey blarg.key -days 365

  7. Open Apache's conf\httpd.conf file and ensure SSL module is enabled - there should be no hash at the start of this line:
    LoadModule ssl_module modules/mod_ssl.so

  8. Some Apache installations place the SSL config in a separate file. If so, ensure that the SSL conf file is being included. In my case I had to uncomment this line:
    Include conf/extra/httpd-ssl.conf

  9. In the SSL config httpd-ssl.conf I had to update the following lines:

    • Update
      SSLSessionCache "shmcb:C:\Program Files (x86)\Zend\Apache2/logs/ssl_scache(512000)"
      to
      SSLSessionCache "shmcb:C:/Progra\~2/Zend/Apache2/logs/ssl_scache(512000)"
      (The brackets in the path confuse the module, so we need to escape them)
    • DocumentRoot - set this to the folder for your web files
    • ServerName - the server's hostname
    • SSLCertificateFile "conf/blarg.cert"
    • SSLCertificateKeyFile "conf/blarg.key"

  10. Restart Apache.

  11. Try loading https://localhost/ in your browser.

Hopefully you made it this far. Feel free to update this post with any other helpful info.

(Screenshots courtesy of Neil Obremski and his helpful article - although now quite out-of-date.)

Solution 2:

I use ngrok (https://ngrok.com/) for this. ngrok is a command line tool and create a tunnel for localhost. It creates both http and https connection. After downloading it, following command needs to be run :

ngrok http 80

( In version 2, the syntax is : ngrok http 80 . In version 2, any port can be tunneled. )

After few seconds, it will give two urls :

http://a_hexadecimal_number.ngrok.com
https://a_hexadecimal_number.ngrok.com

Now, both the urls point to the localhost.

Solution 3:

here is simplest way to do this

first copy these server.crt & server.key files (find in attachment ) into your apache/conf/ssl directory

then open httpd.conf file & add following line

Listen 80
Listen 443

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:443>
    DocumentRoot "d:/wamp/www"  #your wamp www root dir
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "d:/wamp/bin/apache/Apache2.4.4/conf/ssl/server.crt"
    SSLCertificateKeyFile "d:/wamp/bin/apache/Apache2.4.4/conf/ssl/server.key"
</VirtualHost>

Solution 4:

In order to protect the security of information being sent to and from your web server, it's a good idea to enable encryption of the communication between clients and the server. This is often called SSL.

So let's set up HTTPS with a self-signed certificate on Apache2. I am going to list the steps which you should follow:

  • Install apache2 web-server on your machine. For linux machine open the terminal and type

sudo apt-get install apache2

  • After successful installation check the status of apache2 service by executing command

sudo service apache2 status

It should output

apache2 service status

  • Navigate to browser and type

http://localhost:80

Verify that you get default page for apache2 like this.

default output of apache2

  • For encrypting a web connection we need certificate from CA (certificate authority) or we can use self signed certificates. Let's create a self signed certificate using the following command.

openssl req -x509 -newkey rsa:2048 -keyout mykey.key -out mycert.pem -days 365 -nodes

Please fill the information accordingly as shown below.

create self signed certificate using openssl

mykey.key and mycert.pem should be created in your present working directory.

  • It would be nice we if move certificates and keys at a common place and it will be easy for apache2 web server to find them. So let's execute the following commands

sudo cp mycert.pem /etc/ssl/certs

sudo cp mykey.key /etc/ssl/private

  • Let's enable the SSL mode on your server

sudo a2enmod ssl

It should output like this

enable ssl

  • Let's configure apache2 to use self signed certificate and key which we have generated above.

sudo vi /etc/apache2/sites-available/default-ssl.conf

Please find these two lines and replace them with your cert and key paths.

Initial

default-conf

Final

after config changes

  • Enable the site

cd /etc/apache2/sites-available/

sudo a2ensite default-ssl.conf

  • Restart the apache2 service

sudo service apache2 restart

  • Verify the apache2 web-server on HTTPS. Open your browser again and type

https://localhost:443

It should output something like this with a warning that page you are about to view is not secure because we have configured the server with self-signed certificate.

enter image description here

  • Congratulations you have configured your apache2 with HTTPS endpoint , now click on advanced --> add exception --> confirm security exception , you will see the default page again.

page after adding exception

Solution 5:

Windows + Apache 2.4, for example:

  1. uncomment ssl_module in your httpd.conf file.

    LoadModule ssl_module modules/mod_ssl.so
    
  2. listen 443 port just like 80 port in your httpd.conf file.

    Listen 80
    Listen 443
    
  3. uncomment Include Virtual hosts in your httpd.conf file.

    # Virtual hosts
    Include conf/extra/httpd-vhosts.conf
    
  4. add VirtualHost in your conf/extra/httpd-vhosts.conf

    <VirtualHost _default_:443>
        DocumentRoot "D:/www"  #your site directory path
        ServerName localhost
        #ServerAlias localhost.com localhost2.com
        SSLEngine on
        SSLCertificateFile "${SRVROOT}/conf/ssl/server.crt"
        SSLCertificateKeyFile "${SRVROOT}/conf/ssl/server.key"
        <Directory "D:/www">
            Options -Indexes +FollowSymLinks +ExecCGI
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    

only the port number 443 and SSL...... lines are different from normal http config.

save you config file and restart apache service. then you can visit https://localhost/

The web browser will warn you that it's unsafe at the first time, just choose go on.