Blagador! – marvspace.com official Blog
18Oct/0927

How to Setup a Rails application in cPanel

Setting up a Rails application should be painless, but there are times when some hosting restrictions make deployment a real pain in the ass. I would like to share with you some tips that hopefully will help you get over some of the obstacles in deploying a Rails application using cPanel.

Currently my hosting provider does not provide shell access to my account. As we all know, shell access can spell the difference between a good dream and a nightmare deployment.

As a note, all of this was done using cPanel 11.24.5-RELEASE.

Step 1: Create your application in your local machine or from any source

Its as simple as running rails my_app, then populating the code with whatever you want it to be.

Step 2: Check the Rails gems in cPanel

Make sure that the Rails version on cPanel is the same as the version used in your application:

  1. Login to your cPanel administration panel. e.g. http://myapp.com/cpanel
  2. Enter your username and password
  3. Under Software and Services, click RubyGems
System-installed Ruby Gems

System-installed Ruby Gems

cPanel is using the latest available Rails gems. If your application is created using an earlier Rails version, you will need to "vendor" the older version of Rails (as described later in the post).

Step 3: Create the Rails application directory using cPanel

  1. Login to your cPanel administration panel. e.g. http://myapp.com/cpanel
  2. Enter your username and password
  3. Under Software and Services, click Ruby on Rails
    rails_dep1
  4. Enter your application name under App Name
  5. Check Load on Boot (I haven't tested this yet, but some sources say that this is not working, but check it anyway)
  6. The Application Path will be automatically updated using your application name. Take note that your Rails application is now residing under /home/<user>/etc and not in /home/<user>/public_html. This was probably due to security reasons to make your code inaccessible to prying eyes.
  7. Set the Environment to production mode
  8. Create! cPanel will automatically generate all the necessary files and folders needed by your application

Step 4: Vendor your ruby gems

Vendoring gems means putting the gems used by your application in the vendor/gems directory. This will ensure that your application will still run smoothly even if the system-installed gems in the server is changed. To properly do this, follow these steps:

  1. In your local machine, make sure the gem you will vendor is currently installed. If not, run
    gem install <gem name>
  2. Put the proper config.gem in config/environment.rb for each gem you will use
  3. Unpack the gems to be used  in  into the vendor/gems directory using this command:
    rake gems:unpack
  4. Check each unpacked gem if it contains a .specification file.

Example: Vendoring the sqlite3 gem

In my case, the sqlite3 gem is not included in the system-installed gems. Since I will be using sqlite3 and not mySQL as my database, I need to vendor it.

  1. In your local machine, install the sqlite3 gem (if it is not installed yet)
    gem install sqlite3-ruby
  2. Uncomment the config for the sqlite3 gem in config/environment.rb
    config.gem "sqlite3-ruby", :lib => "sqlite3"
  3. Run rake gems:unpack

Step 5: Populate your application with your code

Its now time to upload your code into cPanel. There are many ways to do this, but what I did was to upload my code using FTP and then moving the uploaded code into the main application under /home/<user>/etc/rails_apps/myapp

You will need to upload the following folders: app, vendor, db, config, lib, and public

Warning!
If your application uses Rails plugins, the vendor directory may contain thousands of files, making uploading it very slow. To solve this, I highly recommend archiving/compressing the directory first. Use gzip to compress the archived file. Do not use other types of compression (e.g. zip, rar) since they are not recognized by the cPanel Extract command!

To archive and compress a directory (for example, the vendor directory):
tar -cf vendor.tar <path of folder>/vendor
gzip vendor.tar

After compressing, upload the *.tar.gz file into cPanel. When the upload is finished, go to the cPanel File Manager. Navigate to the folder where you uploaded the file, check the *.tar.gz file and then press the Extract button on the top menu. Enter the directory where you want it to be extracted, and then press Extract. A window showing the files being extracted must be displayed. If the only thing you see is a loading bar saying that the file is being extracted, then the compression type you used is not recognized by cPanel.

After extracting the file, copy/move its contents to the main application directory in /home/<user>/etc/rails_apps/myapp

Step 6: Run your application!

It is now time to test your application. cPanel provides a simple method of stopping or starting your Rails application. Just go to Software and Services, click Ruby on Rails and click either the Run or Stop buttons to start or stop the mongrel processes. There are some things to remember though!

  1. Make sure you deleted/renamed the default Rails index page located at:
    /home/<user>/etc/rails_apps/<myapp>/public/index.html
  2. Check the log files if your application refuses to run. The log files are located at:
    /home/<user>/etc/rails_apps/<myapp>/log/mongrel.log
    /home/<user>/etc/rails_apps/<myapp>/log/production.log
  3. Do not delete the mongrel process id (pid) file! This file is needed by cPanel to determine which processes to stop when you need to stop your Rails application. I accidentally deleted this one time, and because of it I was unable to restart my application and I needed to contact the system administrator to manually kill the mongrel process (since I do not have shell access). This file is located at (why do they put it there...):
    /home/<user>/etc/rails_apps/<myapp>/log/mongrel.pid

Step 7: Create a Rewrite

Your Rails application is now running at a different port than your main URL. For example, the Rails application running in marvspace is at http://marvspace.com:12001. What you want is to make your main URL to point to this specific port. To do this, you need to create a Rewrite in your site public_html/.htaccess file to redirect all requests from your main URL to your Rails application running at a different port.

Fortunately, cPanel provides a very easy way to create a Rewrite. All you need to do is go to Software and Services, click Ruby on Rails and click Create a Rewrite beside the application name. If you check your .htaccess file, you will see these lines:

RewriteEngine on
Options All -Indexes
RewriteCond %{HTTP_HOST} ^marvspace.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.marvspace.com$
RewriteRule ^/?$ "http\:\/\/127\.0\.0\.1\:12001%{REQUEST_URI}" [P,QSA,L]

To put it into plain words, these lines mean: "If a request starting with marvspace.com or www.marvspace.com is accessed, that request is to be handled by the application running at port 12001."

Step 8: Got Errors?

Sometimes your Rails app refuses to run even if you already checked the above things. I will share to you some of the problems I encountered and how to solve them.

Error: My Rails app wont start and the production logs do not show anything.
Solution: Restart your application in development mode. View the development.log file to see the actual cause of error.

Error: no such file to load -- sqlite3
Solution: The sqlite3 gem is not installed in the system. Follow the example at Step 4 to install the sqlite3 gem

Error: libsqlite3.so: cannot open shared object file: No such file or directory
Solution: The system does not have the shared object file required by sqlite3. To solve this, you need to put the  libsqlite3.so file directly into your filesystem.

  1. Download the libsqlite3.so file here.
  2. Put this file into any accessible directory. In my case I put it in /home/<user>/bin
  3. Edit the sqlite3 gem to include the uploaded libsqlite3.so file.
    In /vendor/gems/sqlite3-ruby-1.2.4/lib/sqlite3/driver/dl/api.rb
module SQLite3 ; module Driver; module DL;
  module API
    extend ::DL::Importable
    library_name = case RUBY_PLATFORM.downcase
      when /darwin/
        "libsqlite3.dylib"
      when /linux/, /freebsd|netbsd|openbsd|dragonfly/, /solaris/
        "/home/<user>/bin/libsqlite3.so"
      when /win32/
        "sqlite3.dll"

Restart your application for your changes to take effect.

Error: My Rails application started but there are no images or the stylesheet is not loaded.
Solution: The Rewrite generated by cPanel is faulty. To fix this, you need to edit this file and update the Rewrite rule:
/home/<user>/public_html/.htaccess

Open the .htaccess in your text editor and change
RewriteRule ^/?$

to
RewriteRule ^.*$

Error: I edited my .htaccess file and now my files and folders under public_html are now inaccessible via URL
Solution: Since the updated Rewrite basically says that all requests be redirected to the Rails application, the URLs your other non-Rails directories are now being routed to the Rails application, causing an error. To circumvent this, either edit again the .htaccess file to exclude certain requests or use subdomains.

Comments (27) Trackbacks (0)
  1. hi,
    I have deployed my rails application at home/user/etc/rails_apps/myapp, and the application runs, but wont get any stylesheets or images.
    This is how my .htaccess file in public_html/ looks:

    RewriteCond %{HTTP_HOST} ^mydomain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.mydomain.com$
    RewriteRule ^myapp “http\:\/\/127\.0\.0\.1\:12009%{REQUEST_URI}” [P,QSA,L]

    how do I edit this to get the stylesheets, javascipts, images…

  2. Hello Ajey,

    Try changing the last line to:
    RewriteRule ^.*$ “http\:\/\/127\.0\.0\.1\:12009%{REQUEST_URI}” [P,QSA,L]

    Let me know if it works.
    Cheers

  3. Hi Marv,

    Worked great.
    Thanks for the article and your support.

    Clear skies
    Keep Rubying

  4. Ajey,

    That’s nice to hear. Good luck!

  5. Hello Marvs,
    Nice post… I tried the same to run my app on cpanel..but i m not able to even get rails main default index page…it always show connection timed out…
    wht to do???

  6. Hello Hardik,

    Are you sure you are accessing your application from the correct port? You can always check the port number of your app under Ruby on Rails in cPanel.
    Also, please check if your app is actually running, it could also be possible that there is an error in your app which prevents cPanel from running it.

  7. in cpanel url redirects me to port num 12007, and a fresh rails app must show its default rails page…url generated by cpanel always shows me connection timed out…m using cpanel version 11.25

  8. Hello Hardik,

    In all honesty its really hard to determine the problem with only limited information :)
    My suggestion is that you check out the log files of your application, see what you can find there:
    /etc/rails_apps/MYAPP/log/production.log
    /etc/rails_apps/MYAPP/log/development.log

    Maybe there are some errors which prevent the app from running.

  9. i hav checked log folder..development and production log both are empty..only mongrel log hav some details….but didnt found any error in it..it seems tht rewrite is not working properly….
    even i tried “www.mydomain.com:12007/”
    but same error..”Connection timed out.”
    Here is mongrel log…

    ** Daemonized, any open files are closed. Look at log/mongrel.pid and log/mongrel.log for info.
    ** Starting Mongrel listening at 0.0.0.0:12007
    ** Starting Rails with production environment…
    ** Rails loaded.
    ** Loading any Rails specific GemPlugins
    ** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).
    ** Rails signals registered. HUP => reload (without restart). It might not work well.
    ** Mongrel 1.1.5 available at 0.0.0.0:12007
    ** Writing PID file to log/mongrel.pid

    Most intresting issue is tht production log is empty….

    And thank for ur response…..

  10. Hello Hardik,

    Yes, I remember that I encountered something like this before. The production log is empty and my application is not running. What I did was to run the application in development mode, then check the development.log file. That’s how I found out about the sqlite3 error. Your case may be different though, but it never hurts to try.

  11. Okay thnx for your support and response Marvs….I think I should try some another way..like replacing cpanel…as I m trying to solve this issue since last two months…

    Thnx.

  12. No problem Hardik. Its really a pain in the a$$ trying to run Rails without SSH access, so yeah, cPanel might not be what you are looking for.

  13. i do hav ssh access….can u guide me how to solve this wierd problem with help of ssh…i hav tried to run my app from ssh….n app was started properly..n didnt show any errors…i started my app on 0.0.0.0:12007

  14. I tried to boot webrick from ssh..and i got this pointed line show smthing wired…
    => Booting WEBrick
    => Rails 2.3.2 application starting on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    [2010-02-23 12:37:09] INFO WEBrick 1.3.1
    [2010-02-23 12:37:09] INFO ruby 1.8.7 (2008-06-20) [i686-linux]
    ——->>>>[2010-02-23 12:37:09] WARN TCPServer Error: Address already in use – bind(2)<<<<———-
    [2010-02-23 12:37:09] INFO WEBrick::HTTPServer#start: pid=29769 port=3000

    any idea?????

  15. Hi Hardik,

    It means that port 3000 is already in use. Use
    ruby script/server -p port_number
    where port_number can be of any value, maybe 12010 or something. You can set this value as long as the port is not used.

  16. aww. i’ve got same problem like Hardik. and make it worse, i’m using shared host and no ssh. any idea will be appreciate. thanks.

  17. Hello natsu90 and marvs,
    I found the problem…..natsu..I m using shared hosting and ports on which its rails apps run..they were blocked by firewall…..so u can ask ur hosting provider to open those ports for u so u will able to use ur rails apps…

  18. That is great news, Hardik! I am glad you found the solution.

  19. Hi,

    This post has helped me a lot. However, I still have not managed to display the images or css in a application that I want to work in a sub-uri. I mean, instead of starting a login function such as:
    mydomain.com/login
    I would like to run as
    mydomain.com/myapp/login

    I can make the application to work adding in the environment a config.action_controller.relative_url_root = “/myapp”
    but it won’t load any css or images.

    Please, do you know what could be the cause and how to fix it?

    Thank you in advance

  20. Hello Albert,

    I haven’t used relative_url_root yet so I can only guess. I think the htaccess file must be modified to rewrite requests coming from the public folder (where images and stylesheets are located). I found a post which may help you get to the solution.

    http://www.hostingrails.com/ActionController-AbstractRequest-relative_url_root

    My knowledge in rewrites are very limited, so unfortunately I cannot help you with the details. Thanks :)

  21. Hey! My .htaccess file is different. cPanel 11.25

    RewriteEngine on
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    AuthName MYSITE.com
    AuthUserFile /home/MYUSER/public_html/_vti_pvt/service.pwd
    AuthGroupFile /home/MYUSER/public_html/_vti_pvt/service.grp
    Options All

    tried some twieks but didn’t work. Does any one have any idea? Imgs and CSS are not showing.

    Thanks!

  22. ignore my last post! I was checking the wrong .htaccess! =D

  23. Hi Celso, glad you sorted that out! :)

  24. Hello

    Bye

  25. Excellent Post. As a result, I am down to, hopefully, my last problem. I’m using a slightly modified version of the application, depot, from Agile Web Development as a test case which runs on my local computer and server. However, I get a failure:

    ActionView::TemplateError (undefined method `product_path’ for #) on line #53 of app/views/products/index.html.erb:
    50:
    51:
    52:
    53:
    54: ‘Are you sure?’,
    56: :method => :delete %>

    app/views/products/index.html.erb:53
    app/views/products/index.html.erb:19:in `each’
    app/views/products/index.html.erb:19
    app/controllers/products_controller.rb:14:in `index’

    when running on the host system using cPanel. I do not use the vendor system because the system gems I use seem to be all there.

    Thanks already for your post, and thanks if you can point me in a different direction to debug this.

  26. Oops, in the above post, the source lines didn’t copy correctly. Let me try again.

    50:
    51:
    52:
    53:
    54: ‘Are you sure?’,
    56: :method => :delete %>

  27. Hi Marvis,

    how to config libsqlite3.so into version 1.3.0 of slite3-ruby on the cpanel

    thank you


Leave a comment


No trackbacks yet.

WTF is this?

Marvs is an electronics engineer who does web applications for a living.

Time Flies so Fast

September 2010
S M T W T F S
« May    
 1234
567891011
12131415161718
19202122232425
2627282930  

Recent Blagadag!

Categories

Kala mo naman sikat ka na…

Meta

Live Traffic Feed