How To Create a Self-Signed SSL Certificate for Apache in Debian 9

How To Create a Self-Signed SSL Certificate for Apache in Debian 9

Step 1. Creating the SSL Certificate

TLS/SSL works by using a combination of a public certificate and a private key. The SSL key is kept secret on the server. It is used to encrypt content sent to clients. The SSL certificate is publicly shared with anyone requesting the content. It can be used to decrypt the content signed by the associated SSL key.

We can create a self-signed key and certificate pair with OpenSSL in a single command:

$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt

*. openssl: This is the basic command line tool for creating and managing OpenSSL certificates, keys, and other files.

*. req: This subcommand specifies that we want to use X.509 certificate signing request (CSR) management. The “X.509” is a public key infrastructure standard that SSL and TLS adheres to for its key and certificate management. We want to create a new X.509 cert, so we are using this subcommand.

*. -x509: This further modifies the previous subcommand by telling the utility that we want to make a self-signed certificate instead of generating a certificate signing request, as would normally happen.

*. -nodes: This tells OpenSSL to skip the option to secure our certificate with a passphrase. We need Apache to be able to read the file, without user intervention, when the server starts up. A passphrase would prevent this from happening because we would have to enter it after every restart.

*. -days 365: This option sets the length of time that the certificate will be considered valid. We set it for one year here.

*. -newkey rsa:2048: This specifies that we want to generate a new certificate and a new key at the same time. We did not create the key that is required to sign the certificate in a previous step, so we need to create it along with the certificate. The rsa:2048 portion tells it to make an RSA key that is 2048 bits long.

*. -keyout: This line tells OpenSSL where to place the generated private key file that we are creating.

*. -out: This tells OpenSSL where to place the certificate that we are creating.

Fill out the prompts appropriately. The most important line is the one that requests the Common Name (e.g. server FQDN or YOUR name). You need to enter the domain name associated with your server or, more likely, your server’s public IP address,"127.0.1.1" in case of localhost.

Both of the files you created will be placed in the appropriate subdirectories under  /etc/ssl .

Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:MH
Locality Name (eg, city) []:Pune
Organization Name (eg, company) [Internet Widgits Pty Ltd]:XYZ, Inc.
Organizational Unit Name (eg, section) []:This n That
Common Name (e.g. server FQDN or YOUR name) []:server_IP_address
Email Address []:admin@server_IP_address

Step 2. Configuring Apache to Use SSL

We have created our key and certificate files under the /etc/ssl directory. Now we just need to modify our Apache configuration to take advantage of these.

We will make a few adjustments to our configuration:

  1. We will create a configuration snippet to specify strong default SSL settings.
  2. We will modify the included SSL Apache Virtual Host file to point to our generated SSL certificates.
  3. (Recommended) We will modify the unencrypted Virtual Host file to automatically redirect requests to the encrypted Virtual Host.

When we are finished, we should have a secure SSL configuration. So now,

1. Creating an Apache Configuration Snippet with Strong Encryption Settings

First, we will create an Apache configuration snippet to define some SSL settings. This will set Apache up with a strong SSL cipher suite and enable some advanced features that will help keep our server secure. The parameters we will set can be used by any Virtual Hosts enabling SSL.

Create a new snippet in the /etc/apache2/conf-available directory. We will name the file ssl-params.conf to make its purpose clear:

$ sudo vi /etc/apache2/conf-available/ssl-params.conf

File Content:

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder On
# Disable preloading HSTS for now. You can use the commented out header line that includes
# the "preload" directive if you understand the implications.
# Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"

Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
# Requires Apache >= 2.4
SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
# Requires Apache >= 2.4.11
SSLSessionTickets Off

Save and close the file when you are finished.

2. Modifying the Default Apache SSL Virtual Host File

Next, let's modify /etc/apache2/sites-available/default-ssl.conf, the default Apache SSL Virtual Host file. If you are using a different server block file, substitute its name in the commands below.

Before we go any further, let’s back up the original SSL Virtual Host file:

$ sudo cp /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-available/default-ssl.conf.bak

Now, open the SSL Virtual Host file to make adjustments:

$ sudo vi /etc/apache2/sites-available/default-ssl.conf

Inside, with most of the comments removed, the Virtual Host block should look something like this by default:

<IfModule mod_ssl.c>

<VirtualHost _default_:443>

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLEngine on

SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem

SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

<FilesMatch "\.(cgi|shtml|phtml|php)$">

SSLOptions +StdEnvVars

</FilesMatch>

<Directory /usr/lib/cgi-bin>

SSLOptions +StdEnvVars

</Directory>

</VirtualHost>

</IfModule>

Lets MAKE A MINOR ADJUSTMENTS in the file as :

<IfModule mod_ssl.c>

<VirtualHost _default_:443>

ServerAdmin your_email@server_domain_or_IP

ServerName server_domain_or_IP

DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLEngine on

SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt

SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

<FilesMatch "\.(cgi|shtml|phtml|php)$">

SSLOptions +StdEnvVars

</FilesMatch>

<Directory /usr/lib/cgi-bin>

SSLOptions +StdEnvVars

</Directory>

</VirtualHost>

</IfModule>

Save and close the file when you are finished.

3. (Recommended however not must) Modifying the HTTP Host File to Redirect to HTTPS

-----If you do not want or need this functionality, you can safely skip this section.-----

As it stands now, the server will provide both unencrypted HTTP and encrypted HTTPS traffic. For better security, it is recommended in most cases to redirect HTTP to HTTPS automatically.

To adjust the unencrypted Virtual Host file to redirect all traffic to be SSL encrypted, open the /etc/apache2/sites-available/000-default.conf file:

$ sudo vi /etc/apache2/sites-available/000-default.conf

Inside, within the VirtualHost configuration blocks, add a Redirect directive, pointing all traffic to the SSL version of the site:

<VirtualHost *:80>

. . .

Redirect "/" "https://your_domain_or_IP/"

. . .

</VirtualHost>

Save and close the file when you are finished.

Step 3. Enabling the Changes in Apache

Now that we've made our changes, we can enable the SSL and headers modules in Apache, enable our SSL-ready Virtual Host, and then restart Apache to put these changes into effect.

Enable mod_ssl, the Apache SSL module, and mod_headers, which is needed by some of the settings in our SSL snippet, with the a2enmod command:

$ sudo a2enmod ssl

$ sudo a2enmod headers

Next, enable your SSL Virtual Host with the a2ensite command:

$ sudo a2ensite default-ssl

You will also need to enable your ssl-params.conf file, to read in the values you've set:

$ sudo a2enconf ssl-params

At this point, the site and the necessary modules are enabled. We should check to make sure that there are no syntax errors in our files. Do this by typing:

$ sudo apache2ctl configtest

If everything is successful, you will get a result that looks like this:

Syntax OK

As long as your output has Syntax OK in it, then your configuration file has no syntax errors and you can safely restart Apache to implement the changes:

$ /etc/init.d/apache2 restart

OR Reload Apache Server to activate the new configurations:

$ service apache2 reload

With that, your self-signed SSL certificate is all set. You can now test that your server is correctly encrypting its traffic.

Ref : https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-apache-in-debian-9x


ON side note, if you have your local certificate file provided by your organization that you want to add to your new server's trusted store (in my case, on my local new Debian setup) so to make curl or openssl commands work with secured URLs -

  • Naming the certificates with .crt extensions. Possibly you might get .pem (LINUX) OR .cer (WINDOWS) certificate file from your organization. For .cer (WINDOWS) files, you might need to first convert it to .pem format prior renaming it to .crt with following command :

    openssl x509 -inform der -in /usr/share/ca-certificates/Zcales-Root-CA.cer -out /usr/share/ca-certificates/Zcales-Root-CA.pem

    Then, just rename the file to change extension to .crt to make it as /usr/share/ca-certificates/Zcales-Root-CA.crt

  • Now, put file into /usr/share/ca-certificates if not already in it, and then run update-ca-certificates OR update-ca-certificates --fresh.

There is a Possibility that above Step 2 to run update-ca-certificates won't work. In that case, "dpkg-reconfigure" is at rescue.

$ dpkg-reconfigure ca-certificates // This command generally used FOR NEWER BUILDs based on DEBIAN

And then continue on selecting the certificates you want to install. Make sure you locate the certificate file we've created in above Step 1 in this process.

Generally,

Common CA (Certificate Authority) certificates are in : /usr/share/ca-certificates/

Certificates are installed into : /etc/ssl/certs

They will be compiled into a single file : /etc/ssl/certs/ca-certificates.crt

Add new comment