A self-signed certificate is an SSL certificate that is signed by the person or organization creating it, rather than a trusted Certificate Authority (CA). You can use a self-signed certificate for testing purposes, or to provide encryption within internal networks. If you are looking into renewing a self-signed SSL, please take a look at How to Renew Self-Signed SSL Certificate with OpenSSL Tool in Linux
Here’s the step-by-step process for creating a self-signed certificate on an Ubuntu server:
Step 1: Install OpenSSL
OpenSSL is a tool that provides a library for secure communication over networks. If it’s not already installed, you can install it with the following command:
Step 2: Create a Private Key
The first step in creating an SSL certificate is to generate a private key. This key will be used to encrypt data and should be kept secure.
Run the following command to generate a 2048-bit RSA private key:
This command will create a password-protected key named myserver.key
. If you don’t want to use a passphrase, you can use:
Step 3: Create a Certificate Signing Request (CSR)
The next step is to create a Certificate Signing Request (CSR). This request contains information about your organization and will be included in the certificate. The CSR is normally sent to a CA, but for a self-signed certificate, you’ll sign it yourself.
Use the following command to create the CSR:
You will be prompted to provide information such as:
- Country Name: The two-letter ISO code for your country (e.g.,
US
). - State or Province: The full name of your state or province.
- Locality Name: The full name of your city.
- Organization Name: The name of your organization.
- Organizational Unit Name: The division or department within the organization.
- Common Name: The fully qualified domain name (FQDN) of your server (e.g.,
example.com
). - Email Address: Your email address.
You can leave some fields blank if they’re not applicable.
Step 4: Create a Self-Signed Certificate
Now you can create the self-signed certificate by running the following command:
This command creates a certificate (myserver.crt
) that will be valid for 365 days. You can adjust the -days
parameter if you want a different expiration period.
Step 5: Install the Certificate
To install the certificate on your server, you will need both the .crt
file (the certificate) and the .key
file (the private key). The process of installation depends on which web server you are using. Below are instructions for Apache and Nginx.
For Apache:
-
Copy the certificate and key files to the
/etc/ssl/certs/
and/etc/ssl/private/
directories, respectively: -
Modify your Apache configuration file (usually located at
/etc/apache2/sites-available/default-ssl.conf
or similar) to use the new certificate and key: -
Enable the SSL module and the site configuration, then restart Apache:
For Nginx:
-
Copy the certificate and key files to a secure location, for example:
-
Edit your Nginx configuration file (often located at
/etc/nginx/sites-available/default
) and add the following lines in theserver
block: -
Test the Nginx configuration and restart the service:
Step 6: Verify the Certificate
Once the certificate is installed, you can verify that it is working by using your browser to access the site via HTTPS. You will likely get a warning that the certificate is not trusted because it’s self-signed, but this is expected.
If you want to view the certificate details, you can use the following command:
This will display information such as the issuer, expiration date, and subject details.
Optional: Combine Certificate and Key in a .pem File (If Needed)
Some applications require the private key and the certificate to be bundled together in a .pem
file. You can create this bundle by running:
That’s it! You now have a self-signed certificate installed on your Ubuntu server. Keep in mind that self-signed certificates are not trusted by browsers or external clients by default, so they are generally used for development or internal purposes.