How to host your own server and make it public

Hello world,

Today I’m going to cover how you can host your own server and make it available to the public. Be aware that this will open a port on your router that could introduce vulnerabilities to your intranet. For this exercise I’m going to use a Raspberry Pi B+ running the linux distro Raspbian. Jessie is the newest stable version available at the moment I write this post.

Tips before we start

If you are using the raspberry as I did and you want to make this fast make sure you do the following: * Do not use X. Use the shell * Don’t SSH if possible. Try to execute commands directly from your server. That will reduce the overhead communication * Make sure your connection is good, so there are no lost packages. If possible connect your raspberry directly to your router, via a ethernet cable. That will reduce the time packages travel and will avoid losing packages being lost due to noise

1 – Get your local server up and running

For that we are going to install NGINX. NGINX is an incredible open source web server. It’s easy to configure and it has incredible performance. To install on any debian distro such as Jessie, just run the following code:

sudo apt-get install nginx

If everything went well, try typing:

curl localhost

And you should see the NGINX message welcoming you

2 – Opening up ports to the outside world

This part will require forwarding a port from your local server that you just setup to your router. You can think this as a bridge or tunnel, as it is known. Now, you need to make your router forward all the income traffic that hits your public IP (that your network provider assigns to you) in the port 80 to reach your local server.

First things first. You need to get the IP of your server. To do that run the command:

ifconfig eth0 | grep 'inet addr'

If you are using wifi, probably you are using wlan0. You can check all your network status using only ifconfig

Secondly, we are going to log in into our router. For that it’s much better if you use your own machine and use a browser (firefox, chrome, etc). Normally you can connect to your router by going to the address: 192.168.1.1. Sometimes you can access by 192.168.1.0. My router is a Linksys and I could access with the former IP address. If you don’t remember your password, or if you have never changed it (now is your chance to change it), your password could be something like “admin”, “adm”, “admin1234”, or even a blank string “”. Seriously… Check it out from your router’s manual. If none of those options work for you, routers have a physical switch that you can press for a few seconds and it will reset to its default configuration. You’ll need to check the default password from the manual, though.

In my router, after I successfully logged in, I had to go to the tab “Security”, followed by “Apps and Gaming”. Once there, there was a button to add a new single port forwarding. I clicked and added the information of my simple server. Below a screen shot on how it looked like. drawing

A little explanation here: * Internal port: this is the port your server is exposing. By default NGINX exposes port 80 * External port: this is the port people will be able to hit. If you are going to use SSL, you are going to use port 443, otherwise leave 80 in this field. Those are the ports your browser will look by default (besides 8080 as well). * Device IP: you put the IP of your server here * Protocol: for HTTP you will be fine with either TCP or Both. UDP is more for gaming/stream

You can finally access your server from the outside world. But what IP did your provider give to you? You can check easily by just typing on google “What’s my ip”, or you can use this code in the terminal:

dig +short myip.opendns.com @resolver1.opendns.com

Go to your browser now, or curl that IP and you should see the NGINX welcome message again! You should be able to hit the IP of your router from any device. If you have data connection with your phone, turn your WiFi off and test with that.

3 – Registering a domain

Now the next step from here is to set your web server to use a DNS. There are many services that can register a domain for you. Companies like Namecheap and Google, for example. I’ve used both and they work greatly. Never had any problem with both of them. Although, I liked namecheap better. You can check the whole list here https://www.icann.org/registrar-reports/accredited-list.html

4 – Configuring DNS

Although each DNS registrar has its own setup, you should be able to access a session that will enable you to manage your DNS configuration. What you will need to do, is go to the session that will enable you to edit your domain. Over there you have to map the settings with your IP or corresponding alias.

Some explanation first on what the terms mean: * A type – The record A specifies IP address (IPv4) for given host. A records are used for conversion of domain names to corresponding IP addresses. * AAAA type – The record AAAA (also quad-A record) specifies IPv6 address for given host. So it works the same way as the A record and the difference is the type of IP address. * CNAME – Specifies a domain name that has to be queried in order to resolve the original DNS query. Therefore CNAME records are used for creating aliases of domain names. CNAME records are truly useful when we want to alias our domain to an external domain. In other cases we can remove CNAME records and replace them with A records and even decrease performance overhead. Don’t use a CNAME record for your root domain name (ex: marcusrenno.tech) * The @ symbol in your DNS record refers to the record for your domain name without any www or sub-domain name. * TTL – this the time to live. It’s recommended to leave either automatic or a decent amount of time, so users can cache information.

Example:

| Type | host | value | TTL | | —– | —- | —– | —- | | A record | @ | 74.83.215.86 | automatic | | CNAME | www | marcusrenno.tech | automatic | {.table}

This will make people see my website when the try http://74.83.215.86/, https://www.marcusrenno.tech/ and https://marcusrenno.tech/

5 – Next steps

From here you will have to configure your server to display real content and not that welcome message 🙂 It’s up to you what kind of service you will choose. For this blog I’m using Django for my backend, Ember.js for my frontend (although I could have used Django as well). Well, I’m also using PostgreSQL to persist my data and Express.js for server side rendering.

Cheers!

Resource

www.cyberciti.biz https://www.name.com/support/articles/205516858-Understanding-DNS-record-types https://serverfault.com/questions/426326/what-is-the-name-for-a-dns-record-starting-with http://dns-record-viewer.online-domain-tools.com/


Leave a Reply

Your email address will not be published. Required fields are marked *