crantok nginx

Opalstack's 1-click Rails installer sets up the latest version of Rails running on Puma as the backend server, all powered by the system Ruby. This works pretty well for new apps but not so great for old Rails apps that might not be compatible with the system Ruby or for customers who prefer Nginx+Passenger instead of our simpler Puma setup.

Here's how you can get that old Rails app up and running on Opalstack, complete with its crusty old versions Ruby and the entire Nginx+Passenger stack. This procedure will set up the following:

  • Ruby 2.0
  • Rails 3.2
  • Passenger 4
  • Nginx 1.0.11

⚠️This is not a one-size-fits-all procedure. If you need a different version of something then adjust the procedure accordingly. This is written to help people get older apps up and running, but you should be able to adapt it to any version of Ruby that's supported by rvm on CentOS7 and whatever it is that you want to run (Rails, etc).

Here we go! 🚀

  1. Log into your Opalstack dashboard and create a new "proxy port" application. Make a note of the application name and port assignment. The rest of this procedure assumes a shell user named myuser with an app named myapp assigned to port 55555.

  2. SSH to your Opalstack server as the app's shell user and install rvm...

gpg --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
  1. ... then install Ruby 2.00 with rvm ...
rvm install 2.0.0
rvm use 2.0.0
  1. ... then install the Rails and Passenger gems:
gem install rack-cache -v 1.9.0
gem install rails -v 3.2.13
gem install passenger -v 4.0.2
  1. Next, download and install Nginx and Passenger. We're using Nginx 1.0 here because it is contemporaneous with the Passenger and Ruby versions that we're using:
mkdir -p $HOME/tmp
export TMPDIR=$HOME/tmp
mkdir ~/apps/myapp/src
cd ~/apps/myapp/src
wget http://nginx.org/download/nginx-1.0.11.tar.gz
tar zxf nginx-1.0.11.tar.gz
cd ..
passenger-install-nginx-module --auto --extra-configure-flags=none --nginx-source-dir=$PWD/src/nginx-1.0.11 --prefix=$PWD/nginx
  1. Upload your Rails project into the app directory /home/myuser/apps/myapp. The remaining steps will use myproject as the project name. If you want to start with a new project for your old Rails app 🤔 then you can make a new Rails project like so:
cd ~/apps/myapp
rails new myproject
# the next line is needed for compatibility with the system sqlite
sed -ie "s/gem 'sqlite3'/gem 'sqlite3', '~>1.3.5'/" myproject/Gemfile
  1. Next, install your project dependencies:
cd myproject
export LD_LIBRARY_PATH=/opt/lib
bundle install
  1. Now edit /home/myuser/apps/myapp/nginx/conf/nginx.conf to be like the following example. Be sure to change the port 55555 and paths to match your own. If you're not sure about the ruby-2.0.0-p648 version in the example then run env | grep GEM_HOME to see what rvm is giving you. Here's the config :
env               GEM_HOME=/home/myuser/.rvm/gems/ruby-2.0.0-p648;
env               LD_LIBRARY_PATH=/opt/lib;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    access_log  /home/myuser/logs/apps/myapp/access_myapp.log  combined;
    error_log   /home/myuser/logs/apps/myapp/error_myapp.log   crit;

    include         mime.types;
    sendfile        on;

    passenger_root /home/myuser/.rvm/gems/ruby-2.0.0-p648/gems/passenger-4.0.2;
    passenger_ruby /home/myuser/.rvm/rubies/ruby-2.0.0-p648/bin/ruby;
    passenger_max_instances_per_app  1;
    rails_spawn_method conservative;
    passenger_max_pool_size 2;

    server {
        listen             55555;
        passenger_enabled  on;
        root               /home/myuser/apps/myapp/myproject/public;
        server_name        localhost;
	rails_env          development;
    }
}
  1. You're ready to start Nginx!
~/apps/myapp/nginx/sbin/nginx
  1. Now run a quick test on the server to see if it's serving up your Rails app:
curl -s http://127.0.0.1:55555/ | grep -q "riding Ruby ons Rails" && echo success || echo fail
  1. If you got success on the last command then go back to the Opalstack dashboard and attach the myapp application to a website. You're done! 🎉

  2. If you didn't get success then check the following logs for errors:

    • /home/myuser/logs/apps/myapp/error_myapp.log

    • /home/myuser/apps/myapp/myproject/log/development.log


You can use the following commands to stop and start the app:

  • Start: ~/apps/myapp/nginx/sbin/nginx
  • Stop: pkill -f ~/apps/myapp/nginx/sbin/nginx

That's pretty much it! Just remember to adjust everything for whatever versions you need, install your gems at the correct versions (hopefully handled by bundler), and you should be up and running in no time. 🏃

If you run into any problems or have any suggestions, interesting use cases, etc then please share them here! 🙂

    sean on step 9 when I try to start nginx, it get the follow error:

    error while loading shared libraries: libpcre.so.0: cannot open shared object file: No such file or directory

    any ideas? this post has been very, very helpful so far! thank you!

    • sean replied to this.

      jzpeepz weird! are you following the procedure exactly as shown, or are you using different versions of stuff?

      If the latter, then which versions?

        sean The only thing different is I am using Rails 4.0

        Here was my workaround:

        mkdir $HOME/lib64
        ln -s /usr/lib64/libpcre.so.1 $HOME/lib64/libpcre.so.0
        export LD_LIBRARY_PATH=$HOME/lib64:$LD_LIBRARY_PATH

        Also of note, I needed you run this before bundle install so the pg gem installed correctly:

        bundle config build.pg --with-pg-config=/usr/pgsql-11/bin/pg_config

        I almost have this thing working!!! 😃

        • sean replied to this.

          jzpeepz glad you got that working! 🎉

          I'm puzzled as to why you needed the workaround for libpcre, I did not run into that at all. Is there any other information from the error message that you can share?

          20 days later

          @sean all thanks to this very helpful article!

          I am going to chalk the libpre stuff up to me trying multiple approaches before finding something that worked.

          Is there a good way to make sure nginx is always running in this scenario? Something like supervisor perhaps?

          Thanks for all your help!

          • sean replied to this.
            2 months later

            I'm installing a ruby 1.9.3 webapp without rails and I've got all the way to starting nginx.

            When I use curl to download the home page, I get the nginx welcome page instead. I tried putting a stub index.html in my public directory but that wasn't returned either.

            There are no errors in the error log.

            The access log shows the same line many times, always 200:
            127.0.0.1 - - [11/Feb/2021:16:50:17 +0000] "GET / HTTP/1.1" 200 151 "-" "curl/7.29.0"

            What would be the best way to debug this?

            • sean replied to this.

              Oops. I was about to list the things I did differently to the instructions above for ruby 1.9.3 without rails, (in case that would help anyone else) and I found my notes about an error while installing ruby...

              $ rvm install 1.9.3
              ...
              ruby-1.9.3-p551 - #installing rubygems-3.0.9..
              Error running 'env GEM_HOME=/home/myuser/.rvm/gems/ruby-1.9.3-p551@global GEM_PATH= /home/myuser/.rvm/rubies/ruby-1.9.3-p551/bin/ruby -d /home/myuser/.rvm/src/rubygems-3.0.9/setup.rb --no-document',
              please read /home/myuser/.rvm/log/1612891048_ruby-1.9.3-p551/rubygems.install.log

              The error seems to be about rubygems and I decided to ignore it. When I was installing my app, bundle install worked, so I assume the error is not relevant to my issue above but in the interests of full disclosure...

              The text in the log file is the following:

              [2021-02-09 17:18:39] /home/myuser/.rvm/rubies/ruby-1.9.3-p551/bin/ruby
              current path: /home/myuser/.rvm/src/rubygems-3.0.9
              PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/home/myuser/.local/bin:/home/myuser/bin:/home/myuser/.rvm/bin
              command(7): env GEM_HOME=/home/myuser/.rvm/gems/ruby-1.9.3-p551@global GEM_PATH= /home/myuser/.rvm/rubies/ruby-1.9.3-p551/bin/ruby -d /home/myuser/.rvm/src/rubygems-3.0.9/setup.rb --no-document
              Exception `LoadError' at /home/myuser/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/rubygems.rb:1264 - cannot load such file -- rubygems/defaults/operating_system
              Exception `LoadError' at /home/myuser/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/rubygems.rb:1273 - cannot load such file -- rubygems/defaults/ruby
              /home/myuser/.rvm/src/rubygems-3.0.9/lib/rubygems/core_ext/kernel_require.rb:54:in `require': /home/myuser/.rvm/src/rubygems-3.0.9/lib/rubygems/core_ext/kernel_warn.rb:15: syntax error, unexpected tLABEL (SyntaxError)
                  module_function define_method(:warn) {|*messages, uplevel: nil|
                                                                            ^
              /home/myuser/.rvm/src/rubygems-3.0.9/lib/rubygems/core_ext/kernel_warn.rb:18: void value expression
              /home/myuser/.rvm/src/rubygems-3.0.9/lib/rubygems/core_ext/kernel_warn.rb:43: syntax error, unexpected '}', expecting keyword_end
                      from /home/myuser/.rvm/src/rubygems-3.0.9/lib/rubygems/core_ext/kernel_require.rb:54:in `require'
                      from /home/myuser/.rvm/src/rubygems-3.0.9/lib/rubygems.rb:1390:in `<top (required)>'
                      from setup.rb:28:in `require'
                      from setup.rb:28:in `<main>'
                      
              /home/myuser/.rvm/log/1612891048_ruby-1.9.3-p551/rubygems.install.log (END)

                Also, some very minor errata in the original instructions...

                Step 8:
                /home/myuser/logs/apps/myapp/nginx/conf/nginx.conf
                should be
                /home/myuser/apps/myapp/nginx/conf/nginx.conf

                Step 12:
                pkill -f ~/apps/oldrails/nginx/sbin/nginx
                should be
                pkill -f ~/apps/myapp/nginx/sbin/nginx

                • sean replied to this.

                  crantok Thanks for the corrections.

                  If the problem is that Nginx is serving up a default welcome page then the issue is likely somewhere in your Nginx config.

                  In your nginx.conf try changing server_name localhost; to server_name your_domain_here.com; (using your actual domain) to see if that makes a difference. You'll need to restart the app to pick up the new config.

                  If it still doesn't seem to work then please email support@opalstack.com with the details (ie shell user and app) and we'll take a closer look.

                    sean Thank you!

                    I had failed to comment out some of the default lines in the config file.

                    I have got as far as my app crashing on load so that's a definite improvement! * putting on rubber gloves emoji *

                    I got my 1.9.3 ruby/sinatra app up and running.

                    Wot I did...

                    Step 3:

                    rvm install 1.9.3

                    I got an error message during installation of 1.9.3 about rubygems. I ignored it and everything still seemed to work. You can see the error message above crantok

                    rvm use 1.9.3
                    rvm gemset create myapp-1
                    rvm gemset use myapp-1

                    Creating and using a gemset helped me later. Destroying the gemset and creating and using a new one was a quick way to make sure I had a clean bundle install.

                    Steps 4 and 5:

                    I did not manually download rack and rack-cache gems. My app has a Gemfile and Gemfile.lock and I assume they will handle all required gems.

                    I installed the versions of Passenger (4.0.2) and Nginx (1.0.11) in Sean's instructions above.

                    Step 6: Ignored

                    Step 7:

                    (I've just noticed that I never did export LD_LIBRARY_PATH=/opt/lib)

                    I made a mistake at this point. I used the bundle install command but I had not installed the bundler gem. This meant that I was using the system bundle program. The bundle install command failed half way through. I installed bundler, which required specifying the version.

                    $ gem install bundler
                    ERROR:  Error installing bundler:
                        bundler requires Ruby version >= 2.3.0.
                    $ gem install bundler -v 1.17.3

                    I compounded my mistake by running bundle install again without deleting the existing gemset. I now had gems installed by different versions of bundle. This came back to bite me.

                    Other steps:

                    The rest of the installation was pretty much as Sean's instructions above except that I piped the result of downloading the homepage using curl to less. Everything worked once I had a cleanly installed set of gems.

                    Mastodon