Using environment variables in PHP and on fortrabbitENV vars help to create and shape the environment of where the code runs. It's a good modern practice. Show
ProblemYou most likely run at least two environments of your App: A local one for development and one here on fortrabbit for production. Both instances probably have access to a database. Your local MySQL has of course different credentials than the remote one. Your config file, storing this information, is under Git version control. So how to deal with different environment-specific configurations? Also, how to work in a team when everyone has it's own local settings? How to separate code from configuration, so that the code is portable? SolutionEverything specific to the environment should be stored in Environment variables or short "ENV vars". About ENV varsAn ENV var is a key value pair, like so:
This is a basic example on how you can do that locally. In most cases you will not need to touch that level here. Please read on for further usage and how to do it on fortrabbit. ENV vars in modern PHPYou can access ENV vars from PHP. And it is a commonly wide spread best practice to do that. The .env fileDealing with server settings is not convenient for many developers. So the Ruby community figured out the PHP dotenvThe
Modern PHP frameworks — Laravel, Symfony …) and CMS (Craft) use one or the other under the hood. ENV vars on fortrabbitRecap: ENV vars are environment
specific. So, in consequence, the Now, how should your fortrabbit App know about it's ENV vars? Some users think they need to set up an additional Recap: The PHP application itself will just query the ENV vars. A library just helps to populate the ENV vars into the code base, but only when they are not set already. The fortrabbit Software Preset is where the magic happens. While creating an App on fortrabbit, you'll choose your desired CMS or framework. This selection will configure the server ENV vars in ways, the software can work with it. For example, for Laravel and Craft, the ENV var
So, most likely, your fortrabbit App will work out of the box. As a bonus you even reset the database password without touching any configurations. Adding and editing ENV vars on fortrabbitYou can add ENV vars of your App in the Dashboard > Your App > Settings > ENV Vars. The input supports the dotenv file format and allows you to create or update multiple variables at once. The changes will be distributed after you save the page. It may take around 60 seconds, a re-deploy is not necessary. Some frameworks and CMS might cache the ENV vars, like Laravel, see here. Accessing ENV vars from raw PHPYou can access
your ENV vars from PHP either using the global variable
Advanced topicsSo far we have covered the basics read on to learn to dive deeper into ENV vars and how they can help you. ENV var types on fortrabbitThere are four different kinds of ENV vars here on fortrabbit which are available to your App at runtime. Custom ENV varsThose are the ones you add yourself in the Dashboard. App ENV varsGeneric ENV vars cannot be overwritten by you. They are always available.
Software Preset ENV varsDepending on what you have selected in the Software Preset when creating your App, additional ENV vars will be seeded for you. For example: When choosing Laravel the ENV var Dynamic ENV varsDynamic ENV vars become available if you have enabled "Dynamic ENV vars" in the Dashboard, which is the default for new Apps. They contain access details for services offered by fortrabbit. You can find them in the Dashboard > {{app-name}} > ENV vars on the right hand side. They will never overwrite existing, manually created ENV vars. This means: if you manually create an ENV var, we guarantee that we won't replace it's value by a dynamically generated ENV var. Following a list of available dynamic ENV vars: MySQL (All Universal Apps or Professional App Component)
Object Storage (Professional App Component)
Memcache (Professional App Component)
Nested ENV varsYou can use simple, nested variables in your custom ENV vars. Simple means, that you can set variables which reference other variables, which contain a value, for example:
We do not support multiple levels of interpolation, which means that you cannot use variables, which reference other variables, which again reference other variables, for example:
ENV vars vs securityStoring credentials (passwords, secrets, ..) in environment variables is not without risk. They can be exposed, due to programming errors or oversights, for example when you forget to remove the ENV var validationStrict validation rules for ENV vars are in use in the Dashboard while entering. Chars like the "$" sign can be harmful in Linux systems. Here is the regex we use to validate the ENV var input in the Dashboard:
So sometimes, when you want to store an external API key as an ENV var, you might get a validation error like: "Variable value contains invalid characters". If you can not change the value of your ENV var, you can encode and decode those: Encode and decode ENV varsUse a base64 encoded string and decode it when applying it to your configuration in your code. Encoding works like this:
And this example should give you an idea how you can do the decoding. Reserved environment variable names
What are the variables used in PHP?PHP Variables. A variable starts with the $ sign, followed by the name of the variable.. A variable name must start with a letter or the underscore character.. A variable name cannot start with a number.. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). What are the types of environment variables?There are two types of environment variables: user environment variables (set for each user) and system environment variables (set for everyone). By default, a child process inherits the environment variables of its parent process.
Where are PHP environment variables stored?This means the environment variables must be defined in a PHP-FPM configuration file, typically stored in /usr/local/etc/php-fpm. d/ . So, in order to store your environment variables in NGINX and PHP-FPM, it's a good idea to create a new file at /usr/local/etc/php-fpm. d/env-vars.
What are environment variables example?Examples of environment variables include: PATH : a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that leads to the command.
|