Using a Self signed Certificate to Run Apache2 under SSL

Generating a Self-Signed Certificate for Apache2 SSL

Generating a Self-Signed Certificate for Apache2 SSL

This guide will help you create a self-signed certificate to enable SSL for Apache2. While using a commercial certificate is preferable for production environments, a self-signed certificate is suitable for development or testing purposes.

Step 1: Enable SSL for Apache

First, enable the SSL module in Apache:

sudo a2enmod ssl

Step 2: Create a Directory for Certificates

Next, create a directory to store your self-signed certificate and its associated keys:

sudo mkdir /etc/apache2/certificate

Step 3: Generate the Self-Signed Certificate

Now, generate the keys for your self-signed certificate. You will be prompted to provide some information during this process:

sudo openssl req -new -x509 -days 1095 -nodes -out /etc/apache2/certificate/apache.pem -keyout /etc/apache2/certificate/apache.key

During this step, you will see output similar to this:


Generating a 1024 bit RSA private key
..............++++++
.......................++++++
writing new private key to '/etc/apache2/certificate/apache.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields, but you can leave some blank.
For some fields, there will be a default value.
If you enter '.', the field will be left blank.
-----

Step 4: Modify the ports.conf File

Next, you need to modify the ports.conf file to configure Apache to listen on HTTPS:

Open the file for editing:

sudo nano /etc/apache2/ports.conf

Add or update the following lines:


NameVirtualHost *:443
Listen 443 http

This change is important to avoid errors like: "Server should be SSL-aware but has no certificate configured [Hint: SSLCertificateFile] ((null):0)".

Step 5: Configure the Virtual Host

Now, locate the default SSL virtual host configuration file, usually found in /etc/apache2/sites-available/default-ssl. Edit this file to include the SSL configuration:

sudo nano /etc/apache2/sites-available/default-ssl

Add or update the following lines:


SSLEngine On
SSLCertificateFile /etc/apache2/certificate/apache.pem
SSLCertificateKeyFile /etc/apache2/certificate/apache.key

Step 6: Enable the SSL Site

Create a symbolic link to enable the SSL site:

sudo ln -s /etc/apache2/sites-available/default-ssl /etc/apache2/sites-enabled/000-default-ssl

Step 7: Restart Apache

Finally, restart the Apache service to apply your changes:

sudo systemctl restart apache2

You should now be able to access your server over HTTPS using a self-signed certificate.

Comments

  1. Many thanks for this sharing the complete procedure to generate a self signed certificate. I will do share this information with my friends too as we all are facing problem while doing the same.
    digital certificates

    ReplyDelete

Post a Comment

Popular posts from this blog

Server should be SSL-aware but has no certificate configured [Hint: SSLCertificateFile] ((null):0)

How to Efficiently Handle Sparse Files with tar