The Command
On Ubuntu and Debian:
sudo openssl req -x509 -newkey rsa:2048 -sha256 -days 365 -noenc \
-keyout /etc/ssl/private/example.com.key \
-out /etc/ssl/certs/example.com.crt \
-subj "/CN=example.com" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.com,IP:203.0.113.10"
sudo chmod 600 /etc/ssl/private/example.com.key
On AlmaLinux and Rocky Linux, use /etc/pki/tls/private/ for the key and /etc/pki/tls/certs/ for the certificate. The command needs no answers: -subj fills in the subject.
What Each Option Does
| Option | Meaning |
|---|---|
-x509 | Make a certificate, not a request |
-newkey rsa:2048 | Create a new 2048-bit RSA key |
-sha256 | Sign with SHA-256 |
-days 365 | Valid for one year |
-noenc | No passphrase on the key |
-subj | Subject, here only the name |
-addext | Adds an extension |
The subjectAltName extension matters most. Browsers check the requested name against this list and ignore the common name, so a certificate without it fails even in a test. List every name and IP address that clients will use. Without -noenc, the key gets a passphrase and the web server asks for it at every start. -noenc exists from OpenSSL 3.0, which every current release named here ships; the older spelling -nodes still works but is deprecated. Check your release with how to check the OpenSSL version.
For a smaller, faster ECDSA key, replace the key option with -newkey ec -pkeyopt ec_paramgen_curve:prime256v1.
Check the Certificate
openssl x509 -in /etc/ssl/certs/example.com.crt -noout -subject -dates -ext subjectAltName
The output shows the subject, the start and end dates, and the alternative names. If the names list is missing, the -addext part was not applied.
Use It in Nginx
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
root /var/www/example.com;
}
Test and reload:
sudo nginx -t && sudo systemctl reload nginx
In Apache, the same two files go in SSLCertificateFile and SSLCertificateKeyFile.
What Browsers and Clients Do
Browsers show a full-page warning, because no trusted authority signed the certificate. Command-line tools refuse the connection: curl needs --cacert /etc/ssl/certs/example.com.crt, or -k for a one-off test. On your own computer you can import the certificate, never the key, into the browser's or the system's trust store to remove the warning for yourself. For several internal services, a small private certificate authority whose root you distribute once is easier to manage than many single certificates.
When a Self-Signed Certificate Fits
- Testing: a staging server or a local setup where only you connect.
- Internal services: an admin tool, a database, or a message queue reached only by your own clients, which you can configure to trust the certificate.
- An origin behind a proxy that does not verify: some CDN settings encrypt to the origin without checking the certificate. A strict setting needs a trusted or provider-issued origin certificate instead, or visitors see Cloudflare error 526; see Cloudflare error 525 for handshake problems between the proxy and the origin.
- Not for a public website: visitors see a warning and most leave. Use a Let's Encrypt certificate there, as in Certbot with Nginx.
Keep the Private Key Private
Give the key mode 600, owned by root. Never paste it into a chat, a support ticket, or a repository; a .gitignore rule for *.key helps. The certificate itself is public and can be shared. If a key leaks, create a new key and certificate and replace both everywhere.