Install an OpenSSL self-signed certificate with nginx
Published on 3 min read
Updated on
Seriesgnu-linux-infrastructure
Part 2 of 6In this series22 min read in total
- Installing Docker on Debian
- Install an OpenSSL self-signed certificate with nginx
- Create a DNS server on Debian
- Install Nginx + Webdav on debian 11
- Create an OpenVPN server on debian
- SSH connection with public key
Generate the certificate and the key
sudo openssl req -x509 -days 365 -out mycert.crt -nodes -newkey rsa:4096 -keyout mykey.keyFill in the different information of the certificate
Generating a RSA private key..................................+++++........................................................+++++writing new private key to 'mykey.key'-----You are about to be asked to enter information that will be incorporatedinto 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 blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:FRState or Province Name (full name) [Some-State]:RHONELocality Name (eg, city) []:mavilleOrganization Name (eg, company) [Internet Widgits Pty Ltd]:xsecOrganizational Unit Name (eg, section) []:ITCommon Name (e.g. server FQDN or YOUR name) []:web.it.frEmail Address []:test@gmail.comOpenSSL has generated the certificate and the key in the directory where you are at the time you entered the command. You can move/rename them as you wish.
Edit the nginx configuration file
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; }}-
At the top of the file is a server configuration block. It listens on port
80:HTTPwhich points to/var/www/htmlthen an index page defined inindexline 5. -
As we are going to add a server configuration block that listens on port
443:HTTPS, we need to change it to avoid conflicts:
server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 301 https://$host$request_uri;}Ligne 5Redirects requests from port 80 to port 443.
Add the SSL configuration to the end of the file
server { listen 443 ssl; server_name _; root /var/www/html; ssl_certificate /var/www/sites/client1/mycert.crt; ssl_certificate_key /var/www/sites/client1/mykey.key; index index.html index.htm index.nginx-debian.html;}Ligne 4Remplacer le chemin par le votre.Ligne 5-6Remplacer les chemins par les votres.
The configuration file then looks like this (without the comments):
server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 301 https://$host$request_uri;}
server { listen 443 ssl; server_name _; root /var/www/html; ssl_certificate /var/www/sites/client1/mycert.crt; ssl_certificate_key /var/www/sites/client1/mykey.key; index index.html index.htm index.nginx-debian.html;}Restart nginx
sudo service nginx restart