php7 fpm sock file ownership on service restart - php

I have just installed PHP7
https://github.com/kasparsd/php-7-debian
Everything works except each time I do service php7-fpm restart I need manually to set ownership on file /run/php7-fpm.sock to www-data
After setting the permissions everything works.. But how to avoid this on every restart?
The ownership is always root after restart
php-fpm.ini
user = www-data
group = www-data

Ownership of the file socket is determined by the listen.owner and listen.group directives in the FPM config file.

If thats your local development environment, just add www-data to the sudo. That should make things easy.
Also try reinstalling PHP
Following tutorial might be helpful.
https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04

Related

php application fails to detect the right temp directory

In my Laravel 9 application running on AWS EC2, I'm trying to upload a file.
The action fails with this message: File could not be uploaded: missing temporary directory.
Inside my loaded php.ini, I have it set as upload_tmp_dir = /tmp
But when I do a phpinfo, I see the value for upload_tmp_dir is empty. Looks like whatever value I add for upload_tmp_dir is not read/recognized by php.
I used echo sys_get_temp_dir(); to get the path that the app is using, and it returns as:
/var/www/html/tmp
What's the reason the app is failing to detect the right path to the tmp directory?
If it matters, here are also the directory permissions:
/tmp is owned by root/root and is drwxrwxrwt+
everything under /var/www/html (where the app is) is owned by ec2-user/apache
Here is how I resolved this issue:
Edit /usr/lib/systemd/system/php-fpm.service. Set PrivateTmp=false
Edit /usr/lib/systemd/system/httpd.service. Set PrivateTmp=false
Reload parameters: sudo systemctl daemon-reload
Restart service: sudo systemctl restart php-fpm and sudo systemctl restart httpd

white screen when running the phpMyAdmin 5.2.0 script from another apache (php-fpm) user

operating system: RockyLinux 8
apache version: 2.4.37
php version: 7.2.24 (cli)
script location: /var/www/html
phpMyAdmin version: 5.2.0
chmod permissions on files/folders don't matter (please use default system chmod permissions)
there are no errors in the Apache/PHP-FPM logs that would indicate this problem
please do the following:
$ sudo useradd -d /var/www/ -m -U -s /sbin/nologin www
change the lines in the file /etc/httpd/conf/httpd.conf
User www
Group www
change the lines in the file /etc/php-fpm.d/www.conf
user = www
group = www
listen.acl_users = www,nginx
restart Apache and PHP-FPM
$ sudo systemctl restart httpd
$ sudo systemctl restart php-fpm
open the page in a browser, you will see a white screen
now, set for php-fpm configuration user as apache and group as apache
change the lines in the file /etc/php-fpm.d/www.conf
user = apache
group = apache
restart PHP-FPM
$ sudo systemctl restart php-fpm
now you see the main page of the phpMyAdmin script in a browser
what's the matter here? Thank you!
I was having these exact same symptoms in my Rocky 8 environment. Basically trying to run the entire LAMP service as a user other than "apache", would cause phpMyAdmin to show a blank screen, while all other PHP scripts still worked fine.
The solution for me was to change the permissions of three folders within /var/lib/php directory:
/var/lib/php/opcahce
/var/lib/php/session
/var/lib/php/wsdlcache
These were all set to root:apache ownership with file permissions 770 (rwxrwx---).
By modifying the permissions on these three folders to be writable by the new user (non apache) that is running the httpd & php-fpm services, the phpMyAdmin page began working again. In my case I simply changed the ownership to root:newuser ("newuser" being the user running the services rather than apache).
Environment:
Rocky Linux 8.7: 4.18.0-425.3.1.el8.x86_64
WEB: Apache/2.4.37 (rocky) OpenSSL/1.1.1k SVN/1.14.1
PHP: 7.4.30

Restart apache2 via PHP script

I want to restart apache2 when I load a page with the following code:
exec('/etc/init.d/apache2 reload', $output, $return);
if(!$return) {
$result = "<script>console.log('can not restart apache2');</script>";
echo $result;
echo $output;
} else {
$result = "<script>console.log('restart apache2 successfuly');</script>";
echo $result;
}
And in file etc/sudoers I add this lines:
Cmnd_Alias RESTART_APACHE = /etc/service apache2 restart
www-data ALL=NOPASSWD: RESTART_APACHE
But the result return can not restart apache2.
Am I do something wrong?
#tuanptit
<?php echo shell_exec('service httpd restart &'); ?>
You might have permissions problems with such a script attempting to do this though. It sounds like you're in a full-on dev environment though, so it shouldn't matter for you to give elevated privileges to it.
But the best way the best way to handle this, IMHO, is to give the user that Apache runs under access to restart Apache via the sudo command.
You'll want to edit your /etc/sudoers file and add lines similar to the following:
Cmnd_Alias RESTART_APACHE = /sbin/service apache2 restart
www-data ALL=NOPASSWD: RESTART_APACHE
You may need nobody instead of www-data, it depends on the user which Apache runs under. On Debian, Apache typically runs under user www-data, whereas under Red Hat, often Apache runs under user nobody. Also, the /sbin/service apache2 restart may need to be /sbin/service apache restart or maybe /sbin/service httpd restart. All depends on your system's configuration.
Once that's done, in PHP you can use the code:
exec('/sbin/service apache2 restart');
(Obviously changing that if the command to restart Apache differs on your server.)
Please note: this could very well be considered a security risk! If you do this, you fully trust the sudo binary, the service binary, and your system to obey the rules and not let an Apache/PHP process get a root shell. I highly recommend asking on http://serverfault.com for the implications of what you're doing here.
The Apache service probably dont have rights to restart itself
The solution already discussed here.
How do you restart Apache with a (web) button click?
In the sudoers files you have "restart", in php file you have "reload"
Check if you need to use /sbin/service instead of /etc/service
Make sure the commands match in PHP and sudoer file

Different php result between terminal and browser

My server is running php-fpm with nginx. I had applied these permissions.
sudo chgrp -R www-data /usr/share/nginx/html
sudo chmod -R g+rw /usr/share/nginx/html
sudo chmod g+s /usr/share/nginx/html
I have this code.
if(function_exists("my_void_function")){
echo "exist";
}else{
echo "none exist";
}
I am getting different result that simple code. you can see screenshot of my pc from this image.
As you have added the function via a custom extension, you must load the extension with the respective php.ini file of the PHP engine. PHP CLI has a different php.ini than PHP-FPM/mod_php.
Also, after changing php.ini of PHP-FPM, you must restart the PHP-FPM service (usually sudo service php-fpm restart on Debian-like systems). Those who are using mod_php with Apache need to restart the Apache service.

Vagrant vm + apache permission denied for mkdir()

I am using vagrant to run my vm using ubuntu, apache, etc.
This is running on my OSX host.
Everything works fine until my php application tries to create a folder, files within that folder, etc.
I get Permission denied.
What am I missing on the provision / configuration of the vagrant file?
in this case, if you're using Vagrant + Apache2.
the solution is:
edit /etc/apache2/apache2.conf;
Search for User and Group directives
Change these lines to:
User vagrant
Group vagrant
run sudo service apache2 restart
its not possible to change /vagrant directory owner to www-data (apache user);
Anyway, it works for me.
Edit Vagrantfile
config.vm.synced_folder "./", "/var/www", owner: "www-data", group: "www-data"
You need to set the owner on the folder that php is trying to write to, to the apache user which is normally www-data... ssh into the vagrant box and try:
sudo chown www-data:www-data <dirname>
That should work... if it does then just add that to what ever provisioner you are using.

Categories