I am a little nervous to write my first blog post. It’s harder to think and organize my experience on a blog than explain to someone in person over a cup of coffee.
Hmm… there is always a first time for everything.
Hosting ROR on Apache.
There was no prior experience and knowledge of hosting Rails on Apache. Earlier I was shipping code continuously to the outside world through GitHub and Heroku (refer here).
So, did a search on Google as a first thought and then spend some time having a look at the appropriate authority sites phusionpassenger.com, techadmin.net … …
Yeah, reading a continent of information is time-consuming. But the more you read on your areas of interest, the more it gets interesting. The key learning in this case is that Passenger is required to run Rails application on Apache. Apache cannot execute .rb files on the browser like .php files, as Ruby is a server-side language, needs a process to intercept the request, route to the correct Ruby file, execute and send back the response to the browser.
The versatile Apache web server supports multi-tenancy – it can host multiple different applications in the same server to serve different tenants. More on multi-tenancy vs single-tenancy.
Being a full stacker can be a different world – sometimes you have to sing and dance at the same time. So sharing something interesting – So what in the world is a full stack developer?
Let’s get back to Rails hosting
Based on my requirement, I’ll be using Apache integration mode from the available three Passenger integration modes.
- Standalone mode
- Apache integration mode
- Nginx integration mode
This article explains in depth about all the three integration modes.
The Apache Integration Mode
Apache integration mode serves the purpose of handling the HTTP requests for the Rails application. The Passenger module which works within the Apache server environment serves the business logic for the request by interpreting the Ruby application.
Hold on tight as the next part is all about the installation commands. Nothing comes easy but nothing is difficult when one has done the homework and is prepared.
Onboarding rules
- Have the Prerequisites ready
- Bust out the Interpreter
- Find your way
Yeah, I have made sure that I have the superuser(permissions) on the server to do the installation.
1- Prerequisites:
Apache is set up to load the Passenger Apache module during the Passenger installation process.
These commands will install Passenger + Apache module through Phusion APT repository.
Install PGP key and add HTTPS support for API
- $sudo apt-get install -y dirmngr gnupg
- $sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
- $sudo apt-get install -y apt-transport-https ca-certificates
Add the above APT repository to update its database of what packages can be installed and where to install them from.
- $sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
- $sudo apt-get update
The apt-get install causes to find packages in its database, download and install the mentioned file. Install Passenger + Apache module – Registers Passenger-specific configurations inside Apache.
- $sudo apt-get install -y libapache2-mod-passenger
Enable the Passenger Apache module and restart Apache
- $sudo a2enmod passenger
- $sudo apache2ctl restart
After installation, please validate the installation by running the command below
- $sudo /usr/bin/passenger-config validate-install
2 – Bust out the interpreter:
- $sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
- $sudo apt-get update
The apt-get install causes to find packages in its database, download and install the mentioned file. Install Passenger + Apache module – Registers Passenger-specific configurations inside Apache.
- $sudo apt-get install -y libapache2-mod-passenger
Enable the Passenger Apache module and restart Apache
- $sudo a2enmod passenger
- $sudo apache2ctl restart
After installation, please validate the installation by running the command below
- $sudo /usr/bin/passenger-config validate-install
2 – Bust out the interpreter:
Passenger needs to know what Ruby interpreter you are using to compile.
Run on terminal
- $passenger-config about ruby-command
Write down on a paper or copy to notepad, of the path after “Command” (from the above example, /usr/bin/ruby2.3), which is required in next step (VirtualHost).
I will be honest with you that this is my first shot.
3 – Set your Rails path:
Now I’ll get the boarding pass for Rails application by adding Passenger specific configuration options to the Apache configuration file (Virtual Host).
- Create a new virtual host file from /etc/apache2/sites-available/000-default.conf by making a copy of default file -it has basic file structure and must be used again.
- Open and edit the new config file using nano.
- Replace yourserver.com with your server’s hostname.
- The virtual host document root must point to your application’s public subdirectory.
- Replace /path-to-your-app with your Rails application’s public directory path.
- Allow access to the Rails project directory using per-directory permission.
- MultiViews must be disabled for the Rails project directory.
- Replace /path-to-ruby with the Ruby command that you noted from passenger-config about ruby-command.
- After making necessary changes, enable the newly created virtual host file and restart Apache server.
Run on terminal
- $sudo a2ensite newrailsapp.conf.
- $sudo apache2ctl restart
VirtualHost – 000-default.conf
<VirtualHost*:80>
ServerName newrailsapp.com
#Tell Apache and Passenger where your rails app public directory is
DocumentRoot /path-to-your-app/public
PassengerRuby /path-to-ruby
#Relax Apache security settings
<Directory “/path-to-your-app/public”>
Allow from all
Options -MultiViews
#required for Apache > 2.4
Require all granted
</Directory>
</VirtualHost>
I got bombarded at VirtualHost, what the heck happened everything went smooth until now. Grrr .. my Rails app didn’t show up on the browser.
I was mentally pressing up arrow to get to all previous command that I used earlier, more times(as I do on a terminal), to find what went wrong?
After reading the stack overflow VirtualHost articles I found out, that I made a copy of 000-default.conf file in sites-enabled folder instead of 000-default.conf file in sites-available folder. So when a site is enabled(a2ensite) using the new config file in sites-enabled, a magic link (symlink) didn’t happen between the file in sites-available and sites-enabled. As there is no new created config file (newrailsapp.conf) in sites-available folder (refer here).
Moments learning something new turns your whole world upside down.
