How To Setup Ubuntu VPS to Host Ruby On Rails Applications?
If you've built a small Ruby on Rails application on the local development machine, the next big thing will be to make the application live. In this end, the first question to answer is - at which application will you host the application? If you don't have much money to spend, managed application hosting is not a solution for you. There are other relatively more affordable good solutions such as Heroku but it doesn't provide you with lots of storage space. In fact, Heroku also doesn't provide you with enough control. This is where the VPS comes handy.
An Ubuntu VPS can host many Ruby on Rails application. Let's start with the step-by-step process of setting up Ubuntu VPS for hosting Ruby on Rails applications.
How to Get Started?
First, you've to log in to the Virtual Private Server and add a new user in the server system. Then configure the SSH connections to make the VPS more secure. You now have to use RVM (Ruby Version Manager) script for installing Ruby and RubyGems. With the help of RubyGems, Rails and Capistrano will be installed. Once done, Passenger will then be installed, followed by taking care of Nginx Web server installation. PostgreSQL database will also be featured on the VPS.
How to make it Up and Running?
To get started, choose a suitable and reliable VPS hosting solution and install Ubuntu on the virtual server. For running more than one small Ruby on Rails applications, you can choose a 512 MB VPS. In absence of VPS, users can use open source virtualization software such as VirtualBox.
While setting up the VPS machine, network must be set to "Bridged". This will help the virtual machine to get IP address on the local network. Install Ubuntu Server's fresh copy, during installation choose "OpenSSH server". Login to the control panel of the VPS host. Submit safe "root" password and boot the virtual private server.
How to Login to VPS?
SSH (secure shell) will be used to log into the VPS, where it'll act as a network protocol. An encrypted connection will be used to enable computers for communication. Know your VPS's IP address. Default user here is the "root" and we assume that you are aware of the "root" password and the SSH is installed. IP address of the VPS will be used to log in.
On the local development machine of Ubuntu, open a terminal window. Now, login to your virtual private server. Follow the below mentioned command -
$ ssh default-user@vps-ip-address
Copy
In fact, you may be required to type "yes", followed by "root" password, and you'll be logged in.
How to Secure VPS?
Using "root" for working is not much desirable due to security reasons. To avoid the root, you've to create a new user, followed by giving it administrative rights. To create a new user, follow this -
$ adduser your-user-name
Copy
Invoke "sudo" command for making use of the administrative rights, which provides your system administration an additional security layer. Give a strong password and include the new user in the admin group. Here's the step -
$ adduser your-user-name group-name
Copy
By doing this, the administrative rights will be granted.
Now, you've to secure the SSH server configuration. You should remember that there's a default root user in a Unix system. You've to use SSH for login to disable the root. Check the following step for the same -
$ nano /etc/ssh/sshd_config
Copy
Now, set the PermitRootLogin to no, followed by reloading of the SSH configuration. This will enable the change. Connection of the current "root" user will be maintained inspite of the fact that from logging in the root will get disabled. Here's how to do it -
$ /etc/init.d/ssh reload
Copy
As the "root", logout of the virtual private server. Now, login as the new user. These can be done in the following way -
$ logout
$ ssh your-user-name@vps-ip-address
Copy
Your VPS system is now more secure.
How to Get Up To Date?
You can get updated VPS by -
- Updating package list
- Upgrading packages that are currently installed
- Installing new packages
- Delete the packages that are obsolete
How to Prepare VPS For RVM?
VPS rebooting will take a minute time. Once it's done, use a terminal window for logging back into the VPS.
You'll need Curl and Git package for installing RVM script. While Curl toll helps in transferring data by using protocol ranges such as FTP and HTTP. Git, on the other hand, is a distributed version control system that is capable of handling all kinds of projects with efficiency as well as speed. Most developers of Ruby on Rails, prefer Git for version control.
How to Install RVM?
RVM (Ruby Version Manager) is a command line tool for installing, managing, and working with more than one Ruby environment. You can use this command to install RVM in the current logged in user's directory -
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Copy
Now, edit bash profile of the user for navigating to the home directory, thereby ensuring that every time the corresponding user logs in, the Ruby Version Manager script gets uploaded.
You'll get "rvm is a function" if the set-up is done correctly. In case, the setup is not correct, you've to visit RVM site and look for "Troubleshooting Your Install" for making it work.
How to Prepare VPS For Ruby And RubyGems?
A handy tool is there with Ruby Version Manager to check the dependencies for compiling and installing Ruby and RubyGems from source. This tool is-
$ rvm notes
Copy
How to Use RVM for installing Ruby And RubyGems?
You can use the following command to check which Ruby versions are available -
$ rvm list known
Copy
A regular version such as 1.9.2 can be installed, which may take some time for installation. Now, use this installed Ruby as the default version. Now, check whether Ruby and RubyGems versions were installed properly by using these commands -
$ ruby -v
$ gem -v
Copy
You can also update RubyGems and the Gems manually by following these commands -
$ gem update --system
$ gem update
Copy
How to Use RubyGems for Installing Rails?
Installing Rails with the help of RubyGems is very easy. gem command can be used for installing Rails with the use of RubyGems. For checking whether the installation is done properly, use this command -
$ gem install rails
$ rails -v
Copy
How to Use RubyGems for Installing Passenger?
RubyGems can be used for installing Passenger by following these commands -
$ gem install passenger
$ passenger -v
Copy
Now, check whether the installation has been done correctly.
How to Install Nginx and also Configure Passenger Module?
First of all, installation of dependency is required for letting Passenger to install Nginx. The command is -
$ sudo apt-get install libcurl4-openssl-dev
Copy
You can use this command to start installation of Nginx as well as configuration of the Passenger module -
$ rvmsudo passenger-install-nginx-module
Copy
The dependencies will be checked first and then it'll ask whether you'll -
Option 1 - Download Nginx source code with the help of the program
Option 2 - Download Nginx source code yourself
Choose option 1.
Now, you can yourself start Nginx by using command -
$ sudo /opt/nginx/sbin/nginx
Copy
However, a startup script can also be used for starting Nginx. Wget command line program can be used for downloading files from the internet. The following command can be followed -
$ cd
$ wget https://www.smashingmagazine.com/files/nginx
$ sudo mv nginx /etc/init.d/nginx
$ sudo chmod +x /etc/init.d/nginx
$ sudo /usr/sbin/update-rc.d -f nginx defaults
Copy
To start, stop, reload and restart Nginx, the below mentioned commands can be used -
$ sudo /etc/init.d/nginx start
$ sudo /etc/init.d/nginx stop
$ sudo /etc/init.d/nginx reload
$ sudo /etc/init.d/nginx restart
Copy
How to Install PostgreSQL?
Installation of PostgreSQL can be done along with the dependency. Later on, for installing "pg" Gem dependency will be required. This will connect PostgreSQL with Ruby on Rails. Here's the command to follow -
$ sudo apt-get install postgresql libpq-dev
Copy
Configure PostgreSQL
Once installation of the packages are done, secure psql to configure PostgreSQL. Use the following command -
$ sudo -u postgres psql
# password postgres
# q
Copy
Change ident to md5 for changing authentication configuration. Reload the authentication configuration into PostgreSQL. Here's the command -
$ sudo nano /etc/postgresql/8.4/main/pg_hba.conf
$ sudo /etc/init.d/postgresql reload
Copy
Reload the PostgreSQL configuration to make the changes to take effect. Here's the command to follow -
$ sudo nano /etc/postgresql/8.4/main/postgresql.conf
Copy
After this step, the virtual private server is installed now. For logging out, simply use this command -
$ logout
Copy
Conclusion
Now, to check whether the VPS is working properly or now, you need to test the server. You can develop a small application and then make the deployment to the virtual private server by using Capistrano. This is how you setup Ubuntu VPS to host Ruby On Rails Applications.
Was this answer helpful?
10
10