Installing Passenger + Nginx on a Linux/Unix production server
for Ruby apps + generic installation through RubyGems (with RVM)
On this page, we will install Passenger. After installing Passenger we can begin with deploying the app.
Table of contents
Before you begin: a note about Nginx
Before you begin, you should know that installing Passenger in its Nginx integration mode involves extending Nginx with code from Passenger. However, Nginx does not support loadable modules. This means that in order to install Passenger's Nginx integration mode, it is necessary to recompile Nginx from source. And that is exactly what we will do in this installation guide.
The fact that Nginx needs to be recompiled, has some implications:
Many users installed Nginx using their operating system's package manager (yum, apt-get, etc). This Nginx is usually located in /usr/sbin/nginx, with configuration file /etc/nginx/nginx.conf. However, if you install Nginx from source, then you will end up with another Nginx installation (that has Passenger enabled) that is located somewhere else. This other Nginx installation does not look for its /etc/nginx/nginx.conf, but somewhere else.
Some people get confused by having two Nginx installations and end up editing the wrong config file. To avoid confusion, this guide will recommend you to uninstall the OS-installed Nginx before proceeding.
Init scripts, e.g.
service nginx restart, are not provided by the default Nginx source code. Init scripts are actually extensions provided by the operating system's Nginx package. If you install Nginx from source, then you can't use init scripts to control Nginx anymore. Instead, you will use more generic operating system primitives to control Nginx, such as PID files and signals. Don't worry, this installation guide will teach you.
If you do not like having to recompile Nginx then there are several alternatives you can choose:
- Use the Phusion Passenger APT repository (for Debian, Ubuntu) or the YUM repository (RPMs for Red Hat, CentOS) to install Passenger and Nginx. Through our APT/YUM repository, we provide a Passenger-enabled Nginx package that uses the same structure as the Nginx package provided by the OS. This means that you will be able to use the
servicescript, your config files are at places where you expect them, etc.
- Consider using Passenger Standalone in a reverse proxy configuration to Nginx. Passenger Standalone + reverse proxy is fully supported and supports the same features as Passenger's Nginx integration mode.
If you understand this and choose to use Passenger with its Nginx integration mode, then read on. :-)
Step 1: install gem
Install the Passenger gem with:
$ gem install passenger --no-rdoc --no-ri
--no-rdoc --no-ri argument isn't really necessary, but it makes installation faster by skipping generation of API documentation.
gem installabort with a "permission denied" error? Then re-run it with
Since you are using RVM, don't use
sudo! Always use
rvmsudo instead when executing Ruby-related commands! Learn more about rvmsudo at the RVM website.
Step 2 (optional): uninstall the OS-installed Nginx
As explained earlier in the introductory notes, you are about to install a new Nginx installation from source. If you already installed Nginx before, using the operating system's package manager, then we recommend that you uninstall that in order to avoid possible confusion caused by having multiple parallel Nginx installations. Of course, you don't have to remove the OS-installed version as long as you know what you're doing.
Here are some examples of how you can uninstall the OS-installed version:
sudo apt-get purge nginx nginx-full nginx-light nginx-naxsi nginx-common sudo rm -rf /etc/nginx
|Red Hat, CentOS, Fedora, Scientific Linux, Amazon Linux||
sudo yum remove nginx sudo rm -rf /etc/nginx
Step 3: run the Passenger Nginx module installer
Run the Passenger Nginx module installer and follow the on-screen instructions:
Step 4: validate installation
After installation, please validate the install by running
rvmsudo passenger-config validate-install. For example:
$ rvmsudo passenger-config validate-install * Checking whether this Phusion Passenger install is in PATH... ✓ * Checking whether there are no other Phusion Passenger installations... ✓
All checks should pass. If any of the checks do not pass, please follow the suggestions on screen.
Finally, check whether Nginx has started the Passenger core processes. Run
rvmsudo passenger-memory-stats. You should see Nginx processes as well as Passenger processes. For example:
$ rvmsudo passenger-memory-stats Version: 5.0.8 Date : 2015-05-28 08:46:20 +0200 ... ---------- Nginx processes ---------- PID PPID VMSize Private Name ------------------------------------- 12443 4814 60.8 MB 0.2 MB nginx: master process /usr/sbin/nginx 12538 12443 64.9 MB 5.0 MB nginx: worker process ### Processes: 3 ### Total private dirty RSS: 5.56 MB ----- Passenger processes ------ PID VMSize Private Name -------------------------------- 12517 83.2 MB 0.6 MB PassengerAgent watchdog 12520 266.0 MB 3.4 MB PassengerAgent server 12531 149.5 MB 1.4 MB PassengerAgent logger ...
If you do not see any Nginx processes or Passenger processes, then you probably have some kind of installation problem or configuration problem. Please refer to the troubleshooting guide.
As explained earlier in the introductory notes, you can't use an init script like
service nginx restart. This section teaches you how to control and how to work with the Nginx you installed.
Remember that at some point,
passenger-install-nginx-module asked you where to install Nginx to, and asked you for a "prefix", right? The prefix is the directory in which this new Nginx is installed. By default, the installer picks the directory
/opt/nginx. In this section we will assume that you installed to the default prefix directory. If you specified a different prefix, simply substitute /opt/nginx with the actual directory.
Nginx's configuration files are then located in
/opt/nginx/conf. Its log files are located in
You can start Nginx by running:
$ sudo /opt/nginx/sbin/nginx
Shutting down Nginx
You can shut down Nginx by killing its PID with the
kill command. To find out what Nginx's PID is, use the
ps command. For example:
$ ps auxw | grep nginx root 25817 0.0 0.1 43248 1172 ? S Jan01 0:00 nginx: master process /opt/nginx/sbin/nginx www-data 25818 0.0 0.3 44240 3484 ? S Jan01 0:02 nginx: worker process
Here we see that the Nginx master process has PID 25817 (you can ignore the worker process), so we run:
$ sudo kill 25817
Having to perform two steps is a bit cumbersome. Luckily, there is an easier way to do this. By default, Nginx creates a PID file in
/opt/nginx/logs/nginx.pid. This PID file contains Nginx's current PID. So we can use a single command to shutdown Nginx:
$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
Restarting Nginx is the same as shutting down Nginx, and starting it again. For example:
$ sudo kill $(cat /opt/nginx/logs/nginx.pid) $ sudo /opt/nginx/sbin/nginx
I have multiple Ruby versions or gemsets. Does it matter which one I use to install Passenger with?
Not really. Passenger doesn't care which Ruby you used to install it; it can still serve Ruby apps with any Ruby version, as long as you tell Passenger which Ruby interpreter you want to use.
You tell Passenger which Ruby interpreter to use for a specific app, by using the passenger_ruby directive. This can be customized on a per-application basis.
Please also read How having multiple Ruby interpreters affects Passenger. In particular, read the RVM-related caveats.
How do I install Nginx with additional modules?
The installer is interactive. How do I install in an automated, non-interactive manner?
Please refer to Non-interactive, automatic, headless installs or upgrades.
Some of the dependent libraries are installed in non-standard locations. How do I allow the compiler to find them?
Please refer to Customizing the compilation process.
How do I pass additional flags to the compiler?
Please refer to Customizing the compilation process.
Now that you have installed Passenger, you are ready to deploy your Ruby application on the production server!