How to install Ruby on Rails with Ubuntu 11.04 Server
This tutorial will outline the steps needed to create an in-house production server for ruby on rails using:
- Secure Shell (SSH)
- MySQL
- Apache
- phpMyAdmin
- FTP
- GIT / CURL
- Ruby Version Manager (RVM)
- Rails
- Passenger with Apache Module
Let's get started. First you will need to have a clean install of Ubuntu 11.04 server installed. It doesn't matter if the operating system is running on a standalone system or a virtual environment. I will be using virtual box for this tutorial. When I installed Ubuntu I didn't select any server settings such as LAMP, SSH, etc… I like to be in control of what goes where and when it's installed.
SSH
The first thing we are going to do on our fresh Ubuntu install is install SSH so that we can login remotely from another computer. This is important for those of us who are using a production server and don't want to use remote desktop.
NOTE: When you log in write down the IP address for eth0. This is the IP you will use in your console to SSH into the server.
Go to the Ubuntu console and login. Then type this command to install SSH:
sudo apt-get install openssh-server
You will be asked to provide your password. Follow the steps on the screen.
After the SSH server is installed I will leave the server and go back to my computer. I am using Windows 7 so I will use putty. For those of you on a Mac you don't need any special software just use the console app that is already installed. Mac users will have to use:
SSH {ipaddress} –l {username}
For PC users we will use putty. Once putty is downloaded all you need to do is run the program. Once the program has launched there will be a field to provide the ip address of the server you want to connect to. Because we haven't modified the SSH configuration we will use the default port of 22.
More information on SSH can be found here.
MySQL
After we have installed SSH and connected to our server we will install MySQL. Ubuntu makes it extremely easy with this command:
sudo apt-get install mysql-server mysql-client
Again, you will be asked to provide your password. Follow the onscreen steps. This process can take a couple of minutes.
Apache 2
I favor Apache 2 but nginx (engine x) would work too. Ubuntu provides Apache 2 as a package just as it does for MySQL so to run the install the commands are very similar.
sudo apt-get install apache2
After the package is installed you can navigate to http://{ipaddress} and you should see a simple HTML page with a heading that reads, "It works!"
phpMyAdmin
The next package we will install is phpMyAdmin. This allows us to easily manage our MySQL database that we just installed. Run the following command in console:
sudo apt-get install phpmyadmin
You will be asked which server you want to reconfigure. Again, we are using Apache in this tutorial so hit the spacebar to select apache 2 and then tab to OK hit and the install should continue. Set the password to be the same password as the root user on the mysql database which you set earlier.
You can now navigate to http://{ipaddress}/phpmyadmin and you should see a login screen. Here you use the username "root" and whatever password you set. Ubuntu installs version 3.3.10 of phpMyAdmin. There is a newer version out there that I will go over how to install in a later post.
FTP
I am a fan of proftpd so I will show you how to install it on Ubuntu and configure file permissions so that any new files that are uploaded will be viewable by the outside world. First we need to create an ftp user and a password for the ftpuser.
sudo useradd [username] sudo passwd [username]
next we will add the user to the www-data user group
sudo usermod –g www-data [username]
then we will change mod the /var/www and apache2 virtual host files:
sudo chmod –R g+w /var/www
sudo chmod -R g+w /etc/apache2/sites-available
sudo chown {username}:www-data /var/www –R
sudo chown {username}:www-data /etc/apache2/sites-available -R
Now we are ready to install proftpd:
sudo apt-get install proftpd
When asked, select standalone option.
Let's edit the proftpd conf file to ensure security.
sudo nano /etc/proftpd/proftpd.conf
and edit it to match the following example:
# Use this to jail all users in their homes
DefaultRoot /var/wwwr
# Set the user and group that the server normally runs at.
User www-data
Group www-data
then hit "ctrl" + "x" and save changes. Then restart the ftpftpd process.
sudo /etc/init.d/proftpd restart
Time to test it out. Use your favorite FTP program and ftp into the ipaddress and port 21 with the username and password you have set. More info can be found here.
GIT /CURL
GIT is used to pull projects off of GitHub which will come in handy when we want to install ruby on rails modules. We will install CURL first.
sudo apt-get install curl
sudo apt-get install git
Ruby Version Manager
Ruby version manger or RVM is used to help standardize and maintain RoR applications. RVM is great because just as the name states it allows you to control the version of ruby that the application is using along with what version rails is using. To install RVM use:
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
We will then load the RVM into our shell:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
Then we will reload the shell configuration and test.
source .bash_profile
If configuration and installation was preformed correctly the command below should output 'rvm is a function'.
type rvm | head -1
We will then want to install the dependencies: (I got these from running 'rvm notes')
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
Next we will use the RVM to install ruby. We want to use the latest version and as of today the latest version is 1.9.2.
rvm install 1.9.2
Then we will tell rvm to use ruby 1.9.2
rvm use 1.9.2
next we will set it as the default (optional):
rvm use 1.9.2 --default
Finally we will update the ruby gems:
gem update –system
Rails
We will now install rails:
gem install rails
then run
rails –v
and make sure the command works.
First Rails Project
Now you can start your first rails application.
rails new /var/www/testapp
cd /var/www/testapp
bundle install
then start the rails server in development mode.
rails s
Navigate to http://{ipaddress}:3000 to see the rails app running.
Passenger
After we have completed all of the steps above we are ready to install phusion passenger. The first thing we need to do is install the gem into ruby.
gem install passenger
Because we are using Apache 2 we will install the apache2 module. There are options on the passenger site to install the module for nginx and the standalone versions as well.
passenger-install-apache2-module
This will take us through a set of instructions to complete the installation. We will need to install some additional packages. The installer will walk you through this process.
Next, we will need to add a config line to our apache2.conf file.
sudo vi /etc/apache2/apache2.conf
Then press “i” to insert text and paste.
LoadModule passenger_module /home/thomas/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7/ext/apache2/mod_passenger.so PassengerRoot /home/thomas/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7 PassengerRuby /home/thomas/.rvm/wrappers/ruby-1.9.2-p180/ruby
Then press "ctrl" + "c" and then “:x”
Next, we will edit the default file under /sites-available/.
sudo vi /etc/apache2/sites-available/default
Delete everything -- by pressing "shift" + “d” to delete the entire line and do this for every line.
Then press to insert text and paste in:
<VirtualHost *:80>
ServerName myserver.thomashastings.com
DocumentRoot /var/www/testapp/public
<Directory /var/www/testapp/public>
AllowOverride all
Options –MultiViews
</Directory>
</VirtualHost>
Then make the appropriate changes to point to the test app.
"ctrl" + "c" to exit the editor and then ":x" to save and exit changes.
Finally, we will restart apache.
sudo /etc/init.d/apache2 restart
Finished
You are finished. You can now navigate to http://{ipaddress} and see your testapp in production mode.
