How to Install OpenResty and Passenger on Ubuntu 16.04 (Xenial Xerus)

Making OpenResty and Passenger work together isn't too hard, but it's not obvious how to do it. There's a lot of guides for getting Passenger to work with Nginx, but the process for OpenResty is a little different. For that reason, I'm documenting how I got them to work together here.

Step 1: Install Ruby

The script below installs RVM and version 2.3.3 of Ruby. To check for newer versions of Ruby you can ask RVM to list all known rubies with rvm list known.

echo "INSTALL RUBY WITH RVM"
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm use 2.3.3 --install --default

Step 2 (Optional): Update RubyGems

If we're going to use a Ruby gem, we might as well update the platform.

echo "UPDATE RUBYGEMS"
gem install rubygems-update --no-rdoc --no-ri
update_rubygems
gem update --system
gem pristine --all

Step 3: Install the Passenger Gem

The Passenger gem is an easy way to get the passenger source code onto your system. We need it so we can compile passenger into OpenResty.

echo "INSTALL PASSENGER GEM"
gem install passenger --version 5.1.2  --no-rdoc --no-ri

You can check for the latest version on the Passenger RubyGems page.

Step 4: Install OpenResty

In this step, we do several things. First we download the OpenResty source code. (Check the OpenResty download page for the latest version of OpenResty.) Then we unpack it. Then we configure it and indicate that we want to add the passenger module. Finally, we make and install it into /opt/openresty/. The configure options are the same options that the Passenger gem uses when installing Passenger+Nginx with the command passenger-install-nginx-module. (You can't use passenger-install-nginx-module with OpenResty because OpenResty's configure script is a Perl script, and passenger-install-nginx-module tries to call it with sh. Trying to run a Perl script as a Bash script results in all kinds of bad things.)

echo "INSTALL OPENRESTY (NGINX) AND PASSENGER"
cd ~
wget https://openresty.org/download/openresty-1.11.2.2.tar.gz
tar xvfz openresty-1.11.2.2.tar.gz
cd ./openresty-1.11.2.2
./configure --prefix='/opt/openresty' \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module \
  --with-http_addition_module \
  --with-cc-opt=-Wno-error \
  --with-ld-opt='' \
  --add-module='/home/ubuntu/.rvm/gems/ruby-2.3.3/gems/passenger-5.1.2/src/nginx_module'
make
sudo make install

If you follow the directions above, you will have Ruby 2.3.3, OpenResty 1.11.2.2, and Passenger 5.1.2 installed on your system. Ruby will be installed at ~/.rvm/rubies/ruby-2.3.3/bin/ruby OpenResty will be installed at /opt/openresty. And Passenger will be installed at ~/.rvm/gems/ruby-2.3.3/gems/passenger-5.1.2