php-fpm以root身份启动

为了安全php-fpm默认是以apache用户启动的

[root@chaoge ~]# ps -aux | grep php-fpm
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      4539  0.0  0.0 205552  3716 ?        Ss   10:22   0:00 php-fpm: master process (/etc/php-fpm.conf)
apache    4540  0.0  0.0 205552  3332 ?        S    10:22   0:00 php-fpm: pool www
apache    4541  0.0  0.0 205552  3332 ?        S    10:22   0:00 php-fpm: pool www
apache    4542  0.0  0.0 205552  3332 ?        S    10:22   0:00 php-fpm: pool www
apache    4543  0.0  0.0 205552  3332 ?        S    10:22   0:00 php-fpm: pool www
apache    4544  0.0  0.0 205552  3332 ?        S    10:22   0:00 php-fpm: pool www
root      4556  0.0  0.0 103248   888 pts/1    S+   10:22   0:00 grep php-fpm

【国内直连ChatGPT 29元起】
国内直连ChatGPT,Plus会员每月29元起,支持最新o1模型探索更多领域,无需注册OpenAI账号。

部分文章说要以root身份启动需要设置一堆非常复杂的程序,并且甚至有人说不能以root身份启动。既然是php-fpm程序,所以应该先找配置文件,于是打开默认的配置文件,具体你得看你的默认配置文件路径在哪,比如我的配置文件位置在/etc/php-fpm.d/www.conf

www.conf 找到

; RPM: apache Choosed to be able to access some dir as httpd
user = apache

改成

; RPM: apache Choosed to be able to access some dir as httpd
user = root

重新启动php-fpm

[root@chaoge ~]# ps -le | grep php-fpm
1 S     0  4974     1  0  80   0 - 51388 ep_pol ?        00:00:00 php-fpm
5 S    48  4975  4974  0  80   0 - 51388 inet_c ?        00:00:00 php-fpm
5 S    48  4976  4974  0  80   0 - 51388 inet_c ?        00:00:00 php-fpm
5 S    48  4977  4974  0  80   0 - 51388 inet_c ?        00:00:00 php-fpm
5 S    48  4978  4974  0  80   0 - 51388 inet_c ?        00:00:00 php-fpm
5 S    48  5035  4974  0  80   0 - 51388 inet_c ?        00:00:00 php-fpm
[root@chaoge ~]# kill -QUIT 5035
[root@chaoge ~]# php-fpm
[17-Apr-2015 10:33:38] ERROR: [pool www] please specify user and group other than root
[17-Apr-2015 10:33:38] ERROR: FPM initialization failed
[root@chaoge ~]#

报错了,提示运行php-fpm运行用户和组只能选择其它的除了root以外,于是查看php-fpm文档,会发现其中一个选项-R

[root@chaoge ~]# php-fpm --help
Usage: php [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F]
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -h               This help
  -i               PHP information
  -m               Show compiled in modules
  -v               Version number
  -p, --prefix <dir>
                   Specify alternative prefix path to FastCGI process manager (default: /usr).
  -g, --pid <file>
                   Specify the PID file location.
  -y, --fpm-config <file>
                   Specify alternative path to FastCGI process manager config file.
  -t, --test       Test FPM configuration and exit
  -D, --daemonize  force to run in background, and ignore daemonize option from config file
  -F, --nodaemonize
                   force to stay in foreground, and ignore daemonize option from config file
  -R, --allow-to-run-as-root
                   Allow pool to run as root (disabled by default)

于是我们试着启动的时候加上这个参数试一试

[root@chaoge ~]# php-fpm -R
[root@chaoge ~]# ps -aux | grep php-fpm
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      5497  0.0  0.0 205552  3712 ?        Ss   10:36   0:00 php-fpm: master process (/etc/php-fpm.conf)
root      5498  0.0  0.0 205552  3196 ?        S    10:36   0:00 php-fpm: pool www
root      5499  0.0  0.0 205552  3196 ?        S    10:36   0:00 php-fpm: pool www
root      5500  0.0  0.0 205552  3196 ?        S    10:36   0:00 php-fpm: pool www
root      5501  0.0  0.0 205552  3200 ?        S    10:36   0:00 php-fpm: pool www
root      5502  0.0  0.0 205552  3200 ?        S    10:36   0:00 php-fpm: pool www
root      5524  0.0  0.0 103252   888 pts/1    S+   10:36   0:00 grep php-fpm

成功启动了,测试一下,以root用户写了一个shell脚本保存在root宿主目录下,php里exec写好的脚本正常运行。

full

最后,奉劝大家生产环境大家最好别这样做,否则你知道的。。。

分享

TITLE: php-fpm以root身份启动

LINK: https://www.qttc.net/461-php-fpm-run-as-root.html

NOTE: 原创内容,转载请注明出自琼台博客