OSX apache2 final-stage setup hints?

Dan Kelley wrote:

Preamble: I hope people on this list will tolerate a question that, while *motivated* by RoR, is not strictly *about* RoR. I'm trying to let colleagues see a RoR site I'm developing, so I need to share the pages. I'm an academic, and doing this experimentally, so commercial hosting is not a possibility. I'd like to host the page on my desktop machine, as I've been doing for years with (cough) PHP pages. It seems that mongrel won't let me share outside the box, and all the advice suggests that I should be using apache2. I've followed along with quite a few tutorials for installation and configuration, and things are working fine locally. The problem is with sharing.

You can use mongrel as your "outwards" facing Web server -- just don't give it an "-a" parameter.

E.g. this restricts mongrel to "localhost" port 8000 only:

mongrel_rails start -d -p 8000 -a 127.0.0.1 -e production

This exposes mongrel to the outside world:

mongrel_rails start -d -p 8000 -e production

Dan Kelley wrote:

Thanks. Is there a trick to the URL, afterwards? For example, I do

mongrel_rails start -d -p 8000 -e production

but when I point to

http://(host):8000/(page)

it doesn't respond, *except* if I'm accessing via the host machine.

Start Mongrel and do something like:

netstat --tcp --all --numeric | less

and look for the local address that's bound to port 8000.

Is it 0.0.0.0:8000 or some internal-only facing IP address?

Dan Kelley wrote:

The netstat seems to take different arguments to those suggested. Anyway, if I do

    netstat -a -n

I see the line

    tcp4 0 0 *.8000 *.* LISTEN

and I wonder if that tells me something? (I'm sorry to be such a pain, but you see, you've got me all excited, thinking that I may get this working today, after a long time of trying. Hence, I hope you'll excuse my ignorance.)

Alright it's been a while since I've used BSD but that looks okay so the next question is, do you have a firewall running? It sounds like there's something running on OS X that handles opening and closing ports for you which is probably why Mongrel isn't being seen by the outside world.

At this point it may be easier to just hook Mongrel up through Apache. Unfortunately I'm also not an OS X user so I can't tell you how to fiddle with that Control Panel and the different versions of Apache you have installed to get that to work with Apache 2.

If you want to try it with Apache 1.3 you can do something like this. Setup Mongrel with the -a switch and set it to 127.0.0.1 like in my previous example. Then in your Apache httpd.conf file you would have something like:

<VirtualHost *:80>    ServerName myapp.com    ServerAlias www.myapp.com

   ProxyPass / http://127.0.0.1:8000/    ProxyPassReverse / http://127.0.0.1:8000 </VirtualHost>

Change ServerName and ServerAlias as appropriate or leave them that way if you just want somebody to access your box by your IP.

The ProxyPass and ProxyPassReverse directives requires that you have the mod_proxy module installed so you'll need to check for these two lines in httpd.conf:

LoadModule proxy_module libexec/libproxy.so

AddModule mod_proxy.c

The above instructions work on Linux but I don't know if OS X handles the Apache config differently.

Dan Kelley wrote:

I'm starting to see light at the end of this tunnel...

Do I need to do any tricks with directories? If I do as suggested (proxying to "/"), then I lose the rest of the machine's pages. So I tried as at the end of this post. This halfway works: if I browse to "http://(MY_HOST)/myapp/(A_PAGE)", where "A_PAGE" is the name of a page in my app, then I get the page, but no style sheet. Also, the links in the page don't work. The links are going to e.g. "http://(MY_HOST)/(ANOTHER_PAGE)", i.e. the "myapp" part is missing in the links.

The link/css issues seem to stem from a problem in the construction of relative filenames.

Q: Is there an additional trick with apache?

BTW, I also tried proxying to "/myapp", i.e. without the trailing slash, but that leads to "page does not exist" errors on all but the rails-supplied "welcome aboard" page.

OK, so here is what I have so far. I've done the other suggested things to my http.conf, and it passes cleanly through a /usr/sbin/apachectl stop/configtest/start cycle (NB. I'm here being careful to use apache 1.3, not the apache 2.0 that I had previously thought I would be required to use, which is how this thread got started!)

<VirtualHost *:80>     ServerName myapp.com     ServerAlias myapp.com     ProxyPass /myapp/ http://127.0.0.1:8000/     ProxyPassReverse /myapp/ http://127.0.0.1:8000/ </VirtualHost>

Okay two more changes.

In your httpd.conf setup change it to:

<VirtualHost *:80>      ServerName myapp.com      ServerAlias myapp.com      ProxyPass /myapp http://127.0.0.1:8000/myapp      ProxyPassReverse /myapp http://127.0.0.1:8000/myapp </VirtualHost>

Then in your command line that starts Mongrel (you'll need a recent version of Mongrel for this) you need to add:

--prefix /myapp

E.g.:

mongrel_rails start -d -p 8000 \      -a 127.0.0.1 \      -e production \      --prefix /myapp

Dan Kelley wrote:

Michael, you're a gem. Links work, and the style sheet is being obeyed.

The one remaining problem is images -- they are not showing up.

That should work with just the DocumentRoot pointing to the public directory within the VirtualHost directive. Did you put the images somewhere else? When you view the source of the page, what path is it using for the images?

You could try something like this within VirtualHost:

ProxyPass /myapp/images ! Alias /myapp/images /path/to/public/images

assuming it's trying to use /myapp/images as the path to the images in the source.

Dan Kelley wrote:

Um, none of the following works. My images are in (MY_APP)/public/images.

# test 1 ProxyPass /images ! Alias /images (APP_PATH)/public/images

# test 2 ProxyPass /(APPNAME)/images ! Alias /(APPNAME)/images APP_PATH)/public/images

# test 3 ProxyPass /(APPNAME)/public/images ! Alias /(APPNAME)/public/images APP_PATH)/public/images

Where are you setting DocumentRoot? Do you have any funky mod_rewrite stuff (RewriteCond, RewriteRule, etc.) going on?

If you view source on one of your Rails pages with an image, what's the URL it is trying to access?

Dan Kelley wrote:

Where are you setting DocumentRoot?

I'm not setting DocumentRoot inside the <VirtualHost>, because I found that if I did so, my other pages disappeared.

Do you have any funky mod_rewrite stuff (RewriteCond, RewriteRule, etc.) going on?

Well, nothing that I have set myself. I do see the following in the file, though.

<IfModule mod_rewrite.c>     RewriteEngine On     RewriteCond %{REQUEST_METHOD} ^TRACE     RewriteRule .* - [F] </IfModule>

If you view source on one of your Rails pages with an image, what's the URL it is trying to access?

The images are being accessed from the stylesheet. I should have pointed that out. For example, in

(MY_APP)/public/stylesheets/style.css

I have a line like the following:

background-image: url('/images/background-01.jpg');

Okay try it like this:

ProxyPass /images http://127.0.0.1:8000/myapp/images

with no Alias

Dan Kelley wrote:

*success* at last! Michael, thank you *so* much for your very generous help, and for your patience through so many exchanges.

It probably would've been easier to ask an OS X guru how to open up a port for Mongrel :smiley: