I'm wondering how to add environment variables that will be available in my WP app to use with
getenv( 'MY_ENV_VAR' );
I was reading this but that doesn't seem ideal (especially if I'll have duplicates on every provision) plus I think it's targeting apache, and VVV is running on nginx.
Can they be added in the vvv-custom.yml?
Also one thing I noticed in the answer above is that the env variables added that way, are just added in the .bashrc. I'd like to add them to PHP_FPM, which probably means that I should change the php-fpm.conf file.
I also tried this way, and manually added
; Env variables
clear_env = no
env[TEST] = 'test env var'
to my /etc/php/7.2/fpm/php-fpm.conf and I restarted the php-fpm with sudo service php7.2-fpm restart but no luck.
Any help is appreciated.
So I've added in Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
# Set environment variables...
echo "export TEST_ENV=development" >> /etc/environment
SHELL
Which works when I do printenv, but not when I do getenv('TEST_ENV').
My guess is that the /etc/php/7.2/fpm/php-fpm.conf gets rewritten on provision and I'm losing the clear_env = no which makes it unavailable for getenv().
EDIT:
Weird, I added the
; Env variables
clear_env = no
env[TEST] = 'test env var'
To my php-fpm.conf and the getenv('TEST') works.
Now I need to figure out how to add this during provision.
Does putenv not work for you? PHP conf files get replaced on every provision by design, as they're managed by Utilities
Otherwise, Nginx is the route you should take, e.g.
https://stackoverflow.com/questions/8098927/nginx-variables-similar-to-setenv-in-apache#8331663
I'm going to close this for now though as it's a general Nginx question, nothing that needs VVV specific knowledge
You mean put the variables in the vvv-nginx.conf via location parameters?
Yeah:
fastcgi_param TEST test;
Just be mindful you'll need to fork custom site template to do it, ideally you'd have the provisioner insert them and read from vvv-custom.yml so it was generic and could be backported
I'm getting errors when provisioning
default: nginx: [emerg] invalid variable name in /etc/nginx/nginx.conf:126
default: nginx: configuration file /etc/nginx/nginx.conf test failed
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
I'm not using custom site template (old project) so I added in the vvv-nginx.conf
server {
listen 80;
listen 443 ssl;
server_name test-site.test;
root {vvv_path_to_site}/public_html;
error_log {vvv_path_to_site}/log/error.log;
access_log {vvv_path_to_site}/log/access.log;
set $upstream {upstream};
include /etc/nginx/nginx-wp-common.conf;
location ~ \.php$ {
fastcgi_param APP_ENV 'production';
... bunch of others
}
}
hmmm if you didn't already have a vvv-nginx.conf file your old project should not have been able to load, it looks like you've made a mistake or a typo somewhere, and the config is no longer valid Nginx config syntax. I recommend the standard Nginx linting tactics such as nginx -t
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.