SetEnv how to?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
Anonymous's picture

How Do I set Apache's equivalent of SetEnv in Nginx-Drupal.

In the old Apache config, there are some variables defined in the Vhost config eg:

SetEnv db_type mysql
SetEnv db_name mydb
SetEnv db_user root
SetEnv db_pass root
SetEnv db_host localhost
SetEnv db_port 3306

If I go to phpinfo then I can see these variables available in PHP Variables via _SERVER array like _SERVER['db_host'] = localhost and so on.

Now according to surfing the Nginx docs they say to use fastcgi_param. So, my take on its equivalent is:

fastcgi_param db_type "mysql";
fastcgi_param db_name "mydb";
fastcgi_param db_user "root";
fastcgi_param db_passwd "root";
fastcgi_param db_host "localhost";
fastcgi_param db_port "3306";

I placed this under http {}, server{} and so on, but the phpinfo() doesn't show this value.

where shall I put this settings, or if I am doing something wrong then please advise me. It is pretty important to have the variables in server settings as it is required by drupal settings.php page.

Now the Drupal site downloads the index.php page instead of executing it.

Comments

fastcgi_param

akuznecov's picture

fastcgi_param

Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination. These directives are inherited from the previous level if and only if there are no fastcgi_param directives defined on the current level.

I'm using FPM

rajibmp's picture

can you please elaborate a bit, as I am using PHP-FPM with Nginx, and after setting the fastcgi_param what exactly should I do for FPM to recognize it? It'd be very helpful.

basically

emjayess's picture

figure out what fastcgi params are actually being set, then figure out where in the config these live... and put your set of vars right in with those :)

--
matt j. sorenson, g.d.o., d.o.

If else statement

rajibmp's picture

Ok, I figured out where to put those fastcgi_param.

I made a new file called site1_db.conf and put those fastcgi_params there. and from Perusio's config, in @drupal {} block, I used the file as include site1_db.conf

But the problem is I have multiple sites sharing same server. Now, if I want to include more db_configs then how shall I do... can I do something like:

if($host = site1.com) {
  include site1_db.conf;
}
if($host = site2.com) {
  include site2_db.conf;
}
if($host = site3.com) {
  include site3_db.conf;
}

But easily this config can get bit messy and I'm not even sure if that if statement could be used there, will give it a try.

Any other suggestions for elegant ways to do is also highly appreciated.

Nopes

rajibmp's picture

This if statement is allowed in @drupal {} block, but it cannot have include or fastcgi_param as both throws error.

why?

emjayess's picture

I don't need to ask, but curiosity compels me :) why this approach of configuring db connections in nginx, instead of the usual settings.php file for each site, as per Drupal standard approach?

--
matt j. sorenson, g.d.o., d.o.

Aegir

rajibmp's picture

This is due to Aegir.

I am working on a system built with Aegir and Apache for Multisite environment.

I am not sure whose approach was this, the architecture who design this or Aegir who forces these kind of setup.

According to a line I found in the site's settings.php it might be Aegir, but I don't know... here's the standard settings.php files of all the websites under Aegir.

<?php
/
* @file Drupal's settings.php file
*
* This file was automatically generated by Aegir 6.x-2.x
* on Wed, 16 Jul 2014 00:52:06 +0300.
*
* If it is still managed by Aegir, changes to this file may be
* lost. If it is not managed by aegir, you should remove this header
* to avoid further confusion.
*/

if (isset($_SERVER['db_name'])) {
  /

   * The database credentials are stored in the Apache or Nginx vhost config
   * of the associated site with SetEnv (fastcgi_param in Nginx) parameters.
   * They are called here with $_SERVER environment variables to
   * prevent sensitive data from leaking to site administrators
   * with PHP access, that potentially might be of other sites in
   * Drupal's multisite set-up.
   * This is a security measure implemented by the Aegir project.
   */
  $databases['default']['default'] = array(
    'driver' => $_SERVER['db_type'],
    'database' => $_SERVER['db_name'],
    'username' => $_SERVER['db_user'],
    'password' => $_SERVER['db_passwd'],
    'host' => $_SERVER['db_host'],
    'port' => $_SERVER['db_port'],

and it claims it is secure.

I agree, anyone who can view your phpinfo() can see all your database information, at least Apache use to spill it. I changed the servers to Nginx and it doesn't show database info in the phpinfo() function.

I can also think of some

bhosmer's picture

I can also think of some reasons to do this, but not with a Drupal site. Depending on your answer, you might be doing more harm than good doing this.

No Choice

rajibmp's picture

I have No Other choice then doing this, Aegir puts everything in the plate in an arranged table. Sometimes I just have to appreciate the cook and enjoy the meal instead of complaining it is chicken not turkey.

in @drupal location

rajibmp's picture

For those of you who are curious to know or want to give it a try by defining own $_SERVER variables for PHP via Nginx's fastcgi_param then use it under the location @drupal {} and location @drupal-no-args{} in Perusio's configuration.

redefine fastcgi_drupal.conf for each site?...

emjayess's picture

I have not tried this, so I don't know for certain whether it will work. But I think you may have to redefined the entire fastcgi_drupal.conf file for each of your sites, probably as fastcgi_drupal_mysite1.conf and include that in each respective server{} block, or by pasting the entire block of params directly in the server block (along with your db params of course).

Basing this presumption on what @akuznecov pointed out: fastcgi_params inherited unless you've specified some new params at the server level... so, specify all (or none) at the server level.

--
matt j. sorenson, g.d.o., d.o.

Almost Identical

rajibmp's picture

Your approach is almost identical as I did, but instead of having separate fastcgi_drupal.conf what I did was to export the @drupal{} and @drupal-no-args{} to separate files like drupal_site1 , drupal_site2, etc. and placed the fastcgi_param of database config inside the @druapl{} according to the sites name respectively.

Now my modified config is like:

Filename: drupal_site1

location @drupal {
    include drupal/fastcgi_drupal.conf;
    fastcgi_param db_type "mysql";
    fastcgi_param db_name "mydb";
    fastcgi_param db_user "root";
    fastcgi_param db_passwd "root";
    fastcgi_param db_host "localhost";
    fastcgi_param db_port "3306";
    fastcgi_pass phpcgi;
}

and include this file in Vhost config like
Filename: Server1.conf

server {
    listen 80;
    root /var/www/site1;
    index index.php;
    include drupal/drupal.conf;
    include sites/drupal_site1;
}

The included drupal/drupal.conf is the same drupal.conf file from Perusio's config minus @drupal{} which is different as per different site now.