
You don’t know how to install Nginx and enable the HTTPS feature on an Ubuntu Linux computer to use a self-signed certificate? In this tutorial, we’ll configure the Nginx server to use HTTPS and create a self-signed certificate.
To enable the HTTPS in NGINX we need to install the Nginx server and the required packages. You must be in root privileges.
# apt-get update
# apt-get install nginx openssl
Let’s create a private key and the website certificate using the OpenSSL command.
# mkdir /etc/nginx/certificate
# cd /etc/nginx/certificate
# openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out nginx-certificate.crt -keyout nginx.key
Enter the requested information.
Generating a RSA private key
............++++
.......................................................++++
writing new private key to 'nginx.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.
-----
Country Name (2 letter code) [AU]:PH
State or Province Name (full name) [Some-State]:Philippines
Locality Name (eg, city) []:Manila
Organization Name (eg, company) [Internet Widgits Pty Ltd]:AnyTechTutorials
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:400.400.400.400
Email Address []:
On the option named COMMON_NAME, you need to enter the IP address or hostname.
In our example, we used the IP address 400.400.400.400
Edit the Nginx configuration file for the default website.
# vi /etc/nginx/sites-available/default
This is the file, before our configuration.
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;
}
}
This is the file, after our configuration.
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/nginx/certificate/nginx-certificate.crt;
ssl_certificate_key /etc/nginx/certificate/nginx.key;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
This is optional if you want to redirect HTTP users to the HTTPS version of your website.
In this case, use the following configuration.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/nginx/certificate/nginx-certificate.crt;
ssl_certificate_key /etc/nginx/certificate/nginx.key;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
Restart the Nginx service.
service nginx restart
Open your browser and access the HTTPS version of your website.
In our example, the following URL was entered in the Browser:
https://400.400.400.400
The Nginx server will display the HTTPS version of your website.
