Use netlify environment variables in PHP Project

Site Name: youthful-noether-361ce3

We have a PHP project where we read a value from a .env file


We try to set the value for VUE_APP_API_BASE_URL by a netlify environment variable instead of writing it into the file.

For example:

VUE_APP_API_BASE_URL=<load this from netlify environment variable instead>

Is this possible?

hi eduard, are you trying to run a PHP project on Netlify? I don’t think that’ll work as you are expecting.

Here is a good summary of what you can and can not do on Netlify you might find helpful:

Hello Perry, I try to set a environment variable from netlify and use it in my project as explained in my question.

@eduard.f There are several ways to set environment variables, see the documentation:

However as @perry mentions, if you’re trying to host a runtime PHP site on Netlify you’ll find that they don’t support that.

If you’re using a PHP based static site generator you should be fine.

Hi @nathanmartin , ok just for my understanding: you can just use these environment variables for the build and you cannot let netlify create a file on the project with the environment variable name and value written into it, so that we can use it in PHP and for example show it?

The environment variable would be able to be accessed by a PHP file during build time.

However the expectation is that after your build concludes, there would be a folder of html/css/js files which is what gets deployed to the CDN.

Netlify won’t deploy a PHP file that can be accessed at runtime.

If you’re looking for runtime functionality, then you need to use Functions, (but they cannot be produced with PHP).


Netlify won’t deploy a PHP file that can be accessed at runtime.

It must not be a PHP file, a simple file without any extension would be enough.
I just need to access the values after the build. So the values simply need to be stored into a file.

@eduard.f I’m a little confused by what you’re doing, but it should work.

If you’re ultimately putting a value into an environment variable then writing it into a file and deploying it, you could cut out the middle man and just write it directly into the file.

@nathanmartin I need to deploy the same project multiple times, the only difference is one variable being different. With this variable we control which of our API’s should get loaded.

For example we have one Production API and one Staging API.

Currently we use different git branches to solve it, e.g. if the branch master is set, then the value for VUE_APP_API_BASE_URL in the file .env changes to and if the branch staging is set then the value changes to

However my supervisor wants to only use one branch so the only possibilty is to set the API Url by a netlify variable. So I just need netlify to write a environment variable value into a file.

I hope you understand the problem now.

Is this possible with netlify?

I’m just confused if the two occurrences will be running on Netlify, or if they’ll be running elsewhere and loading a file from Netlify.

However regardless, if you’re going to be creating two site instances in Netlify, both pointing at the main branch, and then configuring each one with different environment variables, that’s absolutely possible.

I’ve used it previously to produce 5 x different locale versions of a site, all from the same main branch.

I am using netlify to deploy the same project multiple times.

How can I write the environment variable into a file as described?

After reading through tons of documentation I found this:

As a very small example workflow, we could “pass on” the $CONTEXT variable into a file that you can access at browse time, like this, during build:

npm run build && echo $CONTEXT > public/netlify-context.txt

I think this is the way to go

You can do this using any of the languages available at build time, or as that example shows just using the redirection operator to directly write to a file in linux via the build command.

In that instance it’s writing to a folder of public/ so you would want to make sure that your Build configuration sets the Publish Directory correctly, so the file ends up where you want it to.

1 Like

At the end I solved it by replacing my .env file with this:


And set a netlify environment variable “NETLIFY_VAR” from the netlify UI, with my desired value.