Get apache environment variables when executing php from command line - php

I'm trying to execute a PHP script from the command line. In this script I'm accessing the apache2 environment variables but it won't let me. When I call the script from a browser it works. But I need it to work from the command line, because it should run asynchronously.
I access the environment variables via the getenv() function.
I've set the environment variables in the " /etc/apache2/sites-available/000-default.conf" file.

Related

Cpanel Cron Job Php Global Variable

The cron job process works, but it doesn't read global variables like $ _SERVER in php.
Cron Job Code:
/usr/local/bin/ea-php72 -q /home/userName/public_html/folderName/folderName2/phpFile.php
PHP Code:
print_r($_SERVER['DOCUMENT_ROOT']);
How do we get it to read these global variables?
For document_root it's normal. You run PHP in command line, so you not used a webserver so you don't have a document_root.
So PHP can't give you this information. Other entries of $_SERVER was not gived when run PHP in command line.
There is no server, so $_SERVER is not set.
You are running the script directly as cron cron (as opposed to from a web server accessed by an HTTP request triggered by a cronjob ), then of course it doesn't work.

not able to execute nutch crawl command using php exec function

I have to run Nutch crawl commands using php exec but it shows
"0 Error: JAVA_HOME is not set"
The command works fine with terminal. I have tried the below code in crawl.php where apache-nutch-1.15 directory is placed.
exec('apache-nutch-1.15/bin/nutch inject crawl/crawldb urls',$output);
and this gives the above mentioned error.
Thank you in advance for any assistance you can provide.
In order to run Nutch you need the JAVA_HOME environment variable set and pointing to the proper path (where your JVM is installed). This works on your terminal because you have this variable set already. You can check this with:
$ env | grep JAVA
When a new process is started with exec from PHP, this environment variable is not set because it is not a shell, you're only starting a process without any "shell environment". You can use the putenv function to specify some environment variables before calling exec.

Add environment variable to access from php script without reboot?

I need to access an environment variable in my php script. It's running on a remote server. I've added them to /etc/environment but I can't access them as the server needs a reboot.
They can be echoed when I run
source /etc/environment
so it's not a problem with the file.
But the php script isn't running as my user so it doesn't have those variables. Since it's a remote server I don't particularly want to reboot it, as I assume this would then pull in the new environment variables I wrote to /etc/environment.
I've seen I can use /etc/profile but that won't affect the apache user, which I assume is running the php scripts.
I've tried to run
echo exec('source /env/environment && echo $the_var');
but the variable still doesn't return.
Any ideas please?
Thanks in advance

Running complicated ROS shell command through php

I'm trying to run a ROS shell program on the server through php on Ubuntu 14.04. I have tried using system, exec, shell_exec but nothing happens and I don't get any output. The system call is the following:
echo shell_exec("/opt/ros/indigo/bin/rosrun gazebo_ros spawn_model -database Part_A -gazebo -model Part_A");
What are the limitations of using system or exec to run any shell command through php on a server?
I don't care as much about the output of the command as for its execution. I think the problem has to do with the fact that PHP doesn't have any PATH like shell does so it can't find any applications without specifying the exact location. How can I make PHP se the same PATH shell does?
The problem was that the apache user and the environment in which the bash commands are running are not set up correctly. I followed the instructions from this answer but instead of using "source" I used "." and instead of using the source.bash file I used the source.sh file. I also set all the environment variables that had to do with ros or gazebo using the putenv() function.

Access Apache Environment in PHP script (CLI)

I currently have a script which will run via the browser and separately via the "php" command line tool.
Now the problem is, I am able to access the Apache Environment variables while script being called from the browser, but unable to access it while the script is called via command line.
How can I get the Apache environment available to the command line script as well ?
First, you must declare your environment variable using this command:
export APPLICATION_ENV=development
Then you can get it in your application cli with the function getenv('APPLICATION_ENV');.

Categories