PHP cli command line safe_mode restriction - php

I'm using the Kohana framework (3.0.9), which generates daily logs. I want to mail the log file if one was made the day before CRON runs the script, but after days trying I can't figure out how to put off safe_mode in PHP CLI modus.
When I'm running my script on the web, there is no problem. But I want to run the script as a CRON task on my Plesk 9.5.2 server (or on the command line as root user) I'm getting the following error:
ErrorException [ 2 ]: dir(): SAFE MODE Restriction in effect. The script whose uid is 10001 is not allowed to access /var/www/vhosts/mydomain.com/subdomains/mysubdomain/httpdocs/application/logs/2011/01 owned by uid 48 ~ APPPATH/classes/controller/ajax.php [ 181 ]
I've allready put SAFE MODE off in my Plesk control panel, which works fine for the web request, but not in on the command line or as an CRON task.
I'm using the following code to test if its working:
$d = dir(APPPATH.'logs/2011/01/');
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
echo $entry."\n";
}
$d->close();
I can read the directory APPPATH.'logs/', and also the directory APPPATH.'logs/2011', but the directory's representing each month with the daily log files always give an error.

At nowadays you can schedule php script execution from UI like this:
In case you still need execute script via command line pay attention that Plesk's PHP binaries are placed in:
# 7.0
/opt/plesk/php/7.0/bin/php
# 5.6
/opt/plesk/php/5.6/bin/php
# 5.5
/opt/plesk/php/5.5/bin/php
# and so on
Original answer:
I know this is a few months old, but for the next person that comes across a problem while using Plesk and cron and PHP, here's the answer.
While Plesk does run cron as ROOT, it also runs PHP by default with safe mode ON, which means that when you setup a cron in Plesk that needs PHP, it's going to have restrictions that you do not experience from the shell or from the web.
So what you do is use the CLI /etc/php.ini option override, like so:
/usr/bin/php -q -d safe_mode=Off /var/www/vhosts/path-to-your-php-file.php

Have you disabled safe_mode in the php.ini for CLI?
You can find the location of this file by running the following command php --ini. Search for safe_mode in this file and change the line to safe_mode = Off.

Related

PHP, system() and inheriting PATH to script

Php pages on /var/www/mypage say system("foo.sh"). foo.sh is located on /one/dir and contains command bar.sh that is in /other/dir. For a user using ssh connection and having /one/dir and /other/dir in PATH everything works.
Now, if I put
<Directory /var/www/mypage>
SetEnv PATH . . . :/one/dir:/other/dir
to Apache config, then print getenv("PATH"); on /var/www/mypage/mytest.php shows that /one/dir and /other/dir are in the path. But system() still does not found foo.sh. What's wrong? And after this also foo.sh should see the same PATH so that it will find bar.sh.
I am setting this on Ubuntu 20.04, PHP 7.4.3.
Most likely because just executing > foo.sh would show command not found: foo.sh.
You should specify full/relative path: system('./foo.sh');

Commands working fine in putty but not using exec() in PHP

So I have installed shhavel's facerec (https://github.com/shhavel/facerec) on my VPS (Ubuntu 14.04.5) and it works perfectly fine when I use it from within putty:
facerec list
jenniferlawrence
emmawatson
However, from within PHP when using the exec() function, the only command that works is facerec -help, all other commands produce an error similar to the following:
facerec list 2>&1
Traceback (most recent call last):
File "/usr/local/bin/facerec", line 359, in
sys.exit(__main__())
File "/usr/local/bin/facerec", line 356, in __main__
return args.func(args)
File "/usr/local/bin/facerec", line 159, in list
for subject in os.listdir(subjects_directory):
OSError: [Errno 2] No such file or directory: './subjects'
The PHP script:
exec('facerec list 2>&1', $output);
foreach($output as $string) {
echo $string."<br>";
}
How do I approach fixing this problem?
Found the problem:
The "subjects"-folder was located in /root/ which the PHP script did not have the permission to access
Possible solutions:
change open_basedir in PHP settings to none (See here: open_basedir restriction in effect. File(/) is not within the allowed path(s):)
install shhavel/facerec as the right user (not as root)
move the subjects-folder to a different location and change the facerec python script accordingly

No input file error from dev_appserver.py

I am trying to create a helloworld GAE projects to use PHP to connect to a Google Cloud SQL database. I am following the steps found at:
https://developers.google.com/appengine/docs/php/gettingstarted/helloworld
However, whenever I start the dev_appserver.py to test my helloworld.php I eventually get a permission denied error. I am running Cygwin (mintty) on a Win8 (64) machine. I have run chmod -R 777 * on a number of related directories like:
/usr/bin/
c:/Development/appengine-php-sdk-1.8.0/
c:/Development/php/
c:/Development/Php Scripts/helloworld/
Error:
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "/cygdrive/c/Development/appengine-php-sdk-1.8.0/google_appengine/google/appengine/tools/devappserver2/server.py", line 1232, in _loop_adjusting_instances
self._adjust_instances()
File "/cygdrive/c/Development/appengine-php-sdk-1.8.0/google_appengine/google/appengine/tools/devappserver2/server.py", line 1209, in _adjust_instances
self._add_instance(permit_warmup=True)
File "/cygdrive/c/Development/appengine-php-sdk-1.8.0/google_appengine/google/appengine/tools/devappserver2/server.py", line 1087, in _add_instance
expect_ready_request=perform_warmup)
File "/cygdrive/c/Development/appengine-php-sdk-1.8.0/google_appengine/google/appengine/tools/devappserver2/php_runtime.py", line 195, in new_instance
self._check_environment(php_executable_path)
File "/cygdrive/c/Development/appengine-php-sdk-1.8.0/google_appengine/google/appengine/tools/devappserver2/php_runtime.py", line 138, in _check_environment
env={})
File "/cygdrive/c/Development/appengine-php-sdk-1.8.0/google_appengine/google/appengine/tools/devappserver2/safe_subprocess.py", line 61, in start_process
stdin=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1248, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
How can I determine what file the permission issue is with?
EDIT
There is no traceback from this new error (now that I am calling the actual exe):
WARNING 2013-06-04 02:21:13,243 api_server.py:329] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO 2013-06-04 02:21:13,252 api_server.py:153] Starting API server at: http://localhost:51615
INFO 2013-06-04 02:21:13,261 dispatcher.py:164] Starting server "default" running at: http://localhost:8080
INFO 2013-06-04 02:21:13,324 admin_server.py:117] Starting admin server at: http://localhost:8000
ERROR 2013-06-04 02:21:13,553 php_runtime.py:199] The PHP runtime is not available because: No input file specified.
Then when I go to localhost:8080 I get this:
`The PHP interpreter specified with the --php_executable_path flag ("/cygdrive/c/Development/php/php-cgi.exe") is not compatible with the App Engine PHP development environment.
No input file specified.`
The problem is in your command line:
appengine-php-sdk-1.8.0/google_appengine/dev_appserver.py --php_executable_path=/cygdrive/c/Development/php/ /cygdrive/c/Development/PhpScripts/helloworld/
Your php_executable_path is not an executable, but a directory. So, GAE tries to run /cygdrive/c/Development/php/ as if it were a program, and gets an EACCES ("permission denied"), exactly as it should.
The fix is to pass /cygdrive/c/Development/php/php.exe (or whatever it's called) instead.
I believe your new problem is exactly what I mentioned in an early comment, and in the earlier version of this answer: You're mixing cygwin and native code in ways that don't work.
In particular, it looks like you're running a native Windows version of GAE in a cygwin Python interpreter, and then pointing it at a native Windows php-cgi interpreter. It's hard to guess exactly which one of these multiple mismatches is to blame, but it's quite likely that one of them is. So, either:
Use a native Windows Python, or
Use a cygwin/POSIX GAE and a cygwin PHP interpreter.

Apache PHP script to Start Tomcat Server in Linux

I made a php script to start Tomcat Server (catalina.sh start) thats works well from command line in Linux. It was run as ROOT with a different set of ENV.
However, I would like to do the same from the Web, Please note due to security issues, this script would only be accesible via localhost (for administration only), so we ironed out the security issue.
Problem is, Tomcat is not starting (we could confirm that from the logs, nothing was initiated). We coded lots of Echo around catalina.sh and its providing the right echo output.
Im sure theres an easier way to do this. Any Ideas?
The PHP script does the following:
Changes Dir to th Tomcat Bin
Starts Catalina.sh Script vis startup.sh
OUTPUT FROM COMMAND LINE PRINT ENV:
ORBIT_SOCKETDIR=/tmp/orbit-root
HOSTNAME=localhost.localdomain
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
XDG_SESSION_COOKIE=c111d8f81ff89709b78252c50000000d-1338310776.433586-1797694621
GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
WINDOWID=73400323
QTDIR=/usr/lib/qt-3.3
QTINC=/usr/lib/qt-3.3/include
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
SSH_AUTH_SOCK=/tmp/keyring-bsFAQz/socket.ssh
GNOME_KEYRING_SOCKET=/tmp/keyring-bsFAQz/socket
USERNAME=root
SESSION_MANAGER=local/unix:#/tmp/.ICE-unix/1788,unix/unix:/tmp/.ICE-unix/1788
PATH=/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
MAIL=/var/spool/mail/root
DESKTOP_SESSION=gnome
PWD=/programs/tomcat/bin
GDM_KEYBOARD_LAYOUT=us
GNOME_KEYRING_PID=1779
LANG=en_US.UTF-8
GDM_LANG=en_US.UTF-8
GDMSESSION=gnome
HISTCONTROL=ignoredups
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SHLVL=3
HOME=/root
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LOGNAME=root
QTLIB=/usr/lib/qt-3.3/lib
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-9HtQtRhCr9,guid=e1755bf07ea8a916f0a4e3f70000001c
LESSOPEN=|/usr/bin/lesspipe.sh %s
WINDOWPATH=1
DISPLAY=:0.0
G_BROKEN_FILENAMES=1
COLORTERM=gnome-terminal
XAUTHORITY=/var/run/gdm/auth-for-root-IT2DiY/database
_=/usr/bin/printenv
OUTPUT FROM APACHE PRINT ENV:
TERM=xterm
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PWD=/programs/tomcat/bin
LANG=C
SHLVL=4
_=/usr/bin/printenv
try using exec()
exec('full path to your script')

PHP Extension (memcache|memcached) not showing in phpinfo() but showing in php -m and php -i?

I am getting both modules listed as installed / configured when I use:
php -m
or if I use:
php -i
but when I use:
$m = new Memcache;
// or
$m = new Memcache();
// or
$m = new Memcached();
//or
$m = new Memcached;
I get the following error:
Fatal error: Class 'Memcached' not found
I am running on a Mac - OS X (10.5.7) with default install of apache & php. Additionally, I have memcached running as a daemon on 127.0.0.1:11211 and libmemcache as required by the php-memcached library. I have restarted apache tons of times and even done a machine restart.
Does anyone know why the modules/extensions show up in the command line but not in my phpinfo()? I am literally stumped, after 3 hours of googling, I am just about ready to give up.
Also, please note, my phpinfo() outputs my ini files as follows AND they are both the exact same file:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /private/etc/php.ini
UPDATE:
Apache is failing to load the extension.
[Fri May 14 04:22:26 2010] [warn]
Init: Session Cache is not configured
[hint: SSLSessionCache] PHP Warning:
PHP Startup: Unable to load dynamic
library
'/usr/lib/php/extensions/no-debug-non-zts-20060613/memcached.so'
- (null) in Unknown on line 0 PHP Warning: PHP Startup: Unable to load
dynamic library
'/usr/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
- (null) in Unknown on line 0
Does anyone know why or how this would happen? Both of the files referenced above DEFINITELY ARE there. Should I move this question to server fault?
Your webserver is probably using mod_php, which is a seperate bit of code from the standalone (CLI) interpreter. If they are both using the same ini file, and the memcache extension is configured in the ini file, then it sounds like for some reason, the mod_php is failing to load the extension - check your webserver error_log for startup errors.
It may be that the mod_php was compiled without memcache support (most extensions need to have a stub file linked into the php code, even though the bulk of the code is not linked until run time). Or it may be a permissions problem on the shared object file. Or your webserver may be running chroot, and unable to find the extension (which would also mean that although the ini files appear to have the same path, this is relative to different roots).
HTH
C.
because both versions use different php.ini
place your php.ini into location noted in the phpinfo() outout
I would suspect that the issue revolves around permissions. When you run php from comand line, it runs as the user invoking it. When run as an apache module, it runs as "nobody".
I would assume that the memcached.so file, or the directory it's in does not have proper permissions.
I stumpled upon this post and was having the exact same problem with an extension in my php -i but not in phpinfo(). Mine was a permissions problem because of selinux on a CentOS machine. I had to change ownership and permissions and now it is working as expected.
Set php path in environment variables as given below.
Right-click on "My Computer"
Properties
Advanced Tab > Environment Variables
Under System variables, scroll down to find "Path", select and click on Edit.
Add path to your php install on the end (make sure to precede with semi-colon ";"). Example: ";C:\php7"
Click Ok.

Categories