openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj '/CN=localhost'

Category: Web Servers & Proxies

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj '/CN=localhost'

Generate a self-signed certificate and private key

-x509 makes openssl issue a self-signed certificate instead of a CSR, and -newkey rsa:2048 creates a fresh RSA key, with -nodes leaving it unencrypted so a server can read it without a passphrase. -subj sets the subject non-interactively, perfect for scripts. Use this for local testing only, since browsers will warn about the trust chain.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.