Topic: Installing Rails, Apache, Passenger and MySQL Client on Ubuntu
In this tutorial, I am going to cover installing a Ubuntu web server utilizing Apache2, Passenger, and using MySQL as the RDBMS.
As with other tutorials, anything in code blocks, type in the command.
Type these things in the shellSo let's get started.
I am starting out with Ubuntu Server 8.04 LTS (Hardy Heron). With it's Ubuntu Server or "Desktop", it doesn't matter. If you're using Desktop, just use the Terminal for the entire process.
First thing you want to do to make sure APT is current, type
sudo apt-get updateType in your password. Once you are returned to the cursor prompt, we will install Ruby, RubyGems, ri, rdoc, and irb.
sudo apt-get install ruby ri rdoc irb -yOnce that completes, type
wget [url]http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz[/url]That will take a short moment to download. Once finished, type
tar -xvf rubygems-1.3.2.tgzNow lets navigate to the new extracted folder.
cd rubygems-1.3.2Now lets setup RubyGems
sudo ruby ./setup.rbLets create a symbolic link, so the "gem" command works
sudo ln -s /usr/bin/gem1.8 /usr/bin/gemNow we want to install Apache2, the MySQL client, and all necessary Apache MPM modules. Type
sudo apt-get install apache2.2-common apache2-mpm-prefork apache2-prefork-dev -yNow let's install MySQL and all the necessary libraries and modules for MySQL connectivity to happen.
sudo apt-get install mysql-client libmysql-ruby libmysqlclient15-devAnd let's install the remaining libraries needed to make everything work.
sudo apt-get install ruby1.8-dev build-essential libapr1-dev -yNow let's start to install some gems, starting with Rails. As of this tutorial, the most current version is 2.3.2.
sudo gem install railsAfter that's finished, let's install the MySQL gem.
sudo gem install mysqlNow, I am fairly positive I didn't miss any libraries for the MySQL gem to work, so if you are getting the "failed to build native extensions" error, let me know.
Now lets install Passenger Phusion
sudo gem install passengerAnd now let's install Passenger to Apache.
sudo passenger-install-apache2-moduleFollow the text based wizard. All you should have to do is hit Enter, but if we missed anything, it will tell you what, and give you the commands.
Once the prerequisites are installed and Passenger is installed properly, it gives you 3 lines to add to your /etc/apache2/apache2.conf. I usually add these lines at the bottom of the file, but some people have a preference where they want the lines. Just copy the 3 lines from the shell and open nano or vi, and paste those lines.
I'd put them here, but as they upgrade Passenger over the years, the versions and formatting of the lines change, so this could just confuse you if they switch it up again.
Make sure to save the apache2.conf file, and then restart apache.
sudo /etc/init.d/apache2 restartNow let's create a Rails app. I'll just call mine myapp.
rails myapp -d mysqlYou see the output that your structure is created. Now let's cd to our app, in my case,
cd myappNow let's create a controller called hello, with an index action
script/generate controller hello indexLet's make sure that worked by itself by firing up Mongrel
script/serverAnd let's open a browser window and point to http://<IP>:3000/hello
Where <IP> is the IP Address of that server/box.
You should see something like "#Hello..". If so, that's great. Hit Ctrl+C to kill Mongrel.
Now, let's create a VirtualHost for Apache.
sudo nano /etc/apache2/sites-available/myappAnd once in there, type
<VirtualHost :*80>
ServerName myapp
DocumentRoot /home/<username>/myapp/public
</VirtualHost>Where <username> is your linux username.
Now, let's disable the default site, and enable the myapp site.
sudo a2dissite default
sudo a2ensite myappNow before we reload Apache, let's make a change in our environment.rb
nano config/environment.rbAnd let's put Rails in production mode for this app, so it can work with Passenger.
Add the line
ENV['RAILS_ENV'] ||= 'production'If you see the line is already there, just uncomment it by removing the # in front of it.
Alternatively, you can set passenger in development mode.
Now, let's reload apache.
sudo /etc/init.d/apache2 reloadNow lets see if it worked. Browse to http://<IP>/hello. You should see the same thing as you did with Mongrel, but now it's being served by Apache.
And that's it! Just delete the myapp (or whatever you called it) if you want, and create more rails apps, and add VirtualHosts for each site you want Apache to serve. If you do have more than one site, make sure to use NameVirtualHost *:80 above the <VirtualHost *:80> line on the first site, but leave the others like above.
Hope this help anyone looking to setup their Ubuntu box as a Rails Apache Web server using external MySQL databases.
Last edited by jmbrink26 (2010-01-31 18:55:40)