Skip to content

Instantly share code, notes, and snippets.

@whatsim
Last active August 29, 2015 14:09
Show Gist options
  • Save whatsim/4e527c3f49525300f386 to your computer and use it in GitHub Desktop.
Save whatsim/4e527c3f49525300f386 to your computer and use it in GitHub Desktop.
Server setup

1. Spin up a server.

There's a lot of ways to do this, you can run your own box (I do this with a Beaglebone sitting on my desk), or pay any number of internet people to run it for you (I do this too). Big names in Virtual Private Servers (VPS)

  • Amazon EC2 (Yeah I have one of these.)
  • Linode (Yeah I have one of these too.)
  • Digital Ocean (Uh, yep this one too.)

We're using Digital Ocean, the steps to spin up a server will differ with approach but interacting with it once its up is pretty similar.

There's a second question after we choose who our host is, and it's operating system. This is a huge topic I'm punting on. We're gonna run Ubuntu. Its a Linux flavor for Servers and Desktops, its pretty mainstream so most software has builds that work on it, and there's a lot of available help for it via Google. Good enough.

2. The First Login.

So we have to get access to this server in the sky, fortunately we have all we need to do that. Digital Ocean provided us with a root account / password and we know the servers IP, so lets login.

Fire up Terminal on your Mac (⌘+Space to open spotlight, search for Terminal, hit enter).

A word about Terminal, while we're here. Terminal is a 'shell', just an alternate way to navigate the file system and run programs. By default you have access to the commands the shell provides (like 'cd' to change directory), as well as any program that sits in your PATH. The PATH is just a list of directories you want the shell to look for executables in. If you want to see where those are just type.

In Terminal

> echo $PATH

As an aside, the '>' is just shorthand for 'in the shell type this'. Don't type the >.

There's a million little tips and tricks to using a shell which are largely out of scope. But its worth noting a couple before we go on. TAB autocompletes directory or file names if you've typed enough for it to be unambiguous. The up and down arrow go through commands you've run previously. In addition here's a few handy commands.

ls - list files in directory
cd a_directory/another_directory - change directory to a_directory/another_directory
cd ~ - change directory to your home directory
cd / - change directory to the root directory
cd - - change directory to the last directory you were in
touch AFILENAME - make a file named AFILENAME
nano AFILENAME - a text editor opening a file called AFILENAME
mv AFILENAME ANOTHERFILENAME - move AFILENAME to ANOTHERFILENAME
man mv - look up the manual for a command in this case 'mv'
rm AFILENAME - remove AFILENAME from the system. There's no Trashcan, it really just gets rid of it.

Okay apologize for the digression, lets move along. To login to our server we want to use a program called SSH, your Mac already comes with it out of the box. If you typed man ssh you'd get a pretty serious manpage about all it can do. We're gonna ignore that for right now since we know what we need to do. Try this:

In Terminal

> ssh root@THE_IP_ADDRESS_DIGITAL_OCEAN_GAVE_YOU

This is pretty easy to parse, we're running the ssh application to login as root on the computer at THE_IP_ADDRESS_DIGITAL_OCEAN_GAVE_YOU. If that completes successfully we're going to get a prompt confirm the fingerprint of the server (this happens the first time we connect to any new computer as a security measure, if that finger print changes for the same IP later ssh will panic and not connect to avoid you getting your credentials stolen). After we say yes to the fingerprint we should get a prompt for our password. Go ahead and enter that.

And if all goes well we're logged in. We get a whole bunch of information about our cloud server spammed at us. We can ignore that for now, though it has some interesting tidbits to look at later. So on first login there's a couple of things we should do.

Change root password.

It'll prompt you to change the root password to something we know. You can do this again later for the account you're logged in as if you want with the 'passwd' command. It'll ask you for the old password, and then a new password twice, easy peasy.

On Linux like systems (this includes your Mac), you normally run as a lower privilege user. This is a safety so you don't do something really bad and destroy your system by mistake. Right now on our cloud server we are running as root though, which means we could do something bad and destroy our system by mistake. Lets make a new user thats not root for us to use safely. For a more indepth view of this Digital Ocean has a great document.

In Terminal

> adduser user_name

It'll prompt for the info it needs and make the user for us. There's one more thing to do though. Its all well and good to have a user who doesn't have the ability to destroy the system, but sometimes we need root level access to install software or whatever else. In Linux land there's a special command for this called sudo, you would use it like:

In Terminal

> sudo mv ~/aProgram /bin/aProgram

That would copy aProgram into the system bin directory (thats probably a part of your PATH). Normally as a user we don't have access to put stuff there. So sudo is handy as a way to say 'yeah I'm serious run this command'. But we need to let the system know that we want the new user we made to have permission to use sudo. So run this:

In Terminal

> visudo

This opens an editor on the sudo permission file. We want to find a line that looks like:

root    ALL=(ALL:ALL) ALL

And edit it to look like this:

root    ALL=(ALL:ALL) ALL
newusername ALL=(ALL:ALL) ALL

Exit by hitting cntl+x, and choose 'y' to save.

Now lets get out of scary admin mode. Go ahead and type 'exit', that will log you out of SSH and your terminal is just your laptop again.

3. Every Other Login.

Lets login again with the user we just made.

In Terminal

> ssh newusername@THE_IP_ADDRESS_DIGITAL_OCEAN_GAVE_YOU
and enter your password

And here we are, logged in as your new user on the same machine. Great. Now its time to start installing stuff. Ubuntu (indeed all Debian like Linux flavors) comes with a package manager called 'apt-get' that can get us started. First lets install 'nginx' a webserver.

In Terminal

> sudo apt-get install nginx

So we're running apt-get with the install option, telling it we want nginx, and we're running that with the elevated permissions provided by sudo. Got it? Good. You'll have to approve the listed changes for it to install. Notice too it installs all the things nginx relies on, cool right?

Now we want to install git probably, and nodejs, easy enough. (You can skip this if your just going to serve flat html/js.

In Terminal

> sudo apt-get install git
later
> sudo apt-get install nodejs

Same deal, alright rad. Now one sticky wicket with the nodejs install. On Ubuntu there's another thing in the repo called 'node', but most node environments expect node to be called...well, node. So lets fix that. We'll make a link called node to nodejs. Run this first.

In Terminal

> which nodejs

Which prints the path to nodejs, we'll need that to make a link.

In Terminal

> sudo ln -s RESULT_OF_WHICH /usr/local/bin/node

Cool, easy enough. Now lets try to run just node.

In Terminal

> node

We should get the node prompt. Got it? Good. Moving on. Go ahead and close it with Cntl+C. So Nginx by default is serving its 'I'm okay' page, if you go to the IP address of your server you'll see it. There's a couple of options for how you can set up nginx, you can have it serve a directory, or you can have it proxy to another server behind it, node, python, or what have you. We'll do the flat file one first.

When you installed nginx apt-get put its configuration files in /etc/nginx. You're probably noticing that these directory names are following a pattern. More on those. etc is normally a place where configuration files live. /etc/nginx then is configuration files for nginx. Cool easy.

So lets go there, but before we leave the user directory lets make a folder to be our web root in our user directory using 'mkdir'.

In Terminal

> mkdir ~/www
> cd /etc/nginx
> ls

That should take us to the directory, and print the files in there for us. Cool. There should be a directory called 'sites-available'. So this is pretty clever, the way nginx works is you put the configuration for a specific site being served in a file in that sites-available. In order to turn that on we then link it into the other directory you might have noticed 'sites-enabled'. Remember linking? We did it with node earlier. Lets go into 'sites-available'.

In Terminal

> cd sites-available

You can take a peek at the default if you like with less, a text preview app.

In Terminal

> less default

Less uses vim style controls, unlike nano (sorry!) To exit type ':q'. Alright now lets make a configuration!

In Terminal

> sudo nano my_site

So now we have a blank config ready to go. Whatever will we put in here. Well. You could do a ton of stuff. Lets start with serving a static folder. More here.

server {
	root /home/newusername/www;

	location / {
	}

}

Thats it. Now we need to link that into sites-enabled. Link it with.

In Terminal

> sudo ln -s my_site /etc/nginx/sites-enabled/

And switch to sites-enabled to remove the default.

In Terminal

> cd ..
> cd sites-enabled
> ls
see the default?
> sudo rm default

Now we need to restart nginx.

In Terminal

> sudo /etc/init.d/nginx restart

If everything worked it should be serving out of your www folder now. Try putting stuff in there.

Its important to note you can connect with the same credentials via SSH in most FTP applications, like Cyberduck, if you're looking to download things to the server. You can do it through command line too, but for large file operations its nice to use a FTP app.

Installment Two covers setting up a database and node app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment