You are on page 1of 7

Rails:Apache-Passenger-MySQL

for Ubuntu 10.04.1 Server i386


Install Prerequisites:
• sudo apt-get update && sudo apt-get upgrade -y
• (optional) Annex A: Set a static IP address in Ubuntu
• (optional) Annex B: Optional SSH config for new Ubuntu install
• sudo apt-get install build-essential ruby-full libmagickcore-dev imagemagick
libxml2-dev libxslt1-dev git-core apache2 apache2-prefork-dev libapr1-dev -y

Install MySQL:
• sudo apt-get install mysql-server libmysqlclient-dev

Install the latest RubyGems from source:

• wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
• tar -xzf rubygems-1.3.7.tgz
• sudo ruby rubygems-1.3.7/setup.rb
• rm -R rubygems*

Reliable way of making a symlink for rubygems 1.3.7:

• sudo apt-get install rubygems -y


• gem -v

Configure RubyGems to only install the gem and no documentation:

• nano ~/.gemrc
Add these two lines:
• install: --no-ri --no-rdoc
• update: --no-ri --no-rdoc

Install gems for your Rails server:


• sudo gem install rails -v=2.3.8
• sudo gem install will_paginate paperclip mysql passenger

Set the default environment to production globally:


• sudo nano /etc/environment
• And add the following line to it:
• export RAILS_ENV=‘production’

Install Phusion Passenger:


• sudo passenger-install-apache2-module
• (use default options, 1)

Configure Apache to load the Passenger module:

• Find the apache config file path: apache2ctl -V | grep SERVER_CONFIG_FILE


• sudo nano /etc/apache2/apache2.conf

At the bottom of Apache’s config file, tell it to load the Passenger module you just
installed by copy/pasting output from the Phusion Passenger installer that looked
something like this (varies depending on ruby install and passenger install):
------------------------------------------
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.15
/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /opt/local/bin/ruby
------------------------------------------

In addition, add the following to the apache.conf file:


------------------------------------------
# Speeds up spawn time tremendously -- if your app is compatible. RMagick seems to be incompatible
with smart spawning.
RailsSpawnMethod smart

# Just in case you're leaking memory, restart a listener


# after processing 5000 requests
PassengerMaxRequests 5000

# only check for restart.txt et al up to once every 5 seconds, instead of once per processed request
PassengerStatThrottleRate 5
# Keep the spawners alive, which speeds up spawning a new Application listener after a period of
inactivity at the expense of memory.
RailsAppSpawnerIdleTime 0

# Additionally keep a copy of the Rails framework in memory. If you're using multiple apps on the same
version of Rails, this will speed up the creation of new RailsAppSpawners. This isn't necessary if you're
only running one or 2 applications, or if your applications use different versions of Rails.
RailsFrameworkSpawnerIdleTime 0

# Keep the application instances alive longer. Default is 300 (seconds)


PassengerPoolIdleTime 1000
-----------------------------------------------------

Create your Apache virtual host:

• cd /etc/apache2
• ls (Take notice of the general layout of Apache’s folders.)
• cd sites-available
• sudo nano MyWebsiteName

Content Example:
------------------------------------------
<VirtualHost *:80>
ServerName cfrghq.forces.ca
DocumentRoot /home/ubuntu/rails-sites/software/current/public/
<Directory /home/ubuntu/rails-sites/software/current/public/ >
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
------------------------------------------

Restart Apache:

• sudo apache2ctl restart

Deploy the application:


On your development machine, we’ll assume you have the following:
git, Capistrano, ssh client, and a http://github.com/ account.

Git:

Before we get started with Git, lets make sure your development machine can access github. If
you haven’t created an account yet, what are you waiting for? :)
Once you have logged in to github, add your development machine’s public key to your account
settings. Github help section can guide you through this.

In the directory of your rails application, initialize an empty git repository on the local machine:

• git init

This creates a series of files for git under the hidden folder ‘.git’ within your application’s folder.
If you want to tell git to exclude any of your application’s files from being seen by git (for
example; large files, databases), you can add them now by creating a file hidden file ‘.gitignore’
in the root of your application’s directory.

Add the files in your application’s directory to Git’s list of files:

• git add .

Commit these files to your first entry with Git:

• git commit -m “my first commit’s message, rails is fun! :D”

Tell git where your repository is. In this case we’re using a github account:

• git remote add origin git@github.com:krisf/Software-Manager.git

Push all the files from your first commit to your git repository’s master branch:

• git push origin master

You now have a repository setup on Github!

Now you need to add your server’s public key to github so it can access the repository too:

• http://help.github.com/linux-key-setup/

Ensure your application has a database in MySQL to deploy to:

• mysql -u ‘root’ -p
> enter MySQL root password:
> create database ApplicationsDatabaseProd;
> exit

Capistrano:
In your application’s directory, get Capistrano to create the files it requires (Capfile,
config/deploy.rb). Capfile is just an initialization file for Capistrano. Your deploy.rb file contains
the Capistrano settings.

Edit deploy.rb and use the following as a template:

Test Webserver performance with ApacheBench (ab):

# ApacheBench: (concurrent requests: 50) (number of total requests : 2000) (url to your
website). Use the following command:

• ab -c 50 -n 2000 (url to your website)


Annex A: Setting a static IP Address in Ubuntu
• sudo nano /etc/network/interfaces

Content Example:
------------------------------------------
auto eth0
iface eth0 inet static
address 192.168.0.100
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
------------------------------------------
Annex B: Configuring SSH on a fresh Ubuntu install
Install SSH:

• sudo apt-get install openssh-server openssh-client -y

Create an SSH Alias for your server:

• sudo nano ~/.ssh/config

Content Example:
------------------------------------------
Host MyServersAliasName
Hostname 192.168.0.10
User ubuntu
------------------------------------------
Now you can use “ssh MyServersAliasName” to connect.

Configure SSH:

On the server:
• mkdir .ssh

On your development machine:


• cd ~/.ssh && ls

If a public rsa key file exists (id_dsa.pub), copy it to the server so you can login without entering
a password:
• scp ~/.ssh/id_dsa.pub MyServersAliasName:.ssh/authorized_keys

If you’re running on Amazon EC2, it’s like:


• scp -i cfrg.pem ~/.ssh/id_dsa.pub MyServersAliasName:.ssh/authorized_keys2

You might also like