Publish Your Startup Cheap — Nginx

Eren Yatkin
4 min readDec 6, 2020

--

Hello folks, couple of days ago I published my portfolio online. During that time I realised that guides out there are outdated. So I decided to create one for myself and thought sharing with you would be nice.

So let’s get you up and running!

You can follow me on Twitter and Linkedin for more content like this.

What are we going to do?

Well, we will install Nginx on a Ubuntu 20.04 server then configure it. Also will secure our web site by using Letsencrypt. Now we need;

  • Ubuntu 20.04 Server, I rented one from Digital Ocean they offer free credits, also you can find tons of coupon codes online to minimize your costs,
  • A domain,
  • Visual Studio Code, we will use it to connect remotely our server but you need to install SSH Extension.

So if you have all of the above,

Let’ start

Connect to the remote server

# if you have pem or any certificate
ssh -i "pathOfCert.pem" user@xx.xx.xx
# if you have credentials
ssh user@xx.xx.xx

Update the package installer and install Nginx

sudo apt-get update && sudo apt-get install nginx

Create a directory in the following path and move your website files into

# change erenyatkin.com with your domain
/var/www/erenyatkin.com

Configuring Nginx

etc/nginx/sites-available contains individual configuration files for all of your possible websites. You can create configuration files for static websites and applications.

etc/nginx/sites-enabled contains links to the configuration files that NGINX will actually read and run.

Create a file called your plain domain such as erenyatkin.com in sites-available and add the following configuration.

server {   
listen 80;
listen [::]:80;
root /var/www/erenyatkin.com;
index index.html;
server_name erenyatkin.com www.erenyatkin.com;
location / {
try_files $uri $uri/ =404;
}
}

Then we need to link that configuration file to sites-enabled so NGINX can enable it and serve.

ln -s /etc/nginx/sites-available/erenyatkin.com /etc/nginx/sites-enabled/erenyatkin.com

After all that, NGINX needs to be restarted so it can apply changes and new configurations. To do that;

sudo systemctl restart nginx

If it restarted without an error that means everything is Ok. Next step is to make our website secure with free tools which is Letsencrypt.

So let’s give it a shot;

Securing The Website

First thing first, we need to remove anything related to Certbot and install a clean copy of it.

sudo apt-add-repository -r ppa:certbot/certbot
sudo apt-get remove certbot
sudo apt-get install software-properties-common

Install snapd to install certbot

# Install sanpd for certbot
sudo apt install snapd
sudo snap install hello-world # to test the snap
# Install certbot
sudo snap install --classic certbot

Install the script which works with nginx (python-certbot-nginx is removed use below instead)

sudo apt-get install python3-certbot-nginx

Let’s create a certification. Run the following command, it will ask you to choose for which names you would like to enable HTTPS. Press Enter to select them all

sudo certbot --nginx certonly
--------------------------------------------------------------------Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Which names would you like to activate HTTPS for?
1: erenyatkin.com
2: www.erenyatkin.com

Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel)

So, we are almost done. We need to add one more thing. We created ourselves a SSL and we need to tell NGINX to use it. Let’s go back and change the configuration file in sites-available and new configuration should look like below. Do not forget to change erenyatkin.com with your own domain.

server {   
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /etc/letsencrypt/live/erenyatkin.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/erenyatkin.com/privkey.pem;
root /var/www/erenyatkin.com;
index index.html;
server_name erenyatkin.com www.erenyatkin.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 0.0.0.0:80;
server_name erenyatkin.com www.erenyatkin.com;
rewrite ^ https://$host$request_uri? permanent;
}

Restart NGINX again.

sudo systemctl restart nginx

Let’s check that out,

Eren Yatkin — Fullstack Software Developer

The End

So we reach to the end. Now we can publish our portfolios, startups, chatbots with SSL. Even we can install Redis or MongoDb in this server. If you encounter any problem you can ask me in the comments. Also if you have any questions or recommendations do not hesitate to share. Like if you liked it and share if you loved it. I wish you all a nice day.

Bonus

The SSL Certificate will expire after a while, so you need to re-create it. Create a cron job to do it for you. That way it can be renewed automatically.

Edit the crontab and create a CRON job to run the renewal command:

sudo crontab -e

Add the following line:

17 7 * * * certbot renew --post-hook "systemctl reload nginx"

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Eren Yatkin
Eren Yatkin

Written by Eren Yatkin

Fullstack software developer who wants to share his knowledge across the universe

No responses yet

Write a response