大网数据广告活动
服务器运维问答

Start PHP-FPM with root option automatically

· 52CE 我爱测

I was trying to get PHP-FPM running a pool as root, but I can't seem to adjust the init.d startup params. It works when starting PHP-FPM from the CLI (with sudo php-fpm7.0 -DRy /etc/php/7.0/fpm/php-fpm.conf , but I can't get this…

问题原文

原帖链接 · redelschaap · CC BY-SA 3.0

I was trying to get PHP-FPM running a pool as root, but I can't seem to adjust the init.d startup params.

It works when starting PHP-FPM from the CLI (with sudo php-fpm7.0 -DRy /etc/php/7.0/fpm/php-fpm.conf, but I can't get this to work with the service command (sudo service php7.0-fpm (re)start. So Everytime the server reboots, PHP-FPM fails and I would have to run this command.

I have tried to place a file "php-fpm7.0" in /etc/default with the line DAEMON_ARGS="--daemonize --fpm-config $CONFFILE -R", and I even tried add the -R parameter to the do_start function in /etc/init.d/php7.0-fpm directly, but I keep getting the please specify user and group other than root error.

Is there any way I can get this working on startup and with the service command?


Off topic: I know all the risks and I know why I shouldn't do it, but I want to anyway. I need to run some scripts to create directories, performing chown commands etc. and the pool will be dedicated to a backend used by an nginx server block which is reachable from within the private 192.168.1.X network only on one specific URL path.

As an alternative I could set up a cron script which executes a PHP script as root, but that approach would cost me a lot more hours.

已采纳解答

原帖链接 · redelschaap · CC BY-SA 3.0

I finally found a solution. It turned out Ubuntu was using systemd to start and restart PHP-FPM and was therefor ignoring the init.d files.

If you experience issues with adjustments to init.d files being ignored and you're on Ubuntu 15.04 or later, big chance that service has a systemd service file as well.


So the fix for my problem: My system has a file named /lib/systemd/system/php7.0-fpm.service, which is used by systemd. With the command cat /lib/systemd/system/php7.0-fpm.service you can see the content of the service file. You replace php7.0-fpm.service with the name of your service file. PHP7.0-FPM's service file looks like this:

[Unit]
Description=The PHP 7.0 FastCGI Process Manager
Documentation=man:php-fpm7.0(8)
After=network.target

[Service]
Type=notify
PIDFile=/run/php/php7.0-fpm.pid
ExecStart=/usr/sbin/php-fpm7.0 --nodaemonize --fpm-config /etc/php/7.0/fpm/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

In my case I needed to adjust the ExecStart rule. We don't have to edit this file, because systemd offers a way to override specific rules. The command sudo systemctl edit php7.0-fpm.service will open up an editor where you can enter those rules. Just enter the section(s) of the rule(s) you want to adjust and save the file. In my case it looks like this:

[Service]
ExecStart=
ExecStart=/usr/sbin/php-fpm7.0 --allow-to-run-as-root --nodaemonize --fpm-config /etc/php/7.0/fpm/php-fpm.conf

Make sure to reset the rule you want to edit first, otherwise both rules will be executed. Also make sure to run sudo systemctl daemon-reload after saving this file. In my case running pools as root is now allowed on Ubuntu 16.04.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。