How to generate a self-signed SSL certificate on Apache web server?

To generate a self-signed SSL certificate for an Apache web server, follow these steps:

  1. Install OpenSSL library: Make sure OpenSSL is installed on your system. You can use your system’s package manager to install it. For example, on Ubuntu or Debian, use the command:
sudo apt-get install openssl
  1. Generate private key: Generate a private key using the openssl genrsa command. Specify the desired key length in bits. For example, to generate a 2048-bit key, use:
openssl genrsa -out privkey.pem 2048
  1. Create CSR (Certificate Signing Request): Generate a CSR using the openssl req command. Specify the private key file and the common name (CN) of the domain for which you are generating the certificate. For example, to generate a CSR for the domain example.com, use:
openssl req -new -key privkey.pem -out csr.pem -subj "/CN=example.com"
  1. Generate self-signed certificate: Generate a self-signed certificate using the openssl x509 command. Specify the CSR file, the private key file, and the number of days for which the certificate should be valid. For example, to generate a certificate valid for 1 year (365 days), use:
openssl x509 -req -days 365 -in csr.pem -signkey privkey.pem -out cert.pem
  1. Configure Apache to use self-signed certificate: Modify the Apache configuration file (usually located at /etc/apache2/sites-enabled/000-default.conf). Add the following lines to the VirtualHost section for your domain:
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/privkey.pem
  1. Restart Apache: Restart the Apache web server for the changes to take effect:
sudo systemctl restart apache2

Now your Apache web server is using a self-signed SSL certificate. Web browsers will display a warning about the certificate being self-signed, but the connection will be secure.

Leave a Reply

Your email address will not be published. Required fields are marked *