mongrel_service alternative: using thin and srvany

So i am developing and deploying Ruby on Rails applications on Windows. Recently i started experimenting with the newer versions of ruby (mingw32platform). One of the side-effects is that the mongrel-service, which i always used to deploy my rails-applications no longer works on mingw32. And aside of that, i have read on various instances [ref] that thin should be better (more efficient) than mongrel. But i still want to install it as a service. First off, you need to download and install the Windows Resource Kit. This contains the executables to make a service of any executable or script. Then create the service running (in the console): [bash] C:\Program Files\Windows Resource Kits\Tools> instsrv "[my_service_name]" "c:\program files\Windows Resource Kits\Tools\srvany.exe" The service was successfuly added! Make sure that you go into the Control Panel and use the Services applet to change the Account Name and Password that this newly installed service will use for its Security Context. [/bash] This will add an empty entry in the registry, which actually still does nothing. To get it working, we have to start regedit, and navigate to the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services[my_service_name] Create a new key (folder) named Parameters. This will contain the settings of our actual application that will be run as a service. Add the following String Values: [bash] [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services[my_service_name]\Parameters] Application=c:\ruby\bin\ruby.exe AppDirectory=[my_ruby_app_folder] AppParameters=c:\ruby\bin\thin start -p 4000 -e production [/bash] Assuming that c:\ruby\bin is your ruby folder, and you want your thin to listen to port 4000. Once that is done, start the service, and your rails application should be up and running :)

... but Windows Server 2008

Unfortunately, to do this on Windows 2008 you need to perform some extra steps. As you can't install the resource kit on Windows Server 2008R2 for some peculiar reason, and at the time of this posting, i have not found a Windows Server 2008 Resource Kit Tools, so i took the following steps:

  1. copy the "srvany.exe " from the "Windows Server 2003 Resource Kit Tools " to a suitable location on your Win2008 server (e.g. C:\Windows\System32\ )
  2. use "sc " to create a new service that launches "srvany " (e.g. sc create MyService binPath= C:\Windows\System32\srvany.exe DisplayName= "My Custom Service" )
  3. using RegEdit create a "Parameters " key for your service as before and fill it with the 3 string values A bit more work, but not too much.

mongrel-service?

And just today I discovered an update of mongrel_service was announced, so you can still work using mongrel_service instead of using thin and manually creating the service. What you need to do:

  • install mongrel service: gem install mongrel_service --prerelease
  • create the service as before: mongrel_rails service::install -N myapp -c c:\my\path\to\myapp -p 4000 -e production That is much easier of course.

Comments
gamov 2011-03-11 03:35:52 UTC

Hello, I'm struggling to find a way of deploying rails3/ruby192 apps on Windows. Your solution seems excellent but you don't specify the rails version you are using? Another question for you? Is it easy to compile thin on Windows or it's a headache? gem install thin doesn't look very good since it wants to build stuff which is always scary on Windods :D Thanks for your time, Gam

Nathan 2011-03-11 09:50:21 UTC

I used this solution both on ruby 1.8.7 and 1.9.2. Just make sure you get a ruby from the mingw32 platform (hhtp://rubyinstaller.com). If you install the developer kit (http://rubyinstaller.org/add-ons/devkit/) as well, installing a gem like thin will just work (and it will compile on the fly). So it actually is really easy. Good luck!

Marco Matrodonato 2011-03-15 10:28:25 UTC

Hello, i use mongrel_service and mingw32 on production environment built on windows 2008 R2 64bit, it works.

Will 2011-09-14 18:40:43 UTC

Great article, Nathan! Thanks! I was able to get this working with Windows 2008 R2, Ruby 1.9.2 and Rails. Started serving with Webrick and switch to thin. I am also using Application Request Routing via IIS7 to balance/host. (http://www.iis.net/download/ApplicationRequestRouting) gamov, you can also install eventmachine easily by specifying a platform... just have a look at: gem list -r eventmachine ...you should get something like: eventmachine (0.12.10 ruby java x86-mswin32-60 ...) ...the list after the version is a list of available platforms... so: gem install eventmachine --platform=x86-mswin32-60 ...should fix you up. Thins is definitely worth the trouble.

Will 2011-09-14 19:05:49 UTC

UPDATE: I lied. The platform option installs a version of eventmachine linked against the 1.8, not the 1.9... so it works for 1.8 folks not the 1.9. Sorry, for the confusion there.

Nathan 2011-09-15 09:30:46 UTC

Glad the article was useful for you.

Ray Shih 2011-10-02 05:11:45 UTC

Very Helpful, Thank you!!!

Rachid Benkirane 2013-04-21 11:23:27 UTC

How about the balance loading in thin with apache webserver? How can I configure apache to do this? Thanks in advance

Lourdes Van-Koningsloo Higon 2013-10-28 12:52:30 UTC

I have a problem with thin server. I have created a service in windows and load well, but when I put the address in internet and I get an error The Page can't be displayed. If I do rails s -e production from command line is work perfectly. Could you help me?

nathanvda 2013-10-28 13:26:50 UTC

Thank you Lourdes, for another question in the category: "please use your psychic skills to guess my problem". I could help you, with a little more information. Did you create the service as I described it? When the service is starting, do you see the production.log file being made, and if so: do you see any errors? If it is not starting: check the application folder in your service configuration. Do you start the service on port 4000 (as I suggested) and are you checking on port 4000? E.g. <code>http://localhost:4000</code>

Lourdes Van-Koningsloo Higon 2013-10-28 14:57:33 UTC

Hello nathanvda. I have install windows 2008, I create the service as you described it, in port 4000. I create in regedit AppDirectory, Application and AppParameters. AppDirectory= C:\Sites\aplicaciones Application = C:\RailsInstaller\Ruby1.9.3\bin\ruby.exe AppParameters = C:\RailsInstaller\Ruby1.9.3\bin\thin start -p 4000 -e production. Service run correctly without errors. if I put http://localhost:4000 the error is This page can't be display If I run from command line rails s -p 4000 -e production is ok. In production.log I can't see any error. Thanks in advanced. Lourdes

nathanvda 2013-10-28 15:11:16 UTC

My spanish is no good, but the c:\sites\aplicaciones is the rails folder? because it sounds like applications, and it should be the rails root folder. Otherwise I have no clue. Is the error you get a rails error (this page can't be displayed?). If so, the rails server is hit. Maybe proxy settings? Try running <code>thin start -p 4000 -e production</code> from the command line?

Lourdes Van-Koningsloo Higon 2013-10-28 15:26:51 UTC

No, c:\sites\aplicaciones is my aplication. If I try thin start -p 4000 -e production from the command line, it gives me an error: You have alredy activated rack 1.5.2 but your gemfile requiere rack 1.4.5. Using bundle exect you may solve this. Sorry but before I used mongrel_service and I have update rails in a new server and I install rails 3.2.13, and mongrel_service doesn't work. Thanks

nathanvda 2013-10-28 17:22:56 UTC

The mongrel_service will most likely have the same error. Easiest to update rake in your gemfile to the newest version? Or use <code>bundle exec</code>. Nice it is solved.

Lourdes Van-Koningsloo Higon 2013-11-22 08:27:09 UTC

Hello nathanvda, how many services whith thin I can run? I have three ( ports 4000, 40001, 4002), but today I create other (4003), and the the threee frists doesn't work Thanks in advanced. Lourdes

nathanvda 2013-11-22 09:45:31 UTC

There is normally no limitation, except for available memory and database connections (resources). Normally one extra would not be a problem, and even if it was, it would only be a problem for the one extra (if it is a lack of resources that is the cause of your problem). So I think you have another problem :)

Lokendra 2014-01-28 10:33:48 UTC

Got an error 'http://localhost:4000' while using using thin server as a service with Rails 3. My production.log file shows the text "Connecting to database specified by database.yml". Any help would be appreciated. Thanks

nathanvda 2014-01-28 11:22:04 UTC

Hi Lokendra: that is not really an error? :) My guess: have you checked the <code>production</code> entry in your `database.yml` ? It seems like it is trying to connect and does not find the database? If that does not solve it: check stackoverflow.com and/or ask your question there. You will need to provide a little more background, because you provide very little information here.

Lokendra 2014-01-28 12:46:49 UTC

Hi Nathanvda, Thanks for the quick response. Actually the same database details works when I run thin server from command prompt, but through service I got error. And my database.yml file contains information: production: adapter: sqlserver dsn: DSN_Name # created System DSN mode: odbc database: Database_Name username: sa password: passwd host: 127.0.0.1

nathanvda 2014-01-28 13:15:47 UTC

Do you run thin server from command prompt in production mode? Please note, like I said, that stackoverflow is a better forum for such questions :)

Jenny 2014-05-12 13:42:59 UTC

Hello, I install ruby193, now I want to create windows service for redmine via mongrel service, but when I write the command mongrel_rails service::install -N RedMine -c d:RailsInstaller\sites\redmine(this is the path for redmine) -p 3000 -e production, it shows an error as below: d:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb: 36:in `require': 126: The specified module could not be found. - d:/RailsInsta ller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5-x86-mingw32/lib/http11.so (LoadError) from d:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custo m_require.rb:36:in `require' from d:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5- x86-mingw32/lib/mongrel.rb:12:in `' from d:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custo m_require.rb:36:in `require' from d:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custo m_require.rb:36:in `require' from d:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5- x86-mingw32/bin/mongrel_rails:11:in `' from d:/RailsInstaller/Ruby1.9.3/bin/mongrel_rails:23:in `load' from d:/RailsInstaller/Ruby1.9.3/bin/mongrel_rails:23:in `' I am new for redmine, ruby, and mongrel, so can anybody help me on this ? Thank you.

nathanvda 2014-05-12 14:11:22 UTC

Can you ask this question on stackoverflow, and post the link here? Please note that mongrel_rails is no longer at this moment, so you better use thin.

Daniel 2015-02-09 23:06:12 UTC

Hi, I realise I'm posting this late but everything is working fine on Windows 7 but when I run the service it runs but no sign of thin server running, i.e. no logs being created. I'm wondering could you give me more info on how to create the String Values, might be doing this wrong. Thanks, Dan

Abhishek Srivastava 2015-12-18 06:16:54 UTC

I am using the same approach and I don't see any logs either. I am trying to debug a issue and have no clue now.

nathanvda 2015-12-18 08:32:17 UTC

My best guess: did you try running <pre>rails s -p 4000 -e production</pre> on the command-line? (as mentioned before).Running a rails app in production is something different: it will connect to a different database entry, and it will pre-load all code, meaning any error (even in unused code) will block starting your rails process.

Add comment

Recent comments

Tags

ruby on rails 34 ruby 26 rails3 17 rails 15 oracle 11 rspec 9 rspec2 7 jquery 7 ubuntu 5 javascript 5 windows 5 activerecord 3 refactoring 3 geoserver 3 gis 3 arrrrcamp 3 actionmailer 2 oracle spatial 2 tdd 2 postgis 2 routing 2 rvm 2 mongoid 2 csharp 2 thin 2 win32 2 gem 2 rails4 2 git 2 service 2 haml 2 cucumber 2 view testing 2 i18n 1 displaysleep 1 spatial 1 gemsets 1 wubi 1 oracle_enhanced_adapter 1 migrations 1 watchr 1 ci 1 plugins 1 coderetreat 1 ie8 1 ssl 1 oci 1 nested model form 1 wcf 1 11.04 1 jsonp 1 ruby-oci8 1 teamcity 1 engines 1 pgadmin 1 soap 1 content_for 1 word automation 1 plugin 1 capybara 1 xml 1 bootstrap 1 migrate to rails3 1 mvc 1 unity 1 rendering 1 word2007 1 x64 1 limited stock 1 fast tests 1 pl/sql 1 delayed_job 1 pdf 1 test coverage 1 optimization 1 processing 1 borland 1 method_missing 1 cross-browser 1 devise 1 schema_plus 1 mongo 1 mongrel 1 dual boot 1 usability 1 mongrel_service 1 dba 1 mission statement 1 model 1 metadata 1 rcov 1 exceptions 1 image_tag 1 attachments 1 bde 1 css 1 yield 1 ajax 1 generative art 1 rails-assets 1 coordinate systems 1 submodules 1 netzke 1 ora-01031 1 authlogic 1 postgresql 1 shopping cart 1 agile 1 fast_tagger 1 subjective 1 wice_grid 1 generators 1 nvidia 1 mongodb 1 etsyhacks 1 staleobjecterror 1 session 1 jeweler 1 wordpress hacked 1 jasmine 1 heroku 1 rjs 1 life 1 unobtrusive-javascript 1 render_anywhere 1 html5 1 rails31 1 json 1 cocoon 1 mingw32 1 observe_field 1 osx 1 actionwebservice 1 testing 1 debugging 1 strings 1