Usually to flush nginx cache, I use the unix command :
touch /var/ngx_pagespeed_cache/cache.flush
I'd like to know if I can do the same with php in order not to log on SSH to do it.
If yes, would this code work ? :
<?php
$flush_file = "/var/ngx_pagespeed_cache/cache.flush";
touch($flush_file);
?>
If not, could you point me on how to please ?
Many thanks in advance.
Reposted my own comment above for better formatting.
Well, I guess it would be safe enough to chown this file to user which run php/webserver ex. www-data. Then give him write permission on this file.
On Debian server it would be somethings like this:
sudo chown www-data /var/ngx_pagespeed_cache/cache.flush
sudo chmod +w /var/ngx_pagespeed_cache/cache.flush
Related
I want to allow run specify command on my website, through PHP exec() function, so i found the way, to add www-data in sudoers.d files to allow run specify commands without asking password.
I tried next things:
check where is program catalogue
whereis hashcat -> hashcat: /usr/bin/hashcat
Add a new file to sudo visudo /etc/sudoers.d/www-data and add next line www-data ALL=(www-data:www-data)NOPASSWD: /user/bin/hashcat
Try to run it through www-data: sudo -u www-data sudo hashcat
And then i get this: [sudo] password for www-data:, that means it doesn't seems to work
What can i do wrong, and what i should do then? And is my decision to do so correct? Is there a better and safer solution?
That problem is solved, by removing the (www-data:www-data) so my "www-data: file in sudoers.d looks like: www-data ALL=NOPASSWD: /usr/bin/hashcat
Thanks all for answering and give me a hint to solve this! Especially thanks to #cyberbrain for my carelessness!
This is my php (turnOn.php) :
<?php
system('codesend 6984294');
?>
I have nginx, when I access to it via a web browser the system() does not launch (if I add echo 'something' in the php file it works though).
However when I shell this : php turnOn.php it does work.
I thought it was a user privilege issue so I edited my sudoers file with visudo and added this :
www-data ALL=NOPASSWD: ALL
I know it's unsafe but it does not work either. I don't know where it can come from.
Any help is greatly appreciated.
I figured out how to solve this.
Thanks to Nic3500 I was able to see that codesend needed to access root commands so I just added sudo before my command :
system('sudo /var/www/html/rf/433Utils/RPi_utils/codesend 6984302');
I finally allowed the right directory in the sudoers file :
www-data ALL=NOPASSWD: /var/www/html/rf/433Utils/RPi_utils/codesend
I need to access /boot so i can change a file continuously involving a PHP script ( To enable a raspberry pi and disable by changing start_x=' ' ) . However this involves me changing the permissions of boot.
I have tried sudo chown -R /boot and still get permissions denied when running as root. Also tried FTP and changing the folder of boot to let users modify content however this did not work either.
Is there anyway going around this???
<?php
$myfile = fopen("config.txt", "w") or die("Unable to open file!");
[...]
?>
Update: I have tried to do chmod 775 /boot/config.txt and also symlink (ran successfully) so i have a shortcut in /var/www/ however when trying to save the php script into config.txt (in www) still get permission denied even though that shortcut is set to 775. The issue still persists with changing to change the permission of the original file under /boot .
Try launching a shell and trying your command:
sudo sh -c "echo 123 >/boot/test"
works but
sudo echo 123 >/boot/test
fails. I don't understand the cause of this difference.
The user running as php would need permissions set in sudoers.d to execute the commands you need.
Try like this:
sudo chown -R root /root/boot
PHP runs under a restricted account and can't write anything in the boot partition.
You might be able to add the account PHP runs as to the sudoers file, although it's a pretty huge security risk.
I would like lauched a command in php over my RPI.
The command is echo 'standby 0' | cec-client -s, it's work fine in ssh my Tv shutdown, but in php echo shell_exec("......") return adapters autodetect FAILED ... But also mkdir doesn't work with sheel_exec, so i think it's probably PATH problem, but i don't know how to fix it. My PATH in ssh is /usr/local/bin....... and in php is /sbin:/bin:/usr/sbin:/usr/bin .
How i can fix it. Sorry for my english ...
I tried the same and got a message saying "failed to open vchiq instance". I found a question on Raspberry Pi StackExchange suggesting to add the user to the "video" group. I did that with usermod -a -G video www-data and then restarted apache with /etc/init.d/apache2 restart. Then I was able to use cec-client from PHP and apache.
It is permissions problem. While running command via ssh you executing it as user pi(probably), but via shell_exec you execute command as user www-data which do not have necessary permissions. You can check this by running echo shell_exec("whoami");
You can easly fix this by adding line "www-data ALL=(ALL) NOPASSWD: ALL" into your "/etc/sudoers" file and then run "echo shell_exec("sudo echo 'standby 0' | sudo cec-client -s");", hovewer this will add to the user www-data all of the sudo permissions and it is very unsecure, but it will work. If you want to do it more secure way you need to find which permissions cec-client required to run properly and then add them to user www-data.
I know that there are a lot of similar questions, but just hear me out and if this was a silly question and there was nothing new about it, i'll just delete this question.
I'm trying to upload using php, but I get permission denied. So here's what I did to solve it and didn't work:
first of all, this is localhost, so i can't set the owner of the files to apache, because I won't be able to edit them.
So I got the apache user name in php, and it was www-data
I created a new group called 'localhostowners'
I added my own user to this group
I added www-data to the group
when I run
grep -i 'localhostowners' /etc/group
I get
localhostowners:x:1001:mnvoh,www-data
And then I set the file permissions for everything on the localhost dir to 764
and set the owner with this:
sudo chown -R mnvoh:localhostowners localhostdir
But now I can't execute the php scripts hence the 4. although according to what I know, the 6 should be affecting apache, since I set the group on the files. What am I doing wrong here???
Thanks everybody :)
Test with:
sudo chown -R mnvoh:localhostowners localhostdir
sudo chmod 764 -R localhostdir
-R: recursivity
First you should check the permission the file
ls -a
or
ll
and make sure the person who want to execute the file have X permission.
-user-group-other
-rwx rwx rwx