CoachFullStack

Hosting your website on Linux server with free SSL certificate.

My first websites were hosted on a VPS. I wanted to setup something the old way. That always worked for me, when it comes to learning new things. In this guide, I will go through steps, that are necessary, to host your website on a Linux server, encrypted using SSL certificate from LetsEncrypt, for free.

Guide was tested on Debian Linux distribution.

What do you need before starting?

To follow through this guide, you will need to have access to a server with Linux installed and domain name registered. It is advised to know a little bit about Linux commands.

1. Install needed packages.

To host website on a Linux server, you need to have programs that will do it for you.

Main program that you need, is a webserver. It will listen for requests coming from the network and answer to them. Webserver I will use for the purpose of this guide is NGINX.

Encrypt your website with an SSL certificate, we will need certbot, that installs LetsEncrypt certificate for you.

Execute command:

sudo apt-get install nginx python-certbot-nginx

2. Check if it works.

Start your webserver.

sudo service nginx start

Open up your browser and go to your server IP address. You should see a website telling you, that it is served using NGINX.

3. Create your own HTML website.

I like to put websites into my home directory in their respective folders.

Commands bellow, will create simple HTML website in folders inside Home directory.

cd
mkdir websites
cd websites
mkdir <website_name>
echo "Hello World" > <website_name>/index.html

4. Update your NGINX configuration.

In this step we will create NGINX config file. For that I use vim, but you are free to use any text editor you like.

# for config name, I often use my domain name
sudo touch /etc/nginx/sites-enabled/<configname>
sudo vim /etc/nginx/sites-enabled/<configname>

Now, once you have your configuration file open, it is time to fill it with something useful. Also decide, if you want to use www subdomain or not, as your main website. I prefer to omit www for the purpose of this guide, so all www traffic will be redirected to non-www.

server {
    listen 80;
    server_name www.example.com;
    return 301 http://example.com$uri_request;
}

server {
    listen 80;
    server_name example.com;
    location / {
        root /home/<username>/websites/<website_name>;
        index index.html;
    }
}

5. Check if it works

First, reload your NGINX

sudo service nginx reload

Now go to your browser and go to your domain. You should see simple hello world on the screen. That means, that what have been done up to this point is working.

6. Install SSL certificate

This step assumes, that you have DNS records of your domain to point to your server.

As you may have noticed, most websites are encrypted, closed lock near the URL bar in browser indicates that. Right now your lock is open, as the website is not encrypted. Lets fix that.

We already installed certbot and now it is time to use it to install LetsEncrypt SSL certificate. Do do it, lets change your location to your configuration files location and start certbot.

cd /etc/nginx/sites-enabled/
sudo certbot

You will be prompted to fill out some data fields. When you will be asked which sites you want to encrypt, indicate which ones you want, by typing in corresponding numbers. In last step you will be asked, if you want certbot to modify your configuration in order to redirect your HTTP unencrypted traffic to HTTPS. My recommendation is that it is a good thing to do, otherwise you need to do it by hand.

7. Done

Reload NGINX

sudo service nginx reload

And go to browser.

After entering your website address into the bar, you should be able to see “Hello World!” and closed lock near the address.

Now instead of having “hello world” website, you can build your own website in the specified directory.



Next: Web Hosting Options Pros and Cons

Previous: First Few Minutes on Your Linux Server