Entries in programming (4)

Wednesday
Oct202010

Setting up multiple Rails apps on OS X using Apache and Passenger

This is a post I thought could benefit people who develop on a Mac and want to use the OS Apache and Passenger to host their Rails projects. You can use passenger pref pane but for some reason this kept crashing my OS pref pane. I couldn’t figure out why this was happening so I removed it and set up everything manually (old school). Turns out it’s not that hard. In general I don’t create new rails apps too often so configuring by hand is not a big deal. Also, it’s likely I’ll forget how to do this by the time I need to add another Rails app so this post will hopefully be beneficial to my future self!

Step 1) Make sure you have installed Ruby, Rubygems, Passenger.

Also, make sure you have installed the Passenger apache module. Instructions are on the Phusion webpage. The instructions that are generated in the terminal are not entirely correct with regards to how you should configure Apache for multiple apps. Don’t let this throw you off, do everything else except the Rails app configuration stuff. That part is described below.

NOTE* Before we get to the configuration stuff; just a reminder to create copies of the files before you start editing them. Name them http-conf.old and hosts-old (for example). Just make sure you don’t have the old conf file end in “.conf” Apache will read any .conf file and load it.

Step 2) Configure Apache to handle named hosts and the one, or more apps your going to have it run.

To do this you’ll need to add a few configuration lines to your Apache httpd.conf file. You should find it under /etc/apache2/http.conf I always add the lines to the end of the file so it’s easy to locate. The file is quite long.

#This allows apache to have name based virtual hosts. can listen on the same Ip. by defualt it does not do this 
NameVirtualHost *:80

<VirtualHost *:80>
  ServerName registar.dev
  DocumentRoot "/Users/brian/Rails/registar/public"
  RailsEnv development
  RailsAllowModRewrite off
  <directory "/Users/brian/Rails/registar/public">
    Order allow,deny
    Allow from all
  </directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName enotis.dev
  DocumentRoot "/Users/brian/Rails/enotis/public"
  RailsEnv development
  RailsAllowModRewrite off
  <directory "/Users/brian/Rails/enotis/public">
    Order allow,deny
    Allow from all
  </directory>
</VirtualHost>

The first line tells Apache to accept named based virtual hosts. This will allow your apps to be accessible at http://myapp.dev instead of the usual http://localhost:3000. Much shorter, and more descriptive. You can see from the code above that I’ve got two apps I’m serving from here, one called Registar and the other called eNOTIS. The ServerName in the VirtualHost block configures how I refer to the app in the browser, the DocumentRoot sets where the apps public folder lives on my filesystem, RailsEnv sets the running environment, and I’m not too sure what the other lines do. I will have to do some more research and update the post. I’m guessing it’s got something to do with access permissions and such and where the request can originate from. Here’s the Apache docs on the config settings.

Step 3) Modify your host file

The hosts file is located in /etc/hosts on your OS X file system. This file is what is used by the OS to figure out where to find http://enotis.dev (in my case) on the network. There isn’t a DNS server out there telling the computer to that this is on my computer (nor should there be). The hosts file is a DNS shortcut to resolve the location (i.e. the IP) of named hosts. Here’s what mine looks like after editing it for the two apps I want to run.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1 localhost enotis.dev registar.dev
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0 localhost

You can keep adding apps to the end of the 127.0.0.1 line, just follow the formatting above. I think the other lines are used by BonJour and such. More can be read about this file on the Apple webpage or by typing ‘man hosts’ in the terminal

Step 4) Restart Apache

We made some pretty big changes so you’ll need to restart Apache before they’ll take effect. To do that from the terminal type

sudo /usr/sbin/apachectl restart

note: see the update below if this causes an error when you run it

Step 5) View your Rails apps

At this point you’ve restarted Apache, and told it where to find your Rails apps. If you go to http://.dev you should see your app. If not, see below.

HELP it doesn’t work!!!!

Well, this does happen from time-to-time. When I was figuring all this stuff out I ran into lots of problems. For a while Apache was serving up one app instead of two, Passenger started crashing, Apache started crashing and failing to restart, etc… etc… Use the logs on the system to help you figure out what is going on. Tail them as you make http requests in the browser to your apps URLs. There are two logs, accesslog and errorlog. Use them well.

tail -n 100 -f /var/log/apache2/error_log

Also take a look at any console message if you’ve hosed your config so bad it’s not doing anything at all. The app is called “Console”, spotlight search for it or look in your Apps/Utilities folder.

Friday
Apr302010

Problem installing git using homebrew

I finally got to the bottom of my git install issue using homebrew.

If you’re not familiar with homebrew it’s a package manager (sort of) for OS X but all the “packages” are built on your machine. It’s really a scripted installer written in Ruby. Here’s a link to a more thorough explanation: Homebrew post on EngineYard.com

Homebrew is great, and I’ve been using it for all of my new installs but I was having problems installing git. I kept getting this error using this command: sudo brew install -v git <= the ‘-v’ is for verbose output

In file included from /usr/local/include/curl/curl.h:36,
                 from http.h:6,
                 from remote-curl.c:5:
/usr/local/include/curl/curlrules.h:134: error: size of array ‘__curl_rule_01__’ is negative
make: *** [remote-curl.o] Error 1

Some backstory: I am on snow leopard but that is relatively recent. After making the transition a few weeks ago I had countless errors with the whole 32/64-bit architecture thing. What I realized in this instance was that since I had compiled curl from source when I was on leopard I needed to recompile. (I’ve had to recompile/reinstall just about everything after the upgrade (postgres, ruby, erlang, etc…)). Turns out, after downloading the latest curl, and compiling/installing it from source. I was able to install git using homebrew without a problem.

Note: I am not sure this is a git specific issue when installing with homebrew. It could be a general issue with homebrew and my system as homebrew uses curl/libcurl to get files and such. I suspect that this would have happened for any homebrew install but I have no evidence to back that up.

Tuesday
Dec082009

PGError: ERROR: permission denied: "RI_ConstraintTrigger_xxxxxxx" is a system trigger

This was a fun one.

Today I set out to get one of my apps setup on the CI server we have use at work when I ran into this problem. It occurred only for a few of the remaining specs in the test suite that still use fixtures. The error looked something like this:

ActiveRecord::StatementInvalid in 'RegistryController should show the current registry for the current user - no params sent'
PGError: ERROR:  permission denied: "RI_ConstraintTrigger_2681229" is a system trigger: ALTER TABLE "schema_migrations" ENABLE TRIGGER ALL;ALTER TABLE "users" ENABLE TRIGGER ALL;ALTER
......
ALL;ALTER TABLE "master_answers" ENABLE TRIGGER ALL;ALTER TABLE "questions" ENABLE TRIGGER ALL;ALTER TABLE "answers" ENABLE TRIGGER ALL;ALTER TABLE "people" ENABLE TRIGGER ALL

Turns out this is a permission issue with the how Postgres handles triggers. Triggers belong to the superuser and if you’ve set up your DB permissions properly your test user on your DB should NOT be a super user.

This author correctly identifies the problem but suggests the solution is to change the permissions on your test user:

Here is a better approach to addressing the actual problem (and thorough explanation):

I deviated from this approach slightly in that I only added the ‘require’ statement to pull in the hack in my CI environment (not for all the environments as the author suggests). I thoughts are that this change only needs to solve a problem in the CI env, so that is where it should live. This could bite me in the butt because it introduces an inconsistency in the adapter behavior across envs. To be honest, I’m not sure which is worse. We’ll see how it plays out.

Thursday
Mar262009

Jumping ship. Moving from Textmate to Vim for Rails development

Background

For the last two years or so I’ve been using Texmate for almost all of my Rails development. When I first started with Rails it was the recommended editor to use and much cheaper than the alternative (BBEdit). I purchased my copy and started hacking away. Even though Textmate met most of my needs I experimented with RadRails (now Aptana), Komodo, and Textpad (on Windows) and none of them had the simplicity, flexibility, or ease of use as Textmate. Recently I’ve noticed that there has been a fall off in Textmate development (version 1.5.8 was release sometime in February, prior to that version 1.5.7 was released in October of 2007!). Not that this is a huge problem, but with the textmate feature request as long as it is I would hope that the developer would be more on top of things.

Why the switch?

After much consideration, I decided to give Vim a try. Part of my reason for a switch in text editors was because I was looking for what would be the the “samurai sword” for software development (if there is such a thing). The other part of the reason why I started looking for other text editors was the ability to open two files at once. While this may not seem like a big deal to most people, I am tired of cmd-T’ing between my models and my spec files, my css and my views, my models and my migrations, etc… Perhaps it’s my development style or lack of short term memory but having to toggle between two files was beginning to wear on me and my productivity.

Why Vim?

Well, it helps that a developer I work with is a Vim guru. Also, (on his recommendation) I started using a Firefox plugin called Vimperator. This is a cool tool that is like a Vim gateway drug. It removes almost all interface elements in your browser and allows you to interact with web pages like they were files in Vim. It’s totally sweet. Being able to browse the web without your mouse is more awesome than you can imagine!

After using Vimperator for a while I started to become comfortable with how to use Vim. I had learned some of the commands to navigate. I was ready to make the plunge into Rails development in Vim.

How to do it

I’m not going to get into how to use Vim. There are far better sites out there for that purpose. Here are some of the add-ons for Vim I recommend to make the transition from Textmate to Vim easier.

  1. First, I use a Mac so MacVim is a must. You can get MacVim here.

  2. Once you’ve got that installed get the Rails script utility to make your Vim Rails aware. It will make it easy to switch between related files, run rake tasks, etc…

  3. To get the nice tree project gutter that Textmate has there is NERDTree. Using the same commands for navigating vim you can navigate your project files with ease.

One last tip… Vim (and other advanced text editors) have a lot of sharp edges. One which cut me was the caps lock key. Caps lock is a pretty useless key. I’m not sure how it ended up on modern computers. Perhaps so people can unleash the fury?! Anyway, there is this neat program called PCKeyboardHack that will allow you to remap your caps lock key to esc which is great for Vim because the esc key is vital to Vim usage.

The old (and busted)

The new (with teh hotness)

Hope this post helps with your Textmate to Vim conversion. It takes sometime to make the transition but it’s worth it. I’ll post more tips when I have them.