How to include .env file in php. what are the configuration is required. Am hosted in aws ec2 instance and how to access env file in server. can anyone help me.
Assuming you are using Linux EC2, if you can type 'env' command, it should list all the environment variables for you. Now if you want to include any specific environment variable before running your php script and if that variable is not part of your global variables listed in as part of 'env' command, you can always set that using export command. This can be set before running your php program or inside php startup script.
Sample command:
export env_variable_name="value"
For persistent environment variable, these are the common files where we need to specify:
/etc/environment
/etc/profile
example:
export PHP_HOME="/path/to/php/home"
export PATH=$PATH:$PHP_HOME/bin
if you are using Bash, you can declare the variables in the ~/.bashrc:
$ export PATH="$HOME/bin:$PATH"
To load the new environment variables into the current shell session use the source command:
$ source ~/.bashrc
Related
I had made a custom artisan command. In homestead everything goes right, but when I execute the command on server it cant read environment variables. When I ssh the server and try php artisan external:import the connection timeouts cause of null env vars.
On artisan tinker env() function returns null.
How can I read env vars on server?
IMPORTANT: This server is part of AWS, so it does not have .env file.
did you clear config cache already?
php artisan config:clear
i think env function did not work after you cached the config
I know it's a bit late to answer this question, however, I'll share my experience in case it can help someone.
The environment variables will be available depending on how they have been configured. The different ways to configure these variables are:
Setting them for the current user ~/.bash_profile, ~/.bash_login or ~/.profile
Setting them globally with the /etc/profile file or with a file in the /etc/profile.d/ directory
Establishing them at the web server software level. For example, in the httpd.conf file with SetEnv, in apache.
If you have set environment variables through server software, they will be available only to scripts that handle http requests. This leaves out the artisan commands.
My recommendation is that you add a step to your deployment process to set environment variables globally by creating a new file in /etc/profile.d/. For example, /etc/profile.d/webapp.sh. So webapp.sh would have lines like
MY_VAR=my-value
I hope this helps you.
I am trying to set an environment variable on Linux which should be available in shell as well as Apache.
I added the variable to /etc/environment, so the file looks something like this:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
MY_VAR="foo"
To have MY_VAR available in apache, this is what I added in /etc/apache2/envvars:
. /etc/environment
export MY_VAR=$MY_VAR
After doing a lot of research, this is the only way I was able to get the same environment variable work system-wide and in Apache without having to declare them twice.
Are there any security concerns with this approach?
In general, you don't need to use the keyword export when sourcing scripts.
Therefore the extra script (i.e. my_apacheenv.sh) should have this format:
VARIABLE_A="some hardcoded value"
VARIABLE_B="${DYNAMIC_ONE}"
VARIABLE_C="${APACHE_HOSTNAME}"
and be sure it is marked as executable (chmod +x /path/to/my_apacheenv.sh).
The file /etc/apache2/envvars should contain:
. /path/to/my_apacheenv.sh
Then you can define the value of DYNAMIC_ONE or APACHE_HOSTNAME in any usual way, either in the session or as environment variable.
For me, it works pretty well when used in combination of a docker-compose file:
version: "3"
services:
single-apache-php:
build: ./../cicd/apache
environment:
APACHE_HOSTNAME: "my.dynamic.hostname.com"
I was able to solve my own problem. I removed MY_VAR from /etc/environment and created a separate file /etc/profile.d/apache.sh which looks like this:
export MY_VAR="foo"
Then I sourced this file in /etc/apache2/envvars like so:
. /etc/profile.d/apache.sh
Any variable set in apache.sh is available system-wide as well as in Apache
Because apache.sh is a script, unlike /etc/environment, the variables do not have to be "export"ed again in envvars
There is no security risk because no unwanted variables are passed magically to Apache
Whatever variables you want to be available system-wide as well as Apache, you add it to apache.sh and if you want it only in Apache then you add it in envvars
I am developing a php web application using Apache on CentOS6.
I have set a custom environment variable in CentOS on command line by using
export test_var=3
I cant figure out why I am unable to print this Linux shell variable in my php script on my web-application. Apache seems to pull up its own environment variables only and ignores Linux environment variables.
Things I have tried so far:
I have set variable variables_order='EGPCS' in /etc/php.ini.
This has allowed me to access Linux variables using PHP CLI but not in my web-application.
I tried adding line PassEnv test_var in my Apache configuration file (httpd.conf) but Apache returns a blank value in my application.
Passenv works when I add line export test_var=1 in /etc/init.d/httpd but I don't want to use this because I already have this variable declared and set in Linux and so I don't want to set it again.
I have written a shell script and have tried using exec to execute the shell script but when I try to print it using print_var, I get a blank array.
Any suggestions? I don't want to set the variable in any file because I have already set it in in Linux. I'm want to pull up the variable from the OS itself.
The Apache documentation says PassEnv should do the trick:
Specifies one or more native system environment variables to make available as internal environment variables, which are available to Apache >HTTP Server modules as well as propagated to CGI scripts and SSI pages. Values come from the native OS environment of the shell which >invoked the httpd process.
I used to have an offline directory on my server with Perl scripts to dynamically create files.
Say this directory was in an offline directory for security reasons (/server/back/scripts) I used to access it with exec(/server/back/scripts/auto.pl $arguments)
Contents of auto.pl:
system('cp /server/back/includes/default /server/front/ann/'.$enc.'.php');
system('chmod 555 /server/front/ann/'.$enc.'.php');
system("perl -pi -e 's/string/".$key."/g' /server/front/ann/".$enc.".php");
This script copy-paste a default file with garbage values to a public directory, and replace garbage values with something else while setting up the rights we want.
how can I reproduce this on Heroku? - if not possible is there any way to at least reproduce the behavior of this script?
It looks like the goal of this script was to inject keys/credentials into your PHP application by searching/replacing.
Heroku encourages configuration via environment variables, especially keys/credentials.
You should add your keys via the Heroku command line tool:
heroku config:set MY_API_KEY=super-secret-hex-goes-here
... and then pull in the values from the environment on your dyno in your PHP code:
$api_key = getenv('MY_API_KEY');
This will allow you to provision keys/secrets for each running application on Heroku without having to store anything in source code.
Heroku does not expose the File system of the current web dyno. So you can't make any changes to the files to have an impact on the running server.
Files can be changed if you have the file in the code base itself. So, you could commit the change and deploy on Heroku.
If these variables, that you are talking about have to be used system wide then the best way is to use Environment variables as mentioned in Winfield's answer.
You can set and unset Environment variables using command :
heroku config:set VAR_NAME=value [VAR_NAME_2=value.........]
heroku config:unset VAR_NAME [VAR_NAME2.......]
If you need to maintain a file with current values of Environment variables , then you can use this command :
heroku config:pull --overwrite
This will get all the Environment variables currently set on server and store them in .env file.
Then you can update the values in .env file locally itself and update the same on server by command:
heroku config:push
This will replace all current values on server with the values in the .env file
Read more about environmet variables here : https://devcenter.heroku.com/articles/config-vars
I'm implementing MediaWiki with the Oracle Database and therefore I need to add TNS_ADMIN variable to my PHP startup environment.
Q1: How do i add TNS_ADMIN variable to PHP Startup environment?
Q2: Is there a specifc file to edit in order to add the env varibale? If so where can i find it on Linux?
If you are using Apache and the apache2handler SAPI, you can use SetEnv; you can also export the variable from the init script that starts the PHP process (works for all SAPIs). For example, on most (older) Linux-based distros, you'll probably find a file named /etc/init.d/apache2 or /etc/init.d/httpd that is a shell script where you can export variables for that service. Check your OS' equivalent of "man 5 init" for specifics.