What is the best way to use .env (dotenv) in php/apache

I use apache/php as my backend. When using server connect I want to set variables that change in dev, testing and production. Typically I would use .env for these but I can’t figure out how to do that in Wappler for a php/apache environment.

I have seached and found a post that talks about dotenv and node but this won’t work for php I believe:

Any advice or other posts that talk about how to do this with PHP?

Here is the solution that I am hacking together. Any alternatives or improvements I should make?

To to this I used a custom module and I also used PHP dotenv by Vance Lucas and Graham Campbell.


  1. I have an issue with the location of the extensions folder mentioned below if anyone has a solution.
  2. Variables are not available in $_ENV.variablename in the Wappler Server action - you have to access them with the name of the server action.
  3. I had to play around with paths to the vendor and .env file locations until I got them right but they look ugly!

Extensions Issue

I am going to show my entire project folder because I have an issue that I can’t figure out - I suspect that it is related to the fact that my site root is set to ‘/html’. It also might be relevant that my links are relative to the site root. You will see that I had to create two ‘extensions’ directories - one in the root and one in html. Both contain identical copies of the same files.

The issue is that my custom action only showed up in the server actions panel if I have the hjson file in the extensions directory located in the root directory. However, the function only works if I have the php file located in the extensions directory located in the web root directory. :confused:

Build the Custom Module

Create your .env file - mine is in root. I added the following variables:

I created loaddotenv.php with the following code:


  namespace modules;

  use \lib\core\Module;

  require_once __DIR__ . '/../../../../vendor/autoload.php';
  use Dotenv;

  class loaddotenv extends Module
    public function load($options, $name) {
      $dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '../../../../.env');

      return $this->app->parseObject($dotenv->load());


Then I created loaddotenv.hjson with the following:

    type: 'dotenv',
    module : 'loaddotenv',
    action : 'load',
    groupTitle : 'Custom Actions',
    groupIcon : 'fas fa-lg fa-database comp-data',
    title : 'Load dotenv',
    icon : 'fab fa-lg fa-hive comp-data',
    dataScheme: [
        {name: 'API_KEY', type: 'text'},
        {name: 'API_SERVER_URL', type: 'text'}
    dataPickObject: true,
    properties : [{
        group: 'Dotenv Properties',
        variables: [{
            name: 'actionName', optionName: 'name', title: 'Name', 
            type: 'text', required: true, defaultValue: ''
            name: 'output', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false

After this step I had to reload Wappler to be able to see my Custom Module in the Server Actions Panel.

wappler dotenv1

Once I added the server action I can access the variables I need from my .env file

1 Like

Great work! Maybe we should have something like this integrated @patrick

Any ideas why I have to put the hjson & php files in different directories?

The hjson has to go in /extensions/server_connect/modules
The php has to go in /html/extensions/server_connect/modules