Installing Ruby on Rails + MySQL + Imagemagick + Nginx + Passenger With Macports OSX

Installing Ruby on Rails + MySQL + Imagemagick + Nginx + Passenger With Macports OSX

This was one epic battle for me when i tried to get rails going with Lighttpd. But with that said, i have been spending quite sometime on just getting my development environment up and running on an environment similar to how i would deploy the app. So this is how i managed to get Rails up and running with Nginx.

First you need to download and install macports. You can get it from: http://www.macports.org/. Install that, it may take some time, but it’s well worth it!

Next we need to get some updating going on, so that your sources are up to date and fresh to get some ports installing. Open up terminal and type:

sudo port -v selfupdate

The -v is for verbose output, so you can see exactly what is going on. After that is complete lets going with installing ruby and some rails.

First we need to install ruby. To do this type:

sudo port install ruby

This will go ahead and install ruby. Next up run:

sudo port install rb-rubygems

This will allow us to access the awesome gems feature that allows you to import third-party gems, or plugins. Once that has been installed you will want to run an update, to get the latest gems sources. This can now be done through the gem command.

sudo gem update --system

To add support for Imagemagick run these commands:

sudo port install Imagemagick
sudo gem install rmagick

Pretty simple hey? Next up we will want to add some support for mysql5. To do this we will go back to our port system and run:

sudo port install mysql5 +server

You will then be shown some hints as to how to set up the test tables and run the secure installation. I would recommend doing this as it allows you to setup your root password and clear test tables. Just follow the onscreen prompts on how to install it. Some people have permission issues with sockets here, if you are one of them check out this page. Once that is done run:

sudo port install rb-mysql

This will install the ruby mysql adapter. Wait for that to go through next we can add rails! Yep we’re up to that stage already!

It’s pretty simple:

sudo gem install rails

Rails is installed! You can start a new rails app now simply by navigating to the folder where you would like to create the app and typing:

rails app_name

and to make sure that you have mysql support created for you instead of manually type:

rails -d mysql app_name

To test that your app ran smoothly you can simply change directory into your new app and run:

ruby script/server

and this will load up the built in Webrick server, of which you can now navigate to in your browser via http://localhost:3000

If all went successfully then you should see a screen like:
Screen shot 2009-11-08 at 12.13.10 PM

We don’t really want to use this built in server, we want to use Nginx with passenger! And to do that we don’t want to build from source, we want to use the Macports elegance, well i did anyway. Unfortunately macports doesn’t support this off the bat. But with some tweaking its very easy to do. First we will need to grab passenger. This is once again very easy to achieve, thanks to gems:

sudo gem install passenger

Now run:

passenger-config --root

this will tell you where passenger installed and where it’s hiding out. We will need this for telling Nginx where to grab it from. So please take now, paste it to a text file or something.

Next step up is to get Nginx installed. As i said before this will require some hackering. So lets do that first. What we need to do is add the passenger variant which currently isn’t available with the nginx macport. So to do that navigate to the nginx portfile directory, which can be done via:

cd /opt/local/var/macports/sources/rsync.macports.org/release/ports/www/nginx

Deep hey. Lets add the variant. Open it up via:

sudo vi Portfile

press <strong>SHIFT + G</strong> this will take you to the end of the file, where you should now insert (via pressing the key i) the following:

variant passenger description {Adds passenger support} {
    configure.args-append   --add-module=/opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/nginx
}

Please take note of the “/opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/nginx”. This is too important! You need to change that with your path to passenger, but with /etc/nginx added to the end. So if you get any compiling errors, it’s probably because you didn’t have enough coffee at this step of the process. Once you’ve done this hit ESC to get out of insert mode and press <strong>SHIFT + ZZ</strong> this will save and close the file.

Next its time to install nginx via the macports system. We will also want to add our variation in. So to install nginx with the passenger module type:

sudo port install nginx +gzip_static +passenger +ssl

Let it do its magic. Once done cd to <strong>/opt/local/etc/nginx/</strong>. We need to copy the example config files to ones we want to use. So do that.

sudo cp mime.types.example mime.types
sudo cp nginx.conf.example nginx.conf

So all that is left is to add our passenger config and a site. Open up the nginx.conf file via <strong>sudo vi nginx.conf</strong>

My config file looks like:

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /opt/local/www/htdocs/;
            index  index.html index.htm;
        }

    }

		passenger_root /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5;
		passenger_ruby /opt/local/bin/ruby;
		passenger_max_pool_size 8;
		passenger_max_instances_per_app 1;
		passenger_pool_idle_time 3600;

		server {
			listen 80;
			client_max_body_size 250M;
			server_name dev.app;
			root /opt/local/www/my_app_name/public;
			passenger_enabled on;
			rails_env development;
		}
}

What you need to pay attention to here is ther location / for root. I.e where it says /opt/local/www/htdocs/ Make sure that exists. Next thing to look out for is the passenger_root /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5; This is where you get your passenger path from <strong>passenger-config --root</strong> The next thing is passenger_ruby. This setting sets the path to ruby, and also where ruby will grab its gems with. I ran into an error first with passenger, stating that my gems were too old, and that i should update. But when i went into terminal and did gem update –system it said i had the latest. So it became apparent that passenger was by default using Mac OSX’s ruby version! So you set the path to the ruby you want, in this case, its the one macports installed.

Anyhow. You might be asking well how do i start, stop and restart MySql or Nginx. Well I’ve created some aliases you should add to your .profile file. To do this run:

vi ~/.profile

then add in:

alias nginx-start="sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist"
alias nginx-stop="sudo launchctl unload /Library/LaunchDaemons/org.macports.nginx.plist"
alias nginx-restart="sudo launchctl unload /Library/LaunchDaemons/org.macports.nginx.plist &amp;&amp; sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist"
alias mysqlstart="sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist"
alias mysqlstop="sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql5.plist"

Save the file, then close down terminal and reload it. You should now be able to run nginx-restart, or start, or stop, same with mysql except for restart. Handy?

You should now be able to navigate to http://localhost and see a success screen, or whatever you have in your htdocs folder. Or if you have set up your hosts file in /etc/hosts to route 127.0.0.1 dev.app then it will show your rails app!

If you don’t you have a problem. tail /opt/local/var/log/nginx/error.log should be able to tell you whats up. Any problems or issues you can’t work out in the first hour by yourself add a comment![/code]

Posted by voidet

Categorised under Software
Bookmark the permalink or leave a trackback.

One Comment

  1. Quality Stuff Right Here. 🙂

    November 9, 2009 @ 12:37 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

or