Learning to create self-signed SSL certificates for XAMPP

Learning to create self-signed SSL certificates for XAMPP

Feb 15, 2024

Creating a self-signed SSL certificate for local development in XAMPP using OpenSSL involves several steps. Here's a step-by-step guide:

Prerequisites

  • Ensure XAMPP is installed on your system.
  • XAMPP comes with OpenSSL, but ensure it’s accessible through your command line or terminal by setting up its path.

Step-by-Step Guide

Open Command Line or Terminal:

  • On Windows, you can use the Command Prompt or PowerShell.
  • On macOS or Linux, use the Terminal.

Navigate to the XAMPP Apache Directory:

  • Typically, this is located at C:\xampp\apache on Windows. On macOS or Linux, the path might be different.
  • Use the cd command to navigate to this directory.

Create a Private Key:

  • Run the following command:
openssl genrsa -out private.key 2048

This command generates a 2048-bit RSA private key and saves it as private.key.

Create a Self-Signed Certificate:

  • Execute this command:
openssl req -new -x509 -key private.key -out cert.crt -days 365
  • You will be prompted to enter details like country, state, organization, etc. Fill these out as needed.
  • The -days 365 option sets the certificate to expire in one year. Adjust this value as necessary.
  • This creates a self-signed SSL certificate named cert.crt.

Copy the Key and Certificate to the Correct Directories:

  • Copy private.key to C:\xampp\apache\conf\ssl.key\ (create the ssl.key directory if it doesn't exist).
  • Copy cert.crt to C:\xampp\apache\conf\ssl.crt\ (create the ssl.crt directory if it doesn't exist).

Configure Apache to Use the SSL Certificate:

  • Open the httpd-ssl.conf file located in C:\xampp\apache\conf\extra\.
  • Find the lines that start with SSLCertificateFile and SSLCertificateKeyFile.
  • Modify them to point to your new certificate and key:
SSLCertificateFile "conf/ssl.crt/cert.crt"
SSLCertificateKeyFile "conf/ssl.key/private.key"

Save the changes.

Restart the Apache Server:

  • Open the XAMPP Control Panel.
  • Stop and then start the Apache server to apply the changes.

Testing the Configuration

  • Open a web browser and navigate to https://localhost.
  • You might see a security warning because the browser does not recognize your self-signed certificate as trusted. This is expected for local development. You can proceed to view your site.

Notes

  • Self-signed certificates are suitable for local development and testing. They should not be used in a production environment.
  • Ensure that your application's configuration, if any, also points to the correct URLs (https vs http).

By following these steps, you should have a working local SSL setup using XAMPP and OpenSSL.