Setup an Ubuntu VPS the way I like it

In this post I want to document my way of installing a LAMP stack plus Ruby on Rails on my Ubuntu VPS.

VPS stands for Virtual Private Server, and it’s like having a real remote machine you can log into to configure it the way you like (and need) it, and serve your web pages to people around the world.

My installation includes:

* Ruby
* Rails
* Passenger with Apache module
* PhpMyAdmin

This procedure is tested both on Ubuntu 10.04 and Ubuntu 12.04.
Be advised that right now 1024 MB of RAM is the minimum recommended specs for a Passenger (with Apache module) installation.
I chose Apache/MySQL over the trendy and possibly superior Nginx/Postgresql, because by using the tried and tested Apache/MySQL solution, I could go ahead with a painless installation of the PhpMyAdmin tool, and both Apache and MySQL have a ton of documentation.

Let’s get this started!

Ah, wait, just a note for all n00bs out there. The “$” symbol means “your command prompt” in the terminal. So you don’t have to type it, it’s just there to signal that we’re in the terminal and have to type a command in there. Yes? Cool.

Upgrading the VPS

First of all, we should login though ssh into our VPS.

$ ssh root@your-ip-address

once we are in, let’s do some updating

$ apt-get update
$ apt-get upgrade

Finally, a nice reboot

$ reboot

Now, we’ve been kicked out. Let’s log back in, like this:

$ ssh root@your-ip-address

and let’s proceed by creating a new user which we’ll use from now on to log into our sweet server. We’ll be prompted to choose a password for our user.
Also, let’s add our user to the sudoers list (meaning our user can get superpowers whenever he wants). Then, we exit.

$ adduser alex
$ adduser alex sudo
$ exit

We’re now ready to log back in, this time with our own user, and let’s start installing some stuff which we’ll need during our installations.

$ ssh alex@your-ip-address
$ sudo apt-get install nano git-core curl libcurl4-openssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev

Let’s now install RVM.

RVM is a tool that will allow us to manage the installation of ruby.

$ curl -L | bash -s stable --auto

Then let’s load the rvm paths to .bashrc, type:

$ sudo nano .bashrc

and then add this line to the bottom of the file

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

You can save with ctrl-o and exit with ctrl-x.

To load RVM in our current session, type the following in terminal

source .bashrc

if any issue should arise, add “source .profile” to the files .bash_profile or .bashrc.

To check if RVM was properly installed, type:

$ type rvm | head -1

You should get the text:

rvm is a function

Now we can go on and install ruby and rails and so on.

Type in terminal:

$ rvm notes
$ rvm autolibs enable
$ rvm requirements

The last command will either install the required packages, or tell you what packages to install through sudo apt-get install.
In any case, after the installation of the needed libraries, let’s go ahead and install Ruby.

Ruby and(on) Rails installation

$ rvm install 1.9.3
$ rvm --default use 1.9.3

Then to check if ruby is doing fine:

$ ruby -v

Now, let’s install rails:

$ gem install rails

Passenger and Apache installation

Let’s install passenger, to serve our Rails apps on our server.

$ gem install passenger

Go with the default options and you should be fine.
To check if it’s properly installed:

$ passenger -v

Now, we can install Apache support for Passenger.

$ rvmsudo passenger-install-apache2-module

Once finished, you should get a message like this:

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /home/alex/.rvm/gems/ruby-1.9.3-p392/gems/passenger-3.0.19/ext/apache2/
   PassengerRoot /home/alex/.rvm/gems/ruby-1.9.3-p392/gems/passenger-3.0.19
   PassengerRuby /home/alex/.rvm/wrappers/ruby-1.9.3-p392/ruby

So let’s do like it says, and let’s add those lines to Apache configuration.

How to find where is the Apache configuration? Well I could tell you right away, but let’s find out.

run the command:

$ ps -ef | grep apache

which will return:

root 1419 1  0 Apr10 ? 00:00:00 /usr/sbin/apache2 -k start

The important part is /usr/sbin/apache2 (or whatever you might have in its place), so just run:

$ /usr/sbin/apache2 -V

It will tell us something like:

-D SERVER_CONFIG_FILE="/etc/apache2/apache2.conf"

So that’s where the config file is stored. Open it with:

$ sudo nano /etc/apache2/apache2.conf

and add the lines we mentioned above (actally, don’t copy them from here, as they might be slightly different in your case)

LoadModule passenger_module /home/alex/.rvm/gems/ruby-1.9.3-p392/gems/passenger-3.0.19/ext/apache2/
PassengerRoot /home/alex/.rvm/gems/ruby-1.9.3-p392/gems/passenger-3.0.19
PassengerRuby /home/alex/.rvm/wrappers/ruby-1.9.3-p392/ruby

Then, restart the server

$ sudo /etc/init.d/apache2 restart

At some point, Passenger will also tell us about how to change the Apache configuration to host our apps, a useful snippet of information actually, here it is for future reference.

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public    
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews

Let’s type in terminal:

sudo apt-get install mysql-server mysql-client
sudo apt-get install php5
sudo apt-get install phpmyadmin

MySql will require you to choose a master password, so choose one when prompted.
PhpMyAdmin will also prompt you to choose some options (choose the default) and a password (choose whatever you like), plus the MySql password you chose above… to make it easier, I choose them to be the same 🙂

To test that our php is working, create a file using nano editor, like so:

$ sudo nano /var/www/phpinfo.php

and in the file, write:

Now visit the page, typing something like this (example Ip address, replace with yours) in your browser address bar: It should work, hopefully. If it does, try your luck with  your-ip-address-here/phpmyadmin, again it should work. In case it does not and you get a “Page not found” kind of error, it’s because we have to tell Apache where to find PhpMyAdmin, so this should be fixed by the following command, which copies a configuration from PhpMyAdmin to Apache itself.

$ sudo cp /etc/phpmyadmin/apache.conf /etc/apache2/conf.d

now restart apèache, try again with your-ip-address-here/phpmyadmin, it should work!