Steven 24.12.2021
This article details how to setup Nginx and Passenger to run a Rails app in production environment on a Ubuntu machine. It uses specifically:
First of all, understand why you’re using the combination of Nginx and Passenger: Nginx web server is used as the web server, Passenger is the application server allowing the Rails app to speak HTTP.
Nginx and Apache are web servers. They provide HTTP transaction handling and serve static files. However, they are not Ruby application servers and cannot run Ruby applications directly. That is why Nginx and Apache are used in combination with an application server, such as Passenger.
https://www.phusionpassenger.com/docs/tutorials/fundamental_concepts/ruby/#nginx-and-nginx
#/etc/nginx/sites-available/default
server{
# Nginx uses the 'server_name' attribute to decide which server should process the request. In case no match is found, 'default server' is used
listen 80 default_server
server_name example.com
# tell Nginx what root folder to use. Make sure to use the public/ folder inside the Rails app folder
root /path/to/rails/app/public
# tell Nginx to enable passenger with this server
passenger_enabled on
}
nginx first decides which server should process the request.
Let’s start with a simple configuration
where all three virtual servers listen on port *:80:
http://nginx.org/en/docs/http/request_processing.html
#/etc/nginx/conf.d/mod-http-passenger.conf
# passenger root and ruby commands. Get these through 'passenger-config --ruby-command
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /path/to/ruby/version;
passenger_default_user <your-local-user>;
passenger_default_group <your-local-user-group>;
Make sure to configure passenger to use the local user account that is owner of the rails app folder. Not only is running the rails app with a local user a good security practice, instruction passenger to use this account also ensures that all local variables (e.g. definitions in .bashrc) are sources when launching the Rails app.
Passenger enables user account sandboxing (also known as user switching) by default. This configuration option allows you to specify the user that applications must run as, if user switching fails or is disabled.
https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_default_user
Store credentials for your Rails app in credentials.yml. Read them out in configuration .yml and .rb files. Never share your master.key.
Comment