The Easy Way to Use a VPS and Deploy Your Personal Projects.

Yoan Ante
9 min readApr 16, 2019

--

Heroku, Heroku, Where art thou Heroku? Deny thy friends and refuse thy suggestions; because if not, you’re going to spend a ton of money or have to deal with apps that take forever to load when they spin up.

So now that the we got my Shakespearian introduction out of the way you’re probably wondering Yoan, what the heck are you talking about?

Well in the first part of this article I spoke about why I don’t use managed service providers for my personal projects. The main reason is essentially because there is no sleeping on your VPS. Therefore, your apps don’t take 30 seconds to spin up if it they haven’t been used in a while, but there are many more reason and I encourage you to read that article if your curious about some of the other benefits of having an entire machine at your disposal.

Another wonderful thing about having your own virtual private server is that its just like having your own computer out on the internet ready for you to use and play with when and where ever you like. You can have an environment that is an almost a complete duplicate of your machine plus it allows you to install and test software on another machine if you don’t want to bloat your computer. It’s literally another computer that you can do whatever you want with, that you rent, for just a few bucks a month! I personally thinks that enough of an amazing reason.

Today we will go over the steps to setup your VPS and deploy Rails, Node, and React Apps. I will go over the overall steps that need to take place on your purchased VPS. At the end I will link to a Github gist with all the commands so you can star it and use it for yourself when ever you set up a new VPS. We will be setting up an Ubuntu machine running version 16.04 LTS. There is a new LTS (long term support) that you can use; the commands should be typical for version 18.04.

In part 3 of this article I will show you how to deploy multiple apps to the internet running on one machine. I will also show you how to get your custom domain name set up with your VPS and finally how to use a reverse proxy to get all your apps running under one domain but at different routes.

What we will do today:

  • Spin up new VPS
  • Create and manage a user and her permissions.
  • Setup your machine to keep you SSH sessions alive.
  • Log in from your local machine over SSH.
  • Install Git and and add ssh key to your Github account
  • Install all the development software to create your environment.
  • Get your apps onto the vps through Git
  • Start up your apps

We will do this using vpscheap.net because, well because they’re cheap! You can use any vps provider and the install and setup will be similar but if you want to follow along go with vpscheap. Let’s jump right into.

  • Spin up a new VPS

Go to vpscheap or your favorite vps provider and purchase a machine. You can choose the cheapest option if you are just trying it out but I recommend getting something with atleast 1gb of ram if you want to run multiple apps from your VPS.

If you choose a yearly option you can get a better deal than paying monthly.

Once you choose a package the website will direct you to page where you are asked to choose a distribution. Choose Ubuntu 16.04. It will take a minute or two for the server to come online. Once it does you will have a screen similar to the following.

This is your main informational dashboard for your server. You can do all your administrative tasks from this window. For now that is fine since its your first time on this server and you won’t be able to SSH in from your terminal on your machine. We will use the HTML 5 serial console to send it its first few commands. Click on HTML 5 Serial console and a terminal or console window will pop up in your browser.

  • Create and manage a user and her permissions.

The first commands you will send this virgin machine will:
• change your password
• update Ubuntu
• create a new user
• give new user root access

Here are the commands in the order you need to execute them. Ignore the `$` it’s just to show you that it is the prompt where you will be typing the commands. When you see something wrapped in <> you will not type the <> instead you will supply the argument to that command with your own information.

$ passwd
<prompt_for_new_password>
$ sudo apt-get update
$ adduser <your_user_name>
$ su - root
$ usermod -aG sudo <your_user_name>
$ su - <your_user_name>

Now to really be cool what you want to do is set up some basic ssh stuff so that you can ssh into the vps you created and not have the connection drop after a few minutes of inactivity.

  • Setup your machine to keep you SSH sessions alive.

SSH is an acronym for Secure Shell. It is a protocol that allows you get log into another machine and use their console or terminal. We will be logging into a bash session and run all will be running commands on this remote machine like you would on your own computer to setup your remote environment.

Still form HTML Serial Console run the following command.

$ sudo nano /etc/ssh/sshd_config 

This will open up the file sshd_config with the nano text editor. In this file we want to add two lines to keep your ssh sessions alive.

ClientAliveInterval 120
ClientAliveCountMax 720

To exit out of the nano editor hit ctrl and O and enter to save, then ctrl X to exit.

  • Log in from your local machine over SSH.

Now it’s time to log in from your terminal or console. On VPS cheap go to your dashboard and copy your ip address. This ip address will be used to login to your account from your personal computer.

To login to the remote computer from your terminal just type your ssh followed the username you created in the previous step followed by @ and your ip address.

$ ssh <your_user_name>@<your.ip.address>

It should prompt you for the password you created earlier. And know you should have a new prompt with something like

<your_username>@<your_remote_host>:~$

You are now in the home directory of your host computer logged in as your username.

  • Install Git and and add ssh key to your Github account

Now lets install git on your vps and generate an ssh key to add to Github so you can git push and pull from your repos.

$ sudo apt-get install git

Verify it installed correctly by checking that git — v returns a version

$ git --v

Now let’s generate a new key and copy it so that we can add it to Github. Note your email must go in quotes. Hit enter all the way through for all the prompts after this upcoming command. You do not need to add a password or change the default save directory. You don’t need to pass the -b option with 4096 or the -C option with you email but its recommended by Github.

$ ssh-keygen -b 4096 -C <”your-email@address.com”>

Once the key is generated it is saved to a directory in your home folder called .ssh. Let’s print out your public key thats stored in the file id_rsa.pub in that directory.

$ cat ~/.ssh/id_rsa.pub

that command will spit something out that looks like this. Copy and paste it.

ssh-rsa AAasdkfjaifaksdnfkaijweaskdfhiahetiahdfjasdkjf/G59RALfnvNOEaIiIsGI8RUQ84tWz0DvS4xNgXThFIkpWMNwgOJx/qeyOqhGv4WCdfHpjV2vlHP5dPkUHu52kurvMCZ2384uisd8uqwtio8djhihahguhadhMYIP8vOv8FJf4LVJVq1eXk7fJoviNA0q3a/dnl3SiA2YUGve+gAf2zcQ6FerRj6jmE1ll+z1/9Sj4OdhgOnJAd6clDMmFnabFdwAL5BwM2AhjcFWiGJ/jxNYf35boR2XcnBtalkdjf8l youremail@youremail.com

That is your public key that we will add to Github. Go to Github.com. On the top right click on your profile image and click on settings. From here you want to select the left menu option that says SSH and GPG keys. Click the top right button that says New SSH key. Copy and paste the key you generated to the text area and give the title something like My Amazing VPS.

If you want to verify that this worked correctly you can try to get shell access to Github by typing from your VPS’ console ssh -T git@github.com This should give you some type of message about it being the first time connected to that remote machine. Type yes and then you should get another message that says successfully authenticated but Github does not provide shell access.

Great Give yourself a pat on the back your done setting up Git and Github.

  • Install all the development software to create your environment.

This is awesome! Let’s finally install the rest of the software we need to get our machine ready for our apps.

We’re going to install the g++ compiler, fix a locale issue not being set, install Curl, install Ruby, install Postgresql, install NVM, install Node, and install Java in that order. We will be exiting our ssh sessions occasionally and then logging back in order for some settings to take effect.

$ sudo apt-get install build-essential
$ sudo locale-gen "en_US.UTF-8"
$ sudo apt-get install curl
$ curl -sSL https://get.rvm.io | bash
$ exit

Open a new session and continue with installing Ruby next. If you want to use a newer version feel free. 2.3.3 is pretty old but its what I’ve used on some projects so its the way I’m set up.

$ rvm install 2.3.3
$ rvm --default use 2.3.3
$ gem update --system
$ gem install bundler

Now install Postgresql. If you need other DB’s that doesn’t come with Ubuntu now is the time to install it. Postgres can be a nightmare so its why I chose it others are a bit easier others are even more annoying to install. The steps these following commands take are: Install Postgress. Switch into a Postgres user. Create another Postgres user with your username from your VPS. And finally exit out of the Postgres session.

$ sudo apt-get install postgresql postgresql-contrib
$ sudo -i -u postgres
$ createuser <your_user_name> --interactive

after the last command you will be prompted if you want to be a super user. Choose yes of-course.

$ exit

You should now be back at your regular vps terminal window and not the postgres session.

$ sudo -u postgres createdb yoan
$ sudo apt-get install postgresql-server-dev-9.5

Hopefully everything went smoothly and there were no errors. After this we move onto NVM. Give your self a big pat on the back, we’re almost done! Some of these commands might span two lines on your monitor.

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash$ echo ‘export NVM_DIR=”$HOME/.nvm”’ >> ~/.bash_profile$ echo ‘[ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh”’ >> ~/.bash_profile$ source ~/.bash_profile

Install Node and Java JDK.

$ nvm install node
$ nvm use node
$ nvm alias default node
$ sudo apt-get install default-jdk

Congratulations! We installed all the software needed to run Rails and Node apps. Including The MEAN, MERN and many other stacks. Lets get one of your apps live.

  • Get your apps onto the VPS through Git and start up your apps

Go on Github.com and go to one of your repos or better yet clone a simple rails app. You can clone them right into your home directory since you will probably only be using this directory for your apps anyway. However, if you like you can create new directories and clone into them instead.

from your home directory clone a simple rails app.

$ git clone git@blahblahdblah.something/yourusername/

Make sure cd into the directory, then erase the Gemfile.lock if it was in there. by running

$ rm -rf Gemfile.lock

run bundle install create your db and migrations and start your rails server as a daemon

$ bundle install
$ rails db:create
$ rails db:migrate
$ rails s -d&

By running -d command your app will run as a daemon the & symbol at the end will make it run in the background. Take note of the port it is running on. Now go to your vps’ ip address and follow it by the port number to check out your live app.

It should look something like this on your browser.

192:023.233.100:3000

and O Happy Day! We are finished with part two of setting up your own VPS to run multiple apps from one development server. In part three of this series I will go over how we can link this up with an actual domain name so that you don’t have to use that hideous IP address. We will also go over in detail how to run a reverse proxy so that you can host multiple apps and not have them finish with a port number. We will do this by running a reverse proxy that will let them live on a route on the domain we will add. So your apps will go from something like this.

192:023.233.100:3000
192:023.233.100:3001
192:023.233.100:5000
192:023.233.100:5001

to something like this.

my-awesome-domain.com/app1
my-awesome-domain.com/app2

I hope you’re enjoying this series. If you found this helpful or interesting don’t forget to throw my articles a clap or leave a comment below.

Here is a youtube playlist I made with these steps in slightly different order. I made some mistakes that are noted in the video description but its an extra reference if you want to get this up and running. Here is a link to a gist with all these commands plus some extra comments.

--

--