Introduction & LAMP
Hosting websites on your very own Linux server is significantly simpler than you think. While it’s often cheaper to pay a host to do the hard work for you, if you’re anything like me, you’ll want to know exactly how they’re hosting it. Well, it’s much simpler than you likely think.
A VPS (Virtual Private Server) can be rented from as little as £5 a month, and the benefit of a VPS over single site hosting, is that it is completely under your control and you can host as many sites as you like (package permitting). So how do they do it?
You may have heard the term ‘Lamp Stack’ before and imagined a leaning skyscraper of piled up desk lamps ready to collapse at any time. Well, sufficed to say, that isn’t what it is. A LAMP Stack stands for ‘Linux, Apache, MySQL & PHP’ Stack. Essentially, it’s a barebones Linux server (I usually opt for Debian, but Ubuntu or literally anything else will suffice) with these four services installed. These are your foundations for hosting websites. If you’re reading this, you’re probably familiar with exactly what Apache, MySQL, PHP and Linux are.
If you’re expecting an epic guide on how to setup the LAMP stack and prepare your server for hosting websites, think again. Thanks to Tasksel, it’s an immensely simple process; two commands in fact.
I’m going to work on the assumption that you’ve hired a server or VPS from a host provider. I personally use Hetzner for my servers here in Europe, as I find them incredibly cost effective and reliable.
To do this, we’re going to be using SSH. Your host will provide you with the root admin password along with your server. To login, it’s something like the following from your terminal (if you’re a Linux or OS-X user) :
You will then be prompted for the password. Once you’re in, you can continue.
Setting up the LAMP Stack
sudo apt-get install tasksel
sudo tasksel install lamp-server
Aaaaaaand that’s it. No, really. The first of these commands will download and install Tasksel, while the latter will utilise the newly downloaded Tasksel to download and install all necessary packages for the LAMP Stack. It’ll prompt for required fields such as the MySQL password, etc.
Once this process is complete, your server is ready to host a website.
Pointing the Domain
It’s safe to assume that if you’re reading about how to host a website, that you own the domain name you want the website to be accessible from online.
Log into your domain provider. Find the option for configuring Domain names (usually marked DNS). Now look for the ADVANCED DNS options. I’ve used a number of domain name providers, and they usually look just like this:
You’ll see that there are two types of DNS in there by default. They are A & MX. MX records are for email, so not relevant to this how-to. What we’re interested in are the A records.
Now, when I was a complete amateur, finding out exactly how to use these A records was a pain. It’s actually enormously simple.
In the far left column, you’ll see two entries. Usually, you’ll need three to begin with, so we’ll start by modifying the existing two, then adding a third.
Put very simply, the @ symbol represents the domain root. Using this site as an example, that would be http://techvillain.com. In the destination/target field, you’d type in the IP address of your server.
The asterisk is a catch-all for subdomains. That means all subdomains will point to the server automatically. You can use anything at all and set them up through the virtualhost, which we’ll get to, shortly. An example of this would be http://random.techvillain.com. Again, in the destination/target field, you’d type in the IP address of your server.
Now we’ll add one. This shouldn’t be needed as your above catchall will take care of it. That being said, I’m told it’s good practice, so we’ll include it. Add a new DNS entry now.
In the first box (Entry), type www. Now, select A as the record type. I’m sure you can guess what this does. In case you’re struggling with the concept, it will make sure http://www.techvillain.com points to your server.
While this isn’t necessary in this guide, now is a great time to tell you that you can point a subdomain at an entirely different server. This is done by creating a new A record, and in the DNS ENTRY field, typing whatever you want the subdomain to be. Let’s say I wanted my subdomain to be bacon, I’d type bacon in this box, and in the destination/target field, the IP address of the server I wanted to point it to. Simple. Right, moving on.
Creating the website
Navigate to /var/www on your server, and create a new folder.Call it whatever your site is going to be called. For the purpose of this guide, we’ll use techvillain. For the remainder of this tutorial, just replace techvillain with whatever your site will be called. Do this like so :
Now that you have your folder setup, you can cd to it, and open it up in nano.
cd techvillain nano index.html
You’ll now be in a simple text editor, with a blank file. Type in the following:
<!DOCTYPE html> <html> <head></head> <body>It works - yey!</body> </html>
Now, CTRL+X on your keyboard, then type Y when it prompts, and press enter. Done. You are now hosting an unbelievably simple website. We’re not done yet though, as while the domain is pointing at the server, your server has no idea where to look for the files. For this, we’re going to use Apache’s virtualhosts.
Configuring your website on your server
When I first started hosting websites, I made most of my mistakes at this point. As such, I’m going to break it down simply and clearly for you!
What we’re going to be working with here, are virtualhosts. A virtualhost is how your server knows which domains to listen for, and where on your server to point it. Put simply, you’ve pointed the domain to this (sort of), and from here, it needs to reach the folder with the index.html file we just made.
So here’s how it’s done.
In here, you’ll have a default conf file. You’re going to want to copy this. Never overwrite the original default as it’s good to have a backup. You will find that when you add subsequent sites, you’ll simply copy one of your existing ones to make this process simpler. So copy it and open the new one in nano, like so:
cp 000-default.conf techvillain.com.conf nano techvillain.com.conf
Look for the start of virtualhost. It will look something like this :
Don’t let this confuse you, you’re mainly interested in the 2nd and 3rd lines; specifically, ServerAdmin and DirectoryRoot. We’re also going to add another couple.
Modify those two lines in the file so it looks like this:
ServerAdmin webmaster@localhost ServerName www.techvillain.com ServerAlias techvillain.com DocumentRoot /www/techvillain
This will ensure that www.techvillain.com points to the techvillain folder we created earlier. If you wanted to setup a subdomain, you’d change ServerName to something like:
When you’ve done this, CTRL+X to save, type Y to confirm, and hit enter. This will save the techvillain.com.conf file with the new settings.
Now, we need to enable the site in the apache configuration. This is done very simply, like so :
Now, we have one very last thing to do, and that’s to restart apache. This is done like so:
service apache2 restart
That is it. Congratulations! You just became a Linux sys-admin. Only kidding of course, but you’re well on your way!
Please let me know in the comments if I wasn’t clear on any part, or if there’s something else you’d like me to explain! Thanks!