Unable to get the environment variable using PHP hosted in amazon linux - php

I set up the environment path in amazon linux vi /etc/environment as ENV_TYPE=DEV. Environment variable are set up in localhost but it not get the environment in web host.
In apache path /var/www/html I create a index.php with below code:
<?php
echo "env ".getenv('ENV_TYPE');
exit;
?>
I already tried the below commands for setup,
vi etc/environment --> ENV_TYPE=DEV
OUTPUT from my host is " **env **" only but expected Output is "env DEV"
the issue is env only execute in this code , ENV_TYPE not fetched from environment variable!!!
output image
i expected output image is

set the Local environment in the path file /etc/environmet
export ENV_TYPE=DEV
then, save and Create a .htaccess file in the apache path /var/www/html and add the line in .htaccess file.
SetEnv ENV_TYPE "DEV"
Allow the .ht file permission in apache conf (httpd.conf) file
Finally, it's automatically taken the Environment Variable as Dev

Related

Could not find .php file in /var/www in apache server

I'm using Vagrant provider for server, now look at my directories :
in my system:
https://i.stack.imgur.com/XCXIm.png
vagrant-site
-Project(/var/www)
--public (/var/www/html)
---index.php
now I created a sample .php file in Project folder then when I run vagrant and logged in vagrant ssh when I cd to /var/www ,that .php file doesn't exist there and I could not work with
include __DIR__ . '/../count.html.php';
this line of code not working because this file doesn't exist in /var/www but exist in my system
That index.php file it's work there is no Problem , but I want to create a php file outside of public directory that , just my self can access that , now when I create that file inside Project folder so now this file must be inside /var/www and when I use include command in my php code , php can not found that , I used file_exists() function and gave the path of php file function , now the function returned false then I check /var/www directory to check that php file exist or not , not exist!
I solved problem,
in vagrant file in Sync folders config section , I found out I can add more directories so I synced : ./Project to /var/www but remember,
something it will change after sync ,this directory : /var/www/html ,
that html folder inside apache2 it will remove and replaced with your public directory name (inside your device ,for me it was public ) public directory folder,
and now you need to change your root directory address inside 000-default.conf file in this Path: /etc/apache2/sites-available/000-default.conf after you change this restart apache2 service
DocumentRoot /var/www/html change this => /var/www/your public directory name
sudo service apache2 restart

Docker configuration virtual domain name is wrong, can not access

Apache and PHP are installed in the docker image. How to configure the virtual domain name of this machine, you can access curl www.test.com. I have configured the hosts file and httpd.conf, but it is still useless. How do I configure it?
You'll have to edit /etc/hosts and add this :
127.0.0.1 www.test.com
By eg you can do this in your dockerfile :
RUN echo "127.0.0.1 www.test.com" >> /etc/hosts
(or check your docker configuration if there is any option to do it)

How to edit Apache Host file using php and add domains into it

I have an dedicated server where i have to create multiple virtual hosts.So for this i have managed to create .conf file in /apache2/sites-available folder using php,but next step is to add that virtual host entry in host file which is at /etc/hosts.
I can do this using Linux command by editing hosts file in nano editor,But i want to do this using php.
I found this command which add entry in host file using terminal which i will execute via shell_exec() programmatically,
sudo echo "192.168.xx.xx example.com" >> /etc/hosts
but this is not working,it throws permission denied error.
So,can i edit host file dynamically using php or can i append a line into it?

Can not access the php file within the htdocs directory

I have the test.php file in this directory C:\xampp\htdocs. I can access the localhost by this link http://localhost:800/xampp/ then it appears the orange XAMPP wlecome window but when I type the following URL http://localhost:800/xampp/test.php I getting something as The required URL could not be found. Why can not I access this file?
Apache ports: 800, 4433
MySQL ports: 3306
test.php
<?php
echo "Hello World!";
?>
If you file is in C:\xampp\htdocs you should access it using http://localhost:800/test.php.
htdocs is the default root directory of apache. (Variable DocumentRoot in conf/httpd.conf)
1) Inside the htdocs directory crate new folder test and add file
test.php into this directory
2) Restart the server
3) Use
http://localhost/test/test.php

Write apache environment variables and read them in php script

I have an index.php file that is running some script making mysql requests. For obvious security reason, i'd like to remove the mysql credentials (host, password, user, database) from this script and replace them with some apache environment variables.
I tried to create such variables in /etc/apache2/envvars using following line of code : export MYSQL_USER='my_user' and then I intend to get it back using getEnv php function like this : getenv('MYSQL_USER') but this returns nothing.
Any idea ? Thanks !
Four steps :
in /etc/apache2/envvars : export MYVAR='value'
in /etc/apache2/apache2.conf : PassEnv MYVAR
Restart apache
in the php file located wherever apache2 is running: echo $_SERVER['MYVAR'];
In the virtual host for Apache you could use
SetEnv VARIABLE_NAME value
and the get the value of this variable from your PHP code using
$variable = getenv('VARIABLE_NAME');
You can read more on SetEnv here http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv
If you would like to store passwords in an environment variable, but you don't want other users to be able to read the password or key, you could follow the following steps (tested on Ubuntu 18.04):
Create a file /etc/apache2/conf-available/aws_key.conf
Change the ownership to root sudo chown root:root /etc/apache2/conf-available/aws_key.conf
Change the file permission, so it is only readable for root sudo chmod 600 /etc/apache2/conf-available/aws_key.conf
Put the password or key in the file, e.g. SetEnv AWS_SECRET_ACCESS_KEY abcdefg12345+-987654321
Enable the configuration file sudo a2enconf aws_key
Restart Apache sudo service apache2 restart
This key is accessible in PHP via $_SERVER['AWS_SECRET_ACCESS_KEY'].

Categories