I use CakePHP (v2) Shell to execute a process behind my online application.
This Shell is part of a Plugin, so stored in:
app/Plugins/MyPlugin/Console/Command/MyScriptShell.php
I used 2 different dev servers, and everything goes well with the execution of this Shell.
Unfortunately, the execution fails on my production environment.
On a Unix terminal, if I try manually to execute the command below, it fails:
# app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
PHP Fatal error: Class 'Controller' not found in /var/www/app/Controller/AppController.php on line 6
In an other side, if I execute the same command line with a "sudo -u {a-unix-user} {command}", the execution works! Here are 2 examples which work:
# sudo -u www-data app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
# sudo -u root app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
Ok, so noticing this behavior, I thought about a unix rights issue or something like this. I decided, as a new test, to grant a unix session of www-data in my termina to execute the same command, but without the "sudo -u {a-unix-user} {command}" directive. Unfortunately, this fails too:
# sudo su www-data
www-data# whoami
www-data
www-data# app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
PHP Fatal error: Class 'Controller' not found in /var/www/app/Controller/AppController.php on line 6
www-data#
So here is a summary of the problem I'm occurring right now…
Any idea how to solve this weird behavior?
Thanks.
Ok, so no one found how to help, but I did it myself ;)
For those experiencing a such behavior, I would recommend you to verify the system PATHS set as variable into your code.
In my case, into bootstrap.php, I've a WWW_PATH define which contains the local path to reach the base of the code directory.
define('WWW_PATH', '/home/foo/www-prod/www/');
In reality, my code directory is located under /var/www-prod/www/, and /home/foo/www-prod is just a unix symbolic link.
It seems that CakePhp Shell cannot load properly ressource with a BASE_DIR using a symbolic link, because after altering my code without this symbolic link into the path, it worked!
Related
here is my problem: i would like to create a directory and limit it size using this method.
The thing is when i try it via cli its working perfectly (the file system is mounted on the newly created directory with its size limit), however when i put it in a bash script the directory is created but not with its size limit.
Here is my script.sh:
# !/bin/bash
# Set default parameters
limited_directory_name=$1
size=$2
# Setting path
parent_path="/path/to/directory/parent/"
directory_path="$parent_path$limited_directory_name"
# Creating directory/mountpoint
mkdir "$directory_path"
#Creating a file full of /dev/zero
limited_size_file="$directory_path.ext4"
touch "$limited_size_file"
dd if=/dev/zero of="$limited_size_file" bs="$size" count=1
#Formating the file
sudo mkfs.ext4 "$limited_size_file"
#Mount the disk
sudo mount -o loop,rw,usrquota,grpquota "$limited_size_file" "$directory_path"
I believe (pretty sure actually), that the problem is in these two last lines
sudo mkfs.ext4 "$limited_size_file"
or/and
sudo mount -o loop,rw,usrquota,grpquota "$limited_size_file" "$directory_path"
because as i said, the file and directory are created but just not with the size limit.
Also when i try to delete the directory ($directory_path/) after executing those command via cli i got : rm: cannot delete '$directory_path/': Device or resource busy, that i dont get when trying to delete it after executing the script. So i guess that the file system is not mounted when executing the script, and the problem is probably in the last two lines. I dont know if its has something to do with the way of using sudo inside a script or just something with mounting a file system inside a bash script.
I just wanna say that i am fairly new to bash scripting and i am sorry if my mistake is something like an obvious (noob) error. You can also say if i can improve my question in any way and i apologize if it's not clear enough.
And one last thing, i have tried different syntax for the last two line like:
sudo $(mkfs.ext4 "$limited_size_file")
or
sudo `mkfs.ext4 "$limited_size_file"`
or just
mkfs.ext4 "$limited_size_file" without sudo.
But nothing seems to work. I am using debian 10 btw and im calling the script like this in a PHP page (if it can help):
exec("myscript.sh $dname $dsize");
I have written simple php script to help me update site contents when the commit is sent to bitbucket. I have following problem with it.
<?php
$repo_dir = '/var/www/vhosts/my_full_path';
$output = shell_exec('cd '.$repo_dir.' && hg --config auth.rc.prefix=https://bitbucket.org/XXXXX --config auth.rc.username=my_username --config auth.rc.password=my_pass pull -u https://bitbucket.org/XXXXXXX &');
echo $output;
?>
When I type it to web browser it doesn't work. The output of script is:
pulling from https://bitbucket.org/XXXXXXXXXXXXXX
but when I try to execute it under console on the server it works like a charm:
php myscript.php
generates following output:
pulling from https://bitbucket.org/XXXX
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 2 changes to 1 files
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
See the oupt is full and correct! in concole I'm using root user in web browser data-www? Is there any difference in this case?
I have found the solution. I hope it helps someone.
There were two problems:
Permissions to my repo dir
Authentication for user www-data for this repo
The problem occured because web browser doesn't flush warnings and abort messages while executing command shell_exec. If you want to test your script, you have to lgoin to console by SSH (as root for example) then execute script / command as apache user:
sudo -u www-data php /path-to-your-script/script.php
In console you will see all problems which following user generates.
So my first problem is that I'm hosting on GoDaddy.
Second problem is that when I try to execute the following PHP script via SSH...
./searchreplacedb2cli.php -h hostname -u username -d databasename -p 'password'
...I get a /usr/bin/php: bad interpreter: No such file or directory error. This is because on shared hosting, there is no /usr/bin/php. Instead, the php cli I want is located here:
/usr/local/php5_3/bin/php
So, I manually changed the shebang declaration in the script to...
#!/usr/local/php5_3/bin/php -q
...and sure enough, it works.
However, I don't want to have to edit this script manually. I'm looking for a way to circumvent this issue programmatically. I use this script to deploy Wordpress sites on the fly. I wget it from a git repository each time I use it. Furthermore it is deleted and installed regularly from a bash script, so modifying it in a text editor each time is not an option.
Is there a way to pass an alternate interpreter to a bash command? Like, "if you don't find the default interpreter, use this one instead".
I tried this but it doesn't work:
./searchreplacedb2cli.php -h hostname -u username -d databasename -p 'password' | /usr/local/php5_3/bin/php
And because it's shared hosting I lack the permissions to symlink /usr/bin/php to the right place.
My next idea was to, via bash, edit line 1 of the php script using a sed replace command. I thought I would inquire here first for alternatives. Thanks.
If it's only this host, add /usr/local/php5_3/bin/ to your $PATH, and use this shebang:
#!/usr/bin/env php
I've got a recently setup server which is running Apache and PHP 5.3.9, every request made to the server results in an error in the error log file:
sh: line 0: cd: /root: Permission denied
The error is happening before any PHP code is executed so it must be something in the setup somewhere but it's a new box and is running a stock configuration.
What would cause this error?
EDIT:
Running on Amazon Linux (EC2) 2.6.35.14-97.44.amzn1.x86_64
It shouldn't be running any scripts, it's running PHP in mod_php mode, not CGI and the error is happening before and PHP code is executed
Could look like a script that does not have executable rights.
If you know which script it is, you can give it the rights by:
chmod +x /path/to/your/script.sh
As root or with sudo infront.
Edit:
Looking agin, it looks like your script is trying to cd to your /root folder.
Most likely the script is run by another user which does not have permission to cd to /root.
Only root has that
This is the AWSSDKforPHP doing this.
Inside /usr/share/pear/AWSSDKforPHP/sdk.class.php you will find:
$_ENV['HOME'] = `cd ~ && pwd`;
For some silly reason, it's trying to "cd" into /root. Change that line to the following:
$_ENV['HOME'] = "/var/www"; #`cd ~ && pwd`;
And it will go away.
I am trying to run a command line file conversion using open office.
openoffice pdf filename.doc 2>&1
when i execute in command line as root it works fine and the file is converted. However when i pass the above command in a PHP file as apache user, it does not execute.
I tried all three PHP command line execution:
$command_output=system($command_line,$rtnval);
$command_output=exec($command_line,$rtnval);
$command_output=passthru($command_line,$rtnval);
Also,
echo print_r($rtnval);
echo print_r($command_output);
$rtnval returns 1 and $command_output 1. I am confused unable to know what is the linux (centos) response to above command passed. It is very frustration because unable to know what the system response when i try to execute the command.
I also included /etc/suders permission for apache to run the open office command.
apache ALL: (ALL) NOPASSWD: /path/to/openoffice
still the command is not execute in PHP as apache user.
What am i missing for PHP as apache user not to execute this command?
It could be that openoffice is not in PATH. Try to execute it with the full path.
To run your command as if you were the apache user, just try this in a shell:
# switch to superuser
sudo su -
# then switch to the apache user
su - www-data
You will find yourself in a quite restricted shell, from which it is usually not possible to start openoffice. Indeed, it requires a lot of environment, that would be unsafe to completely set up for apache anyway.
AFAIK, better create a dedicated user that is allowed to run your command (eg a regular "www-runner" user), then "su" to it from PHP. Other security measures include chroot'ing the dedidacted user, or using apparmor to limit what and where it is allowed to run. In any case, never let www-data run something as root by adding www-data to the sudoers: this is way too dangerous!
You can also have a look at libapache2-mod-suphp (a suid apache module to run php scripts with the owner permissions).It is easier to use than the dedicated suEXEC apache beast (http://httpd.apache.org/docs/2.0/suexec.html). The latter really is not for a quick fix ;)
It is possible that your php in apache runs in safe mode or what's it called, in which system() function and alike are disabled.
This answer, actually, assumes that what you call "running as apache user" is in fact running in apache environment, whatever it is.