Answers: <p>Root cause: environment variables from <code>/etc/environment</code> are not passed to Passenger. The standard method seems to be <a href="https://stackoverflow.com/questions/10080656/where-to-put-environment-variables-when-using-nginx-and-passenger-on-ubuntu/10080827">passenger_env_var</a> in your nginx config as suggested by user <strong>joshaidan</strong>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-31T00:48:37.897Z
Answers: 2019-12-31T00:48:37.897Z
Answers: afarley
Body: <p>I'm writing Ansible playbooks to provision an Ubuntu 18.04 server.</p>
  
  <p>I want to provision the server for a Rails application using Passenger and Nginx. I used <a href="https://galaxy.ansible.com/geerlingguy/passenger" rel="nofollow noreferrer">geerlingguy.passenger</a> from Ansible Galaxy to configure Passenger and Nginx. </p>
  
  <p>In addition, I used <a href="https://galaxy.ansible.com/zzet/rbenv" rel="nofollow noreferrer">zzet.rbenv</a> to install ruby 2.5.3.</p>
  
  <p>The Ansible playbooks seem to have executed correctly, and when I log into the server via SSH, everything seems to be working (rbenv is on the path, <code>RAILS_MASTER_KEY</code> environment variable is defined).</p>
  
  <p>Deployment of my application via Capistrano also appears to run successfully.</p>
  
  <p>However, when I attempt to visit the landing page, I get a Passenger error: "Web application could not be started by the Phusion Passenger application server."</p>
  
  <p>Looking in <code>/var/log/nginx/error.log</code>, I see:</p>
  
  <pre><code>Missing encryption key to decrypt file with. Ask your team for your master key and write it to /var/www/releases/20191230212823/config/master.key or put it in the ENV['RAILS_MASTER_KEY'].
  </code></pre>
  
  <p>I'm not sure why the above error is happening, because I've already configured a persistent environment variable using the <a href="https://galaxy.ansible.com/weareinteractive/environment" rel="nofollow noreferrer">weareinteractive.environment</a> role.</p>
  
  <p>Inspecting /etc/environment confirms that RAILS_MASTER_KEY is defined, and I can see if it I log in manually as my 'deploy' user. However, <code>/etc/nginx/nginx.conf</code> shows that the default user is <code>www-data</code>:</p>
  
  <pre><code>user www-data;
  </code></pre>
  
  <p>I think this should be fine, because the environment variables are configured in a way that is not associated with a user account. So, why does Rails fail to see this environment variable even though I see it when I log in via SSH?</p>
  
  <p>I'm able to execute the following command on the server and see the contents of the encrypted credentials file:</p>
  
  <pre><code>RAILS_ENV=production EDITOR=vim bundle exec rails credentials:edit
  </code></pre>
  
  <p>So what is going wrong when Nginx/Passenger launch Rails? </p>
  
  <p>Edit: It seems like Passenger/Nginx do not load environment variables from /etc/environment, which mostly explains my problem. I'm trying to figure out the appropriate way to configure environment variables for Passenger using Ansible now.</p>
  
Tags: <ansible><nginx><environment-variables><ruby-on-rails>
Title: Why is Rails (with Nginx/Passenger via Ansible) unable to see my RAILS_MASTER_KEY environment variable?
UserDisplayName: afarley
ThreadText: # Why is Rails (with Nginx/Passenger via Ansible) unable to see my RAILS_MASTER_KEY environment variable?
  
  (Asked by: username_0 on 2019-12-30)
  
  I'm writing Ansible playbooks to provision an Ubuntu 18.04 server.
  
  I want to provision the server for a Rails application using Passenger and Nginx. I used [geerlingguy.passenger](https://galaxy.ansible.com/geerlingguy/passenger) from Ansible Galaxy to configure Passenger and Nginx.
  
  In addition, I used [zzet.rbenv](https://galaxy.ansible.com/zzet/rbenv) to install ruby 2.5.3.
  
  The Ansible playbooks seem to have executed correctly, and when I log into the server via SSH, everything seems to be working (rbenv is on the path, `RAILS_MASTER_KEY` environment variable is defined).
  
  Deployment of my application via Capistrano also appears to run successfully.
  
  However, when I attempt to visit the landing page, I get a Passenger error: "Web application could not be started by the Phusion Passenger application server."
  
  Looking in `/var/log/nginx/error.log`, I see:
  
  ```
  Missing encryption key to decrypt file with. Ask your team for your master key and write it to /var/www/releases/20191230212823/config/master.key or put it in the ENV['RAILS_MASTER_KEY'].
  
  ```
  
  I'm not sure why the above error is happening, because I've already configured a persistent environment variable using the [weareinteractive.environment](https://galaxy.ansible.com/weareinteractive/environment) role.
  
  Inspecting /etc/environment confirms that RAILS\_MASTER\_KEY is defined, and I can see if it I log in manually as my 'deploy' user. However, `/etc/nginx/nginx.conf` shows that the default user is `www-data`:
  
  ```
  user www-data;
  
  ```
  
  I think this should be fine, because the environment variables are configured in a way that is not associated with a user account. So, why does Rails fail to see this environment variable even though I see it when I log in via SSH?
  
  I'm able to execute the following command on the server and see the contents of the encrypted credentials file:
  
  ```
  RAILS_ENV=production EDITOR=vim bundle exec rails credentials:edit
  
  ```
  
  So what is going wrong when Nginx/Passenger launch Rails?
  
  Edit: It seems like Passenger/Nginx do not load environment variables from /etc/environment, which mostly explains my problem. I'm trying to figure out the appropriate way to configure environment variables for Passenger using Ansible now.
  
  **Tags:** ansible, nginx, environment-variables, ruby-on-rails
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-12-31. Score: 0 | Accepted answer
  
  Root cause: environment variables from `/etc/environment` are not passed to Passenger. The standard method seems to be [passenger\_env\_var](https://stackoverflow.com/questions/10080656/where-to-put-environment-variables-when-using-nginx-and-passenger-on-ubuntu/10080827) in your nginx config as suggested by user **joshaidan**.
  
  
  ---
  
  