A beginners tutorial to hosting websites in GNU/Linux
Ever wanted to host your own website? Have a spare PC laying around (even REALLY old) that you can use? This tutorial is going to show you how!
Now, three disclaimers I am going to give:
- This tutorial will be done nearly entirely with the command line. I personally find it much easier, and less time consuming to do this sort of work in a terminal, plus it’s good practice to anyone who decides they want to work with remote servers in the future for hosting a website off-site.
- This tutorial will not be covering hardening, SSL certificates, advanced HTML/CSS/Javascript nor advanced DNS work. This is simply going to show you how to put a website up for others to view, it’s up to you to design it!
- This tutorial serves as a test, to see the interest in this sort of thing on Ghacks. If reception is good, I will cover more advanced topics relating to server work, webhosting, email servers, hardening, etc. If you the reader is more interested in keeping things on a ‘home user’ level, do let me know in the comments!
For this, we will be using the Nginx webserver. There are multiple webservers available, but the two most common are Apache and Nginx. My production servers all use Nginx, and without drowning you with information about why, I’ll summarize it as, “Better performance.â€
This tutorial will assume you are running Debian, Ubuntu, or one of their derivatives; as they are the most common distributions, and Debian is one of the most popular server operating systems anyway due to it’s rock solid stability.
Hosting your own website: The Process
Note: The first thing we want to do is ensure that our system has the latest updates:
- sudo apt update
- sudo apt upgrade
If you have any updates to do, agree to them and let it happen.
Next, we want to install nginx:
- sudo apt install nginx
Nginx should only take a few moments to install, it’s very small. Once that is done, you’ll want to make sure that nginx is running:
- sudo sysemctl status nginx
OR if not using systemd:
- sudo service nginx status
You should receive some text, telling you something like:
- nginx start/running, process 5142 <-- If you are using sysinit
- and for systemd: Active: active (running) since XXX XXXXX XXXX (Today’s date and time)
Testing nginx
A good way to test nginx and reconfirm that it’s running, is to visit the test page! (That’s right, from the moment nginx installed itself, you have been broadcasting a test page to the internet!)
First, we need to find out what our IP address is. If you are connected to the internet via ethernet, you can try this command:
- ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Or, you can use a command called curl, used to transfer URL data, to check on a website called http://icanhazip.com:
- curl -4 http://icanhazip.com/
Now that we have our IP address, enter it into your webbrowser using a different device than the machine you are running nginx on:
- http://11.111.11.11 (your IP address)
You should find the default nginx welcome page!
Directories, and further setup tips
The last part of this very simple tutorial, is knowing the important nginx directories.
- /var/www/html or /var/www/ <- - Where your website is stored. You will want to replace “index.html†with your own index file / your homepage!
- /etc/nginx <-- The main nginx configuration directory, where all the important files you may one day need to edit are stored, such as nginx.conf
- /etc/nginx/sites-available <--The main directory where configurations or “Server blocks†for each website you host, are stored. This is where the files you need to edit in the future for changing things like SSL are stored.
- /etc/nginx/sites-enabled <-- files from “Sites Availble†must be symlinked here, before nginx will serve the page to the web.
This is the absolute barebones basic of nginx hosting.
Domain names, and final thoughts
Having nginx installed and running is fine and dandy, but we need a domain name!
Freenom is an amazing little website offering year long subscriptions without the need for registration, to free domain names, such as myfreetestsite.tk or Miketurcotteisawesome.tk
Feel free to register a domain name to use for your website! You will need a basic understand of how to point your domain name to your sever using DNS. I found a handy little website specifically using freenom, showing how to do that in about 30 seconds.
Once that is done, people can then enter http://yournamehere.tk (or whatever your domain name is) and visit your website.
What are your thoughts? Ever had the desire to put something up on the internet? Would tutorials like this be of interest? Give me some feedback in the comments!
Yes, I would love a series on this and others like it. Please.
I definitely want to put up a website or two. It is a bit intimidating and I have to figure out if copying and pasting from an article is a practical way of using the command line or if learning it properly is a realistic option for me. I’m interested in reading about these things in any case.
I for one am always looking forward to reading these tutorials here at gHacks. Started playing around with linux just a few years ago an as of a couple weeks ago, after installing the creators update, linux became my sole OS. Linux in general is so versatile wish i had done that years ago.
“from the moment nginx installed itself, you have been broadcasting a test page to the internet!”
Please, PLEASE revise the article to remove that ridiculous “broadcasting” claim (and, in followup, delete this comment)
Thanks for the tutorial. This is something I already know how to do, but I think it is very useful for others nevertheless.
What I think that you should cover in more detail when it comes to self hosting is the private/public IP issue, because not everyone has a public IP address, and they risk publishing their site without realizing that it won’t be visible to anyone on the internet.
if running centos this helps too if not using a web admin panel
http://centminmod.com/
Huh, No kidding, just earlier this week, I was wondering how to do exactly this.
This is virgin territory to me so and looking forward learning more.
I am really hoping these Linux articles flourish.
Thanks for reading my mind!!!
Please keep publishing intermediate articles like this! I work as a junior sys admin and this type of stuff helps get me through the boring night shifts.
Write and folks will read Mike. I have an Odroid C2 I run nginx as reverse proxy for an express server with a Wit blog and a Gitea git repo inside my lan using ssl only. Never have put it on the net. I harden it enough for what I am doing with it behind my router. Production mode, helmet, and a few file configs on express for security. Fail2ban and a few configs for ssh. Part of html5-boilerplates server-configs on nginx, pieces of those are deprecated. I keep my config files, node apps, and write ups in the git repo so I can get access from a PC I may be working on to get at the files. Had the thought several times to open at least the git repo out on the net so I could get at it from somewhere besides home and just haven’t had the need as yet. Write away Mike I might learn something and I always have room for that.
Next in the series, Nginx setup on Windows
You can copy and paste most this article already
You can copy and paste all the commands in this article too if you enable SSH on your Debian or Ubuntu server and connect to it with Putty
Yes, tutorials are always nice. This one is very basic, but it’s a good start. As more and more developers (and normal users) are heading towards VPS’es or dedicated servers due to their flexibility – there is and will be demand for such topics as hardening, SSL configuration (e.g. using Let’s Encrypt), DNS, http server setups, load balancing and so on. Using command line tools is very good choice as this is natural way of working in Linux based hosting solutions.
+1 from me :)