In this tutorial, you will learn how to generate a self-signed certificate and use it with an nginx web server on a Debian Linux system. The self-signed certificate will be valid for 365 days. To run nginx, we will use a helpful tool, namely Docker. In the Docker container we will include the configuration file and the generated public key pair files.
You need a virtual server from netcup with Debian or Ubuntu installed. Also, this tutorial requires a Docker setup. For instructions on setting up Docker on Ubuntu, see the netcup community tutorials.
Check your docker version:
sudo docker -v
The output should look like the following, although version and build may be different:
Docker version 20.10.10, build b485636
Run the following command to install OpenSSL and curl:
sudo apt-get install openssl curl
Create the self-signed certificate:
openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out public.crt -keyout private.key
You need to answer some questions on the command line about the certificate, such as questions about your website or about your organization. Normally, a self-signed certificate is used for a private use case, so the information is not that important.
For example, you can enter these values and press Enter after each answer:
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:Berlin
Locality Name (eg, city) []:Berlin
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Test Company Name
Organizational Unit Name (eg, section) []:Test Organisation Unit
Common Name (e.g. server FQDN or YOUR name) []:your-netcup-domain.net
Email Address []:test-email-address@your-netcup-domain.net
In the next lines you find a description of all the OpenSSL options used in this command:
-newkey rsa:4096: Create a 4096 bit RSA key, which is used for the certificate. RSA (Rivest–Shamir–Adleman) is the cryptography algorithm and 4096 the generated key size in bit.
-x509: Create a self-signed certificate.
-sha256: Request of the certificate generation using 256-bit SHA (Secure Hash Algorithm)
-days: Defines how many days the certificate is valid. In the case of a self-signed certificate, this value can be very low or very high.
-nodes: Don't require a passphrase. If you remove this option, you have to enter the passphrase in the commandline each time the application starts.
Use a text editor of your choice to create an index.html file in the current directory:
vi index.html
Insert the following:
<html>
<head></head>
<body>Hello World!</body>
</html>
Save the file.
Use a text editor of your choice to create the default.conf
configuration file in the current directory:
vi default.conf
Insert the following:
server {
listen 443 ssl;
server_name _;
ssl_certificate /etc/certs/public.crt;
ssl_certificate_key /etc/certs/private.key;
location / {
root /usr/share/nginx/html;
}
}
Save the configuration file.
The following command creates a new container with the base nginx:1.21.4 image and mounts various volumes into the container. If the base image doesn't exist in the local registry, Docker will pull the image first. Run the following command to start a new container named nginx:
docker run -d --name nginx \
-p 443:443 \
-v $(pwd)/public.crt:/etc/certs/public.crt \
-v $(pwd)/private.key:/etc/certs/private.key \
-v $(pwd)/index.html:/usr/share/nginx/html/index.html \
-v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf nginx:1.21.4
To check the status of the created container, run this command:
docker ps -a
The output should look like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2c2c05b2a64 nginx:1.21.4 "/docker-entrypoint.…" 54 seconds ago Up 52 seconds 80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx
Run this command on the command line:
curl --insecure https://localhost:443
The output should look like this:
<html>
<head></head>
<body>Hello World!</body>
</html>
Open you browser and enter the URL but don't forget to change the IP address: https://<ip-address-of-your-netcup-server>:443
.
Your browser displays a warning page. To accept the warning, click on "Expanded" and then on "Accept risk and continue".
Now you should see the message "Hello World!".
sudo docker stop nginx
sudo docker rm nginx
rm public.crt
rm private.key
rm index.html
rm default.conf
You learned how to generate a self-signed certificate and how to configure nginx to use the generated public key pair. You also learned how to start nginx in a Docker container and mount various files into the container. Self-signed certificates can be valid for a very long period of time, for example, more than a year. You can specify the number of days in the command line.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.