DEV Community

Emily Flias
Emily Flias

Posted on • Updated on

How can I set up SSL on localhost (https://localhost/)?

Before we start, it's important to note that SSL certificates are typically issued for domain names, not IP addresses or localhost. However, you can still create a self-signed SSL certificate for localhost that can be used for testing purposes.

Here are the steps to set up SSL on localhost:

Generate a self-signed SSL certificate

On Windows, you can use a tool like OpenSSL or XAMPP to generate a self-signed SSL certificate. On Linux or macOS, you can use the OpenSSL command-line tool.
Open a command prompt or terminal and navigate to the directory where you want to generate the SSL certificate.
Enter the following command to generate a private key:

openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out csr.pem
Enter fullscreen mode Exit fullscreen mode

Enter the following command to generate a self-signed SSL certificate:

openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem
Enter fullscreen mode Exit fullscreen mode

This will generate a self-signed SSL certificate and save it to a file called "cert.pem" in the current directory.
Install the SSL certificate

On Windows, you can install the SSL certificate by double-clicking the cert.pem file and following the prompts in the Certificate Import Wizard.
On Linux or macOS, you can copy the cert.pem file to the /usr/local/share/ca-certificates/ directory and then run the following command to update the system's certificate store:

sudo update-ca-certificates
Enter fullscreen mode Exit fullscreen mode

Configure your web server to use SSL

If you're using Apache, you can enable SSL by uncommenting the following line in the httpd.conf file:

LoadModule ssl_module modules/mod_ssl.so
Enter fullscreen mode Exit fullscreen mode

Then, create a virtual host for your localhost site that uses the SSL certificate:

<VirtualHost *:443>
    ServerName localhost
    DocumentRoot /path/to/localhost
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Replace /path/to/localhost with the path to your localhost site's directory, and replace /path/to/cert.pem and /path/to/key.pem with the paths to your SSL certificate and private key files, respectively.
If you're using Nginx, you can enable SSL by adding the following lines to your server block:

listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
Enter fullscreen mode Exit fullscreen mode

Again, replace /path/to/cert.pem and /path/to/key.pem with the paths to your SSL certificate and private key files, respectively.
Test the SSL connection

Open a web browser and navigate to http://localhost/. You should see a warning that the site's certificate is not trusted, since it's self-signed. However, you should still be able to proceed to the site.
If you're using Chrome, you may need to click the "Not Secure" button in the address bar and then click "Certificate (invalid)" to view the certificate.

How can I run phpmyadmin on localhost with SSL?

  • Follow the steps to generate a self-signed SSL certificate and configure Apache to use SSL as i said above answer.
  • Open the phpMyAdmin configuration file, which is located in the "phpmyadmin" directory of your XAMPP installation (e.g., C:\xampp\phpmyadmin\config.inc.php).
  • Add the following lines to the end of the file:
$cfg['ForceSSL'] = true;
$cfg['SSLVerify'] = false;
Enter fullscreen mode Exit fullscreen mode
  • The first line forces phpMyAdmin to use SSL, and the second line disables SSL verification. Since you're using a self-signed certificate, the SSL verification will fail unless you install the certificate as a trusted certificate on your computer, but for development purposes, it's okay to disable it.
  • Save the changes to the configuration file.
  • Restart Apache in the XAMPP control panel.
  • Open a web browser and navigate to https://localhost/phpmyadmin. You should now be able to access phpMyAdmin using SSL. You may see a warning about the self-signed certificate, but you should be able to proceed to the site.

Top comments (0)