Table of Contents
Setting Up an NginX Web-and Mailserver
Preparations:
- Search for a cheap Website-Host (i use Hetzner, alternatives are Linode and Vultr)
- Create an Account
- Get the cheapest cloud server which runs Debian on it (you can upgrade the Power and Storage later when your Website has actual traffic)
Domain:
- Choose any DNR you want (i use njalla, because no personal data is required)
- Create an Account and buy your wished Domain (.xyz endings are pretty cheap)
- Edit the DNS Records so that they point to your Server
DNS resolution involves converting a host name, e.g. www.example.com, into a computer-friendly IP address, e.g. 192.168.1.1.
This is how to edit your DNS Records on the Server to redirect to your yourdomain.xyz
Type | Name | Points to | TTL |
A (IPV4) | www | your IPV4 Adress | 5m |
A (IPV4) | @ | your IPV4 Adress | 5m |
A (IPV4) | * | your IPV4 Adress | 5m |
AAAA (IPV6) | www | your IPV6 Adress | 5m |
AAAA (IPV6) | @ | your IPV6 Adress | 5m |
AAAA (IPV6) | * | your IPV6 Adress | 5m |
Logging into a Server
We first want to log into our Server. Therefore pull up a terminal and type (*Note you can also replace the yourdomain.xyz with the IP address of your server):
ssh root@yourdomain.xyz
Installing Nginx
If the program runs without an error, ssh has now logged you into your server. Let's start by running the following commands.
apt update apt upgrade apt install nginx
The first command checks for packages that can be updated and the second command installs any updates. The third command installs nginx (pronounced Engine-X), which is the web server we'll be using, along with some other programs.
Configuring the Webserver Nginx
Nginx is your webserver. You can make a little website or page, put it on your VPS and then tell nginx where it is and how to host it on the internet. It's simple. Let's do it.
Nginx configuration files are in /etc/nginx/. The two main subdirectories in there (on Debian and similar OSes) are /etc/nginx/sites-available and /etc/nginx/sites-enabled. The names are descriptive. The idea is that you can make a site configuration file in sites-available and when it's all ready, you make a link/shortcut to it in sites-enabled which will activate it.
Create a file in /etc/nginx/sites-available by doing this:
nano /etc/nginx/sites-available/yourwebsite
Note: “nano” is a command line text editor. You will now be able to create and edit this file. By hitting enter, this file will now appear.
Add the following content to the file.
server { listen 80 ; listen [::]:80 ; server_name yourdomain.xyz ; root /var/www/yoursite ; index index.html index.htm index.nginx-debian.html ; location / { try_files $uri $uri/ =404 ; } }
If you want to hide .html on your domain-endings (https://yourdomain.xyz/home.html) replace from 'location / {':
location / { if ($request_uri ~ ^/(.*)\.html(\?|$)) { return 302 /$1; } try_files $uri $uri.html $uri/ =404; }
Explanation of those settings:
The listen lines tell nginx to listen for connections on both IPv4 and IPv6.
The server_name is the website that we are looking for. By putting yourdomain.xyz here, whenever someone connects to this server and is looking for that address, they will be directed to the content in this block.
root specifies the directory we're going to put our website files in. This can theoretically be wherever, but it is conventional to have them in /var/www/. Name the directory in that whatever you want.
index determine what the “default” file is; normally when you go to a website, say yourdomain.xyz, you are actually going to a file at yourdomain.xyz/index.html. That's all that is. Note that that this in concert with the line above mean that /var/www/yourdomain.xyz/index.html, a file on our computer that we'll create, will be the main page of our website.
Lastly, the location block is really just telling the server how to look up files, otherwise throw a 404 error. Location settings are very powerful, but this is all we need them for now. Create the directory and index for the site
We'll actually start making a “real” website later, but let's go ahead and create a little page that will appear when someone looks up the domain.
mkdir /var/www/yoursite
Now let's create an index.html file inside of that directory, which will appear when the website is accessed:
nano /var/www/yoursite/index.html
I'll add the following basic content, but you can add whatever you want. This will appear on your website.
<!DOCTYPE html> <h1>My website!</h1> <p>This is my website. Thanks for stopping by!</p> <p>Now my website is live!</p>
Enable the site
Once you save that file, we can enable it making a link to it in the sites-enabled directory:
ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled
Now we can just reload or restart to make nginx service the new configuration:
systemctl reload nginx
You can now visit your up and running webpage.
Encrypt your Site
In order to get the green official certified Page Symbol (everyone wants) we need to install a certification bot, therefore type:
apt-install python3-certbot-nginx
After the installationprocess is finished you just have to type in the following command. Press enter and choose the domain's you want to encrypt and then press enter a couple more times.
cerbot --nginx
Pushing and Pulling your Website Files
This part will be a repetetive task (feel free to automate it with a script). In order to upload your local files (from the device you are using to read this guide), you need to install rsync.
rsync -vrP --delete-after ~/path/to/websitefiles root@yourdomain.xyz:/var/www/yoursite/
Now we are finished, the website should update automatically immeadietly after you synced your files via rsync (no need to restart the server).