DEV Community

Yuval
Yuval

Posted on • Originally published at thecodingnotebook.com on

Generating Self-Signed SSL certificates for development

Howto: generate multi-domain self-signed certificates (and use with nginx)

ssl

Intro

Often it is required to generate self-signed certificates to be used during development, this is a way to do it on a Linux machine (WSL is good too).

NOTE: Do NOT use such certificates in production - such certificates are not trusted by browsers.

Step 1: Copy conf template

cd ~
cp /usr/lib/ssl/openssl.cnf .
Enter fullscreen mode Exit fullscreen mode

Step 2: Edit the conf file adding domains

Edit the file above, insert the following line immediately BEFORE the “HOME” entry:

SAN="email:your-email@domain.com"
Enter fullscreen mode Exit fullscreen mode

Add the following line immediately AFTER the [v3_req] and [v3_ca] section markers:
(add as much domains as needed)

subjectAltName=DNS:sub1.domain.com,DNS:sub2.domain.com
Enter fullscreen mode Exit fullscreen mode

Step 3: Generate the certificate

openssl req -new -x509 -sha256 -days 365 -nodes -out cert.pem -keyout cert_key.pem -config openssl.cnf
Enter fullscreen mode Exit fullscreen mode

To view the cert:

openssl x509 -in cert.pem -noout -text
Enter fullscreen mode Exit fullscreen mode

Use certificate in Nginx

In step 3 we generated 2 files, cert.pem and cert_key.pem, copy them to where you like having your certificates, /etc/nginx/ssl is a good place.
Next we'll use these files in our nginx config.

Edit the nginx conf file (usually /etc/nginx/nginx.conf but it really depends on your setup).
Find the server section and add the ssl conf:

server {
  server_name: domain.com;
  listen 80;

  # SSL
  listen 443 ssl;
  ssl_certificate /etc/nginx/ssl/cert.pem;
  ssl_certificate_key /etc/nginx/ssl/cert_key.pem;
}
Enter fullscreen mode Exit fullscreen mode

Add to "trusted certificates" on Windows

If you'll try to browse to a site using that certificate Chrome will give error as the certificate is not trusted.
In order to "trust" this certificate:

  1. Using chrome, browse to some site that is using the certificate
  2. Click on "View Certificate"
  3. Click on "Copy to File..." and save the certificate as a ".der" file
  4. open Windows "Manage User Certificates" settings
  5. Right-click on "Trusted Root Certification Authoroties/Certificates" -> All Tasks -> Import
  6. select the saved certificate from above
  7. restart chrome

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more