#!/linuxSucks/Setup a website with Nginx

Dec 19, 2020

Install nginx

This setup is for a simple html static web site in a online server.

Servers and Domains

I recommend this two sites for your servers and domains, This two sites are well know and very cheap.

After you have the server and domain, Install (Debian):

sudo apt install nginx certbot python-certbot-nginx

If it’s Ubuntu server change python-certbot-nginx to python3-certbot-nginx

When everything is installed, create your website as follow

Create directory

Create the file for you website, in there is where you website will live, run:

mkdir /var/www/yourdomain

Then create a simple index.html

echo "<h1>Under Constructions</h1>" >> /var/www/yourdomain/index.html

With that in place, set up your site in nginx.

Nginx Setup

Create a copy of the default configuration file or create a new file (if you create a new files skip the following command)

cp /etc/nginx/sites-avaiable/default /etc/nginx/sites-avaiable/yourdomain

Edit the new config file (i use vim)

vim /etc/nginx/sites-avaiable/yourdamin

Copy and past the following and change it to domain (this config sample is for html only)

server {
    listen 80;
	listen [::]:80;

    server_name your_domain www.yourdomain;
    root /var/www/yourdomain;

    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ /\.ht {
        deny all;
    }

}

Saved it close it. Then we need to enable the new site:

ln -s /etc/ngnx/site-avaiable/yourdomain /etc/ngnx/site-enable/

Now you need to reload/restart you nginx server

systemctl reload nginx

And just for fun (if you want it) restart nginx

systemctl restart nginx

Certbot

Before you run certbot, you need to setup your domain and cnames in your domain provider. If you didn’t do it, certbot will fail.

Run:

certbot --nginx

Follow the instructions, below are the output of the instructions

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): your@email.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: yourdomain
2: www.yourdomain
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 2

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for yourdomain
http-01 challenge for www.yourdomain
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/yourdomain
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/yourdomain

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/yourdomain
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/yourdomain

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://yourdomain,
https://yourdomin, https://www.yourdomain, and
https://www.yourdomain

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=yourdomain
https://www.ssllabs.com/ssltest/analyze.html?d=yourdomain
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/dkdraperyhardware.xyz/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/dkdraperyhardware.xyz/privkey.pem
   Your cert will expire on 2021-03-21. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

If everything goes well you won’t see any error on the screen. Give it a minute to update then try in your web browser, your domain will be re-direct to https.

Add a cronjob to renew certbot

To renew the certificate run

certbot renew

Another way is to add a cronjob to do that run this command in the terminal

crontab -e

At the end of the files add:

1 1 1 * * certbot renew --quiet && systemctl reload nginx

What this does? It says run certbot renew every first of the mount. That will take care of the https in the server.

Delete a Certificate

If you want to delete a certificate

certbot delete

Then just select the domain you want to delete

Home  Linux  Notes  Blog Spot