TL;DR
This guide shows you how to create a strong private key for your website’s HTTPS security. We’ll cover using OpenSSL, storing it safely, and important considerations.
Generating Your Private Key
- Choose a Strong Algorithm: RSA is common, but ECC (Elliptic Curve Cryptography) offers better security for the same key length. For new keys, consider ECDSA with curves like secp384r1 or secp521.
- Use OpenSSL: OpenSSL is a powerful command-line tool available on most systems. If it’s not installed, you’ll need to install it (e.g.,
sudo apt-get install opensslon Debian/Ubuntu). - Generate the Key (RSA Example): This creates a 2048-bit RSA key.
openssl genrsa -out yourdomain.key 2048 - Generate the Key (ECC Example): This creates an ECDSA key using the secp384r1 curve.
openssl ecparam -name prime256v1 -genkey -noout -out yourdomain.key - Secure Key Permissions: Immediately after generation, restrict access to the key file. Only the user running your web server should be able to read it.
chmod 400 yourdomain.key
Storing Your Private Key
- Never Store in Web Root: Absolutely avoid placing the key file within your website’s document root (e.g.,
/var/www/html). This is a major security risk. - Dedicated Directory: Create a dedicated, restricted directory for keys.
mkdir /etc/ssl/privateMove the key file there:
mv yourdomain.key /etc/ssl/private/ - File System Permissions: Ensure only the web server user can read the key.
chown www-data:www-data /etc/ssl/private/yourdomain.key(Replace
www-datawith your web server’s user.) - Backups: Create secure backups of the key, stored offline and encrypted. Consider a hardware security module (HSM) for critical applications.
Important Considerations
- Key Length: For RSA, 2048 bits is the minimum recommended length. 3072 or 4096 bits are stronger. ECC keys of equivalent security require shorter lengths (e.g., secp384r1).
- Passphrase Protection: You can encrypt the private key with a passphrase during generation.
openssl genrsa -des3 -out yourdomain.key 2048You’ll be prompted for a passphrase. *However*, automating server setup becomes harder, and you need to securely manage the passphrase.
- Regular Rotation: Periodically rotate your private keys (e.g., every year or after a security incident).
- Cyber security Best Practices: Keep your OpenSSL version up-to-date to patch vulnerabilities. Regularly audit server configurations and logs.

