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 shell
So 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 update
Type 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 -y
Once that completes, type
That will take a short moment to download. Once finished, type
tar -xvf rubygems-1.3.2.tgz
Now lets navigate to the new extracted folder.
Now lets setup RubyGems
sudo ruby ./setup.rb
Lets create a symbolic link, so the "gem" command works
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
Now 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 -y
Now 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-dev
And let's install the remaining libraries needed to make everything work.
sudo apt-get install ruby1.8-dev build-essential libapr1-dev -y
Now 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 rails
After that's finished, let's install the MySQL gem.
sudo gem install mysql
Now, 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 passenger
And now let's install Passenger to Apache.
Follow 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 restart
Now let's create a Rails app. I'll just call mine myapp.
rails myapp -d mysql
You see the output that your structure is created. Now let's cd to our app, in my case,
Now let's create a controller called hello, with an index action
script/generate controller hello index
Let's make sure that worked by itself by firing up Mongrel
And 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/myapp
And 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 myapp
Now before we reload Apache, let's make a change in our environment.rb
And 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 reload
Now 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)