How to Add HTTPS for the Domain api-domainname.com on Localhost XAMPP


Step 1: Create Directory and SSL Certificate

Open Command Prompt (CMD) as Administrator.

Navigate to the Apache directory of XAMPP:

cd E:\xampp\apache\bin

Create a directory to store the SSL certificate:


mkdir E:\xampp\apache\conf\ssl

If there is no openssl.cnf file, create a new file with the following content:

[ req ]
default_bits       = 2048
default_md         = sha256
distinguished_name = req_distinguished_name
req_extensions     = req_ext
x509_extensions    = v3_ca
prompt             = no

[ req_distinguished_name ]
C  = US
ST = USA CITY
L  = USA CITY
O  = TECH
OU = IT Department
CN = api-domainname.com

[ req_ext ]
subjectAltName = @alt_names

[ v3_ca ]
subjectAltName = @alt_names
basicConstraints = CA:true
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer

[ alt_names ]
DNS.1 = api-domainname.com

Run the command to create the SSL certificate:


set OPENSSL_CONF=E:\xampp\apache\bin\openssl.cnf openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout E:\xampp\apache\conf\ssl\api-domainname.com.key -out E:\xampp\apache\conf\ssl\api-domainname.com.crt

Step 2: Configure Apache

Open the Apache configuration file httpd.conf:

E:\xampp\apache\conf\httpd.conf

Uncomment the following lines to enable SSL and Virtual Hosts:

LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-ssl.conf

Open the Apache SSL configuration file httpd-ssl.conf:


E:\xampp\apache\conf\extra\httpd-ssl.conf

Add the Virtual Host for api-domainname.com:

<VirtualHost *:443>
    DocumentRoot "E:/xampp/htdocs/@api-domainname.com"
    ServerName api-domainname.com

    SSLEngine on
    SSLCertificateFile "E:/xampp/apache/conf/ssl/api-domainname.com.crt"
    SSLCertificateKeyFile "E:/xampp/apache/conf/ssl/api-domainname.com.key"

    <Directory "E:/xampp/htdocs/@api-domainname.com">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 3: Configure Non-SSL Virtual Host (Optional)

Open the file httpd-vhosts.conf:


E:\xampp\apache\conf\extra\httpd-vhosts.conf

Add the Virtual Host for the domain api-domainname.com without using SSL:

<VirtualHost *:80>
    DocumentRoot "E:/xampp/htdocs/@api-domainname.com"
    ServerName api-domainname.com

    <Directory "E:/xampp/htdocs/@api-domainname.com">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 4: Add Domain to Hosts File

Open the hosts file on Windows:


C:\Windows\System32\drivers\etc\hosts

Add the following line at the end of the file to point the domain api-domainname.com to localhost:


127.0.0.1 api-domainname.com

Access E:\xampp\apache\conf\ssl and install the api-domainname.com.crt certificate.

Step 5: Restart Apache

Restart Apache from the XAMPP Control Panel.

Step 6: Access the Domain

Open your browser and go to https://api-domainname.com.

Done!