1

How does one set Environment Variables in MAMP Pro when using Nginx?

Existing questions on Stack Overflow focus on an Apache implementation: Setting Environment Variables in MAMP?

These variables need to be retrievable from PHP using \getenv()

My attempted solution is the following:

//Edit Jan 4: I later realized the path to include for fastcgi_params was incorrect, it has since been fixed to: include /Applications/MAMP/conf/nginx/fastcgi_params; however, the environment variables are still not functioning enter image description here

PHP:

<?php

\var_dump( \getenv('DB_DSN') ); 
//outputs 'bool(false)'

Here is another attempt trying to set the Environment Variable TEST:

enter image description here

PHP:

<?php

\var_dump( \getenv('TEST') ); 
//outputs 'bool(false)'
JimmyBanks
  • 4,178
  • 8
  • 45
  • 72
  • Have you restarted everything? (Both nginx and MAMP?) – bishop Jan 04 '20 at 23:47
  • @bishop Yes of course, many times just to make sure – JimmyBanks Jan 04 '20 at 23:48
  • What does `getenv()` without arguments produce? – bishop Jan 05 '20 at 00:17
  • The PHP server variables, for example: `["SERVER_SOFTWARE"]=> string(12) "nginx/1.13.2"` `["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1"` – JimmyBanks Jan 05 '20 at 00:28
  • You sure the workers running your code are going through that location block? Try removing that block and/or adding env variables to other location blocks. – bishop Jan 05 '20 at 00:35
  • I've included the `fastcgi_param` lines in the `@php` block which is functioning (it directs to the correct PHP file names), but still no environment variables being set – JimmyBanks Jan 05 '20 at 00:50
  • Please update your post with the lateat configuration. – bishop Jan 05 '20 at 01:27
  • The mamp config defines a `TEST` variable, but the code looks for a `DB_DSN` variable. Typo, or source of current problem? – bishop Jan 05 '20 at 22:15
  • Sorry above the image I explained it, the 2nd image attempts to set the variable `TEST`, the php code has been updated appropriately for that variable, I'll update the content to reflect that more clearly – JimmyBanks Jan 05 '20 at 22:25

1 Answers1

0

I had the same problem and could not set environmental variables in nginx as easily as apache.

Actually your first solution does not work because the content "Additional Parameters for directive" is placed after this directive generated by MAMP.

location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_php7.2.1.sock;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          /Applications/MAMP/conf/nginx/fastcgi_params;
        }

Your location directive is then ignored by nginx since it comes after the one generated by MAMP on the same location (.php$).

This is the workaround. MAMP recreates at every server startup the nginx.conf file. This is created using a template. You can actually modify the templates to generate a conf file the suits your needs.

This is the link to the MAMP documentation. https://documentation.mamp.info/en/MAMP-PRO-Mac/Menu/File/

I edited the nginx.cenf template to add the content of the "Additional Parameters for directive" box right before the creation of the location done by MAMP.

  1. Edit the nginx.conf in MAMP. File > Edit Template > nginx
  2. Locate the keyword MAMP_VirtualHost_AdditionalCustom_MAMP and remove it
  3. Place the same directive just in front of this block

        MAMP_VirtualHost_AdditionalCustom_MAMP
    
        MAMP_Nginx_ReverseProxy_IF_MAMP
        # proxy the PHP scripts to Apache
        location ~ \.php$ {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://MAMP_Apache_IP_MAMP:MAMP_Apache_Port_MAMP;
        }
        MAMP_Nginx_ReverseProxy_ELSE_MAMP
        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpHost_MAMP.sock;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          /Applications/MAMP/conf/nginx/fastcgi_params;
        }
        MAMP_Nginx_ReverseProxy_END_MAMP
    

After restart for me it worked. You can fiddle around with a more sophisticated solution.

Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39