Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
I know this is old - but if anyone else stumbles across this, do also check out this comment on the PHP wiki.
There’s a PHP configuration setting (variables_order) that needs to include the character ‘E’ to access environment variables.
Looks like this may be a security feature (stopping php from reading environment variables).
There may be a setting you have to turn on (or off) in php.ini but what I’ve found if you put the enviroment variable in /etc/enviroment php will read it with getenv().
This link may shed some light on it: https://stackoverflow.com/questions/13568191/how-to-get-system-environment-variables-into-php-while-running-cli-apache2hand
You need to ensure the environment variable is accessible to the web server and, by extension, PHP.
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following line to pass the environment variable to PHP:
SetEnv SENDGRID_API_KEY "your_sendgrid_api_key_here"
Restart Apache to apply the changes.
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
env[SENDGRID_API_KEY] = "your_sendgrid_api_key_here"
sudo systemctl restart php7.4-fpm sudo systemctl restart nginx
If PHP is running under a different user (like www-data), it won’t inherit your shell’s environment variables. Ensure that you add the environment variable to the web server’s configuration rather than relying on shell variables.
Once the above changes are made, test if PHP can now access the environment variable.
Create a simple PHP file to test:
<?php
$apiKey = getenv('SENDGRID_API_KEY');
echo $apiKey ? "API Key: $apiKey" : "No API Key found.";
?>
Access this file via your browser to verify if the API key is being printed.
.env Files with PHP (Optional)Alternatively, you can use a .env file and load it into PHP with a library like vlucas/phpdotenv to manage environment variables in your application.
composer require vlucas/phpdotenv
.env file in your project root:SENDGRID_API_KEY=your_sendgrid_api_key_here
.env file:require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$apiKey = getenv('SENDGRID_API_KEY');
echo $apiKey;