Question

Darshan Hiranandani : How do I configure environment variables for a PHP application on App Platform?

Hi everyone! I’m Darshan Hiranandani, working on deploying a PHP application to App Platform and need some help with configuring environment variables. Can anyone share their experience or best practices for setting up environment variables in this environment? I’m looking for tips on how to manage and secure these variables effectively.

Regards Darshan Hiranandani


Submit an answer


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!

Sign In or Sign Up to Answer

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.

KFSys
Site Moderator
Site Moderator badge
August 6, 2024

Heya @darshanhiranandani,

I’ll recommend you to check the following docs :

https://docs.digitalocean.com/products/app-platform/how-to/use-environment-variables/

It’s exactly what you are looking for

Bobby Iliev
Site Moderator
Site Moderator badge
August 7, 2024

Hi Darshan,

Deploying a PHP application on DigitalOcean’s App Platform and configuring environment variables is straightforward!

Setting Up Environment Variables

  1. Define Variables in App Platform:

    • Navigate to your app in the App Platform dashboard.
    • Go to the “Settings” tab and find the “Environment Variables” section.
    • Click on “Edit” to add new variables. Provide the key and value for each variable.
  2. Use Environment Variables in PHP:

    • In your PHP code, you can access these variables using getenv() or $_ENV.
    $dbHost = getenv('DB_HOST');
    $dbUser = getenv('DB_USER');
    $dbPass = getenv('DB_PASS');
    

    Feel free to share more information about your PHP app setup and I can give you more details on this!

Some things that you should always consider are:

  1. Sensitive Data Handling:

    • Never hardcode sensitive information such as database credentials, API keys, or secrets in your source code.
    • Use environment variables to store this information securely.
  2. Use a .env File Locally:

    • During local development, use a .env file to store environment variables.
    • Use a package like vlucas/phpdotenv to load variables from the .env file into your PHP environment.
    require 'vendor/autoload.php';
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->load();
    
  3. Version Control Exclusions:

    • Make sure that your .env file is included in your .gitignore file to prevent it from being committed to version control.
    # .gitignore
    .env
    

For more detailed instructions, you can refer to the official DigitalOcean documentation.

Hope this helps, and happy deploying!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.