How do I allow HTTPS for Apache on localhost?
I was asked to set up HTTPS with a self-signed cert on Apache on localhost, but how do I actually do that? I have no idea at all.
Use Serveo! ssh -R youruniquesubdomain:80:localhost:3000 serveo.net Slap in your subdomain and port number and you ready to go on https://youruniquesubdomain.serveo.net
@Timo Seems like Serveo is dead, but localhost.run does the same: ssh -R 80:localhost:8080 ssh.localhost.run
@totymedli, awesome answer-comment! I had this going in a couple of minutes, didn’t even read any of those verbose answers, lol.
@totymedli how am I suppose to use this service. I do run the command in the windows terminal. but from there where to?
16 Answers 16
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.
- Ensure you have write permissions to your Apache conf folder
- Open a command prompt in Apache2\conf folder
- Type
..\bin\openssl req -config openssl.cnf -new -out blarg.csr -keyout blarg.pem - You can leave all questions blank except:
- PEM Passphrase: a temporary password such as «password»
- Common Name: the hostname of your server
..\bin\openssl rsa -in blarg.pem -out blarg.key
..\bin\openssl x509 -in blarg.csr -out blarg.cert -req -signkey blarg.key -days 365
LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
- 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»
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.)
How can I install SSL on localhost in Ubuntu?
I want to install an SSL certificate on my localhost in Ubuntu environment because I can’t work on the production server directly. I have to put some conditions in my code on the basis of whether the page is HTTP or HTTPS. How can I do this?
Installing an SSL certificate isn’t the same thing as installing SSL itself. Don’t fall into the lazy habit of using ‘SSL’ to mean ‘SSL certificate’. It isn’t the same thing and it just adds confusion and ambiguity to your question. Please clarify which it is you’re talking about.
1 Answer 1
Enable the Apache module by typing:
After you have enabled SSL, you’ll have to restart the web server for the change to be recognized:
sudo service apache2 restart
Let’s start off by creating a subdirectory within Apache’s configuration hierarchy to place the certificate files that we will be making:
Now that we have a location to place our key and certificate, we can create them both in one step by typing:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
The questions portion looks something like this:
Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:New York Locality Name (eg, city) []:New York City Organization Name (eg, company) [Internet Widgits Pty Ltd]:Your Company Organizational Unit Name (eg, section) []:Department of Kittens Common Name (e.g. server FQDN or YOUR name) []:your_domain.example Email Address []:your_email@domain.example
Open the file with root privileges now:
sudo nano /etc/apache2/sites-available/default-ssl.conf
With the comments removed, the file looks something like this:
ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog $/error.log CustomLog $/access.log combined SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key SSLOptions +StdEnvVars SSLOptions +StdEnvVars BrowserMatch "MSIE 4" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 BrowserMatch "MSIE 17" ssl-unclean-shutdown
In the end, it will look something like this. The entries were modified from the original file:
ServerAdmin admin@example.com ServerName your_domain.example ServerAlias www.your_domain.example DocumentRoot /var/www/html ErrorLog $/error.log CustomLog $/access.log combined SSLEngine on SSLCertificateFile /etc/apache2/ssl/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/apache.key SSLOptions +StdEnvVars SSLOptions +StdEnvVars DirectoryIndex index.php AllowOverride All Order allow,deny Allow from all BrowserMatch "MSIE 4" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 BrowserMatch "MSIE 17" ssl-unclean-shutdown
Save and exit the file when you are finished. Now that we have configured our SSL-enabled virtual host, we need to enable it.
sudo a2ensite default-ssl.conf
We then need to restart Apache to load our new virtual host file:
sudo service apache2 restart
That’s it now run your site with https.