在 vagrant ubuntu 14.04 环境中配置的 php-fpm 总是自启动失败,每次都需要 vagrant ssh 上去切换到 root 手动起来
sudo su -
/etc/init.d/php-fpm start
非常不专业
排查思路
首先,确认 init.d 是否有统一的日志文件,看 centos - Is there an init.d start log? - Unix & Linux Stack Exchange,并没有。
然后,确认一下,php-fpm 的日志文件是否记录了启动错误。从 /etc/init.d/php-fpm 启动脚步文件中
php_fpm_CONF=/usr/share/php52/etc/php-fpm.conf
得知 php-fpm.conf 的位置,然后找到对应的错误文件的位置
/usr/share/php52/logs/php-fpm.log
然而从 php-fpm.log 中,并没有找到启动失败的日志
Nov 24 09:12:49.308395 [NOTICE] fpm_unix_init_main(), line 284: getrlimit(nofile): max:4096, cur:1024
Nov 24 09:12:49.325857 [NOTICE] fpm_event_init_main(), line 88: libevent: using epoll
Nov 24 09:12:49.326117 [NOTICE] fpm_init(), line 52: fpm is running, pid 2374
Nov 24 09:12:49.326845 [NOTICE] fpm_children_make(), line 352: child 2375 (pool default) started
Nov 24 09:12:49.327193 [NOTICE] fpm_children_make(), line 352: child 2376 (pool default) started
Nov 24 09:12:49.327337 [NOTICE] fpm_children_make(), line 352: child 2377 (pool default) started
Nov 24 09:12:49.327525 [NOTICE] fpm_children_make(), line 352: child 2378 (pool default) started
Nov 24 09:12:49.327707 [NOTICE] fpm_children_make(), line 352: child 2379 (pool default) started
Nov 24 09:12:49.327726 [NOTICE] fpm_event_loop(), line 107: libevent: entering main loop
使用默认的 vagrant 用户启动
vagrant@vagrant-ubuntu-trusty-64:~$ /etc/init.d/php-fpm start
Starting php_fpm Nov 24 11:20:09.272806 [ERROR] fpm_stdio_open_error_log(), line 266: open("/usr/share/php52/logs/php-fpm.log") failed: Permission denied (13)
failed
然后这个错误信息,并没有出现在 php-fpm.log 中,说明 init.d 如果是因为权限问题启动失败,那么对应的错误信息也不会出现在日志中。
那么 init.d/php-fpm 到底是由哪个用户启动的呢?将 logs 目录的权限进行了修改
chown vagrant:vagrant -R /usr/share/php52/logs/
仍然启动失败,说明也并不是由 vagrant 启动的。那么如何让启动脚本由 root 账户开机自启动呢?参考 How to run a script during boot as root - Ask Ubuntu
第一次知道 init.d 中的脚步并不会被自动执行,需要软链到 rcX.d 中才能被执行,突然感觉这么多年 Linux 是白玩了!
于是
ln -s /etc/init.d/php-fpm /etc/rc3.d/S99php-fpm
启动之后,依然没有发现 php-fpm 进程,突然发现这句话
Note that under Ubuntu Server 14.04, I needed to use /etc/rc2.d, instead of rc3.d as indicated in the example.
于是把 rc3.d 换成 rc2.d
ln -s /etc/init.d/php-fpm /etc/rc2.d/S99php-fpm
vagrant halt & vagrant up, 哈哈 php-fpm 可以正常自启动了!
runlevel 2 与 3 的区别
3 is used when a system is in full production. Take it as the runlevel your software will run. 2 is historical and is like 3, but without NFS.
那为何 Ubuntu 中使用的是 runlevel 2 而不是 runlevel 3 呢?
The Debian distribution (and hence Ubuntu, which is derived from it) does not define any differences between runlevels 2-5 as a matter of policy. It is up to the local system administrator to make use of runlevels as they see fit. Since there is no difference between runlevels 2-5, a default runlevel 2 was chosen.
我觉得 Debian 系很实在,搞得简单点挺好!