In this post I will walk you through how to set up a web server on Amazon Web Services with Ubuntu. I will also explain some incredibly bare bones basic stuff about the interwebs, as I feel this knowledge is lacking in newsrooms today. It’s the first part in a series of walkthroughs on how I set up a recent project in Django using Ubuntu, Postgresql, nginx, uWSGI and Varnish.
This is geared more toward the journalist who wants to learn some basic development chops, so pardon me if I try to make this not super technical. And in no way is that because I only half know what I’m talking about. In no way.
So before I walk you through setting up a server, let me sort of explain how the Internet works.
HOW THE INTERNET WORKS
Say you want to visit a website. Perhaps it’s www.andymboyle.info. You type the address into your browser, and somehow, magically, a screen appears with text, an image of a beautiful man and awesome music plays in the background. This is quite similar to the process of you looking up a new bar to visit and then asking a bartender for a beer.
When looking up the location of a bar, sometimes people search a phone book (if this was the 1970s). You find the physical location of the bar, 1000 1st St., and then you showed up. You would walk in, sit down at the counter, and ask the bartender for a Yuengling, because you are a True Patriot.
On the Internet, when you visit a website, your browser is searching a digital phonebook to find the physical location of the website, which resides on a server somewhere. It finds the address of the server — or IP address (Internet Protocol address) — and heads there. It asks the server to do something — in most cases, please fetch the HTML of the page (please give me a beer, bartender) — and the server does it. Then your page is displayed.
You now know how the Internet works. You’re welcome.
SETTING UP AN AWS INSTANCE
Now go to aws.amazon.com and set up an account. For a quick walkthrough, you can read this. After you’ve done that, sign in. Click on the EC2 tab. Look under “Service Health.” It should list something like “Amazon EC2 (US East – N. Virginia).” You want to remember what it says in the first part of the parenthesis, as we’re going to need that later. Mine is US East, but yours could be different.
Click on “Launch Instance,” then the Community AMIs tab. We’re going to be installing Ubuntu 10.04 LTS, which is the operating system for the physical server. Similar to how your computer needs an operating system, so does your physical server.
You need to search for the right AMI to use. An AMI is a copy of a server that someone already setup. Ubuntu keeps a bunch up and going and shows you where to find them at this website. We will be using the 32-bit EBS version. So, for me I find US East 32-bit EBS’s number is ami-a2fe04cb (as of me writing this). Search for that under the Community AMIs tab. Once it finds it, click “Select.”
You want 1 instance, availability zone is no preference (feel free to change this stuff if you’re more knowledgeable than me), instance type is small, click continue. Then click continue again. On the next screen, click on the spot to the right of “Name” and give this server a name. Perhaps “Django Awesome Project.”
Create a key pair if you haven’t yet and then download it. It should be something like “blah.pem.” This is what you will use to login to your server later. On the next page, click default for security groups (unless you’ve already set stuff up yourself, feel free to use that). Then click launch. Give it a few minutes, but it should appear on your list of instances (on the left rail, click on “Instances).
Click on “Django Awesome Project” or whatever you named it. A window should appear below the list of instances, and scroll down. You will see something called “Public DNS” and then some stuff listed after it. That’s where your server lives — that’s the address of the bar. If it said, for instance, ec2-18.104.22.168.compute-1.amazonaws.com, that’s where your stuff lives. The 22.214.171.124 would be your IP address.
Now you need to log into your server. First I will explain how to do it with Windows and then with Apple OS X. If you’re on a Linux box, you can probably follow along the OS X one. Also, you probably don’t need to read this because you’re obviously a superior human being.
[Added March 12, 2012] You will most likely need to update your security settings on this instance before you can log into it. On the left, click on security groups. Odds are your group is set to “default” for your instance, so click on that one. At the bottom, a box will have two tags with “Details” and “Inbound.” Click “Inbound.” If it says nothing about a port 22, you need to add that. Do so by clicking the “Create a new rule” dropdown and move it to “SSH.” The default should work (0.0.0.0/0). Click “Add Rule” and then “Apply Rule Changes.”
LOGGING ONTO YOUR SERVER WITH A WINDOWS TERMINAL
If you’re on Windows, I’m sorry but that sucks. You need to download PuTTY, which is a terminal client for Windows. It works okay. Once you install that, open it.
Under host name, put in what you found after “Public DNS,” which would be similar to ec2-126.96.36.199.compute-1.amazonaws.com and make sure under protocol “SSH” is marked. But in front of that put ubuntu@, which is the username, so altogether is looks like email@example.com
On the left you should see a part that says SSH (you may need to click on Connection first). Then click on Kex. Find wherever it is you downloaded that key pair to and put that in there (it could have a .pem extension). Read this and convert your .pem file to a .ppk. Now, click Session again. Give this thing a name, perhaps “Awesome Django Project” under “Saved Session” and then click Save.
Now click load. A screen should pop up. It may ask you a question about “yes or no.” Say yes. And bam, you’re on the physical server. This is called a terminal window, and you are now awesome.
LOGGING ONTO YOUR SERVER WITH MAC OS X
Oh blessed be. You’re better than that Windows guy. Click the magnifying glass on the top right of your computer and type in “Terminal.” An app that looks like a black box should show up. Click on it. WELCOME TO THE MOST POWERFUL PART OF YOUR $2,000 LAPTOP. You’re welcome, again.
So. Remember that key pair you downloaded? You need to go to the directory it’s in. If it’s on your desktop, then you would need to type
If it’s in another folder, then cd to that folder name. I’m not going to teach you basic terminal commands. So. Deal with that on your own, please. AWS, as I said earlier, makes you assign a key to your instance, which you downloaded earlier. If you were given a .ppk file by a coworker like some of us instead of the .pem version, you’ll need to convert it into a .ssh file. You can read how to do that here if you have access to a Windows machine. I installed a program to do it on my Mac, but I can’t recall what it was called. It also borked some stuff up, so that sucked. And a word of advice: DON’T INSTALL MACPORTS.
If you have the .pem, you need to do this to the file to give it the right permissions:
sudo chmod 600 filename.pem
Once this is done, you’ll type this command (change .pem to .ssh if you have a .ssh file, of course. And change filename to whatever you named that file. And change the ec-240.10.10.10.compute-1.amazonaws.com bits to the one you found in your AWS terminal, like I told you to do earlier):
ssh -i filename.pem firstname.lastname@example.org
You should get a screen that looks like this:
And huzzah! Your server is up and running. In the next installment, we’ll walk through setting up Django, uWSGI and nginx and how to configure it all.