Linux centos系统安装php-fpm Nginx时出现问题 Failed to start php-fpm.service: Unit not found. PID file

鲁德佑
2023-12-01

前言:

            自己在Linux centos7 系统下配置了Nginx服务器和php-fpm。一开始也是看网上的教程,自己敲代码。不能说别人分享的不对,怎么的,只能说每个人的系统不一样,环境也不一样。在安装的过程中,这样那样的错误也总常在。所以,我这篇博客,也不是都适用每个人。只是把自己的问题分享给大家,当大家出现类似的问题的时候,能找到共鸣,不至于绝望,搞着搞着就不想弄了,在程序员这条路上,我们会有各种各样的问题,也改不完的bug。所以,心态放好,遇到问题了,就分析问题的所在。而不是一味的百度什么的,只有找到问题的核心点,才能找到精准的解决办法,进而问题才能得以快速解决。---这个是我自己感悟。不能碰到什么问题都想都不想百度,这样效率低,还得不到解决。

背景:微信小程序:中玉玉器微商平台。由于这个项目业务扩充,原有的服务器不足以完成现在需求。需要更大的空间和响应速度,原由服务器都不能满足。

原项目架构:Apache(2.4.9)+PHP(5.5.12)+MySQL(5.6.17);现改:Nginx+PHP(7.0.0)+MySQL;


问题:

(代码比较多,请耐心看下去)

    1:Nginx服务器搭建:(部分报错错误,忘记了)

        网上教程很多,这里不一一赘述。直接讲遇到问题;

报错nginx.service不存在

        1) /etc/init.d/下新建nginx保存,

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server

# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {

    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

    echo -n $"Starting $prog: "

    daemon $nginx -c $NGINX_CONF_FILE

    retval=$?

    echo

    [ $retval -eq 0 ] && touch $lockfile

    return $retval

}


stop() {

    echo -n $"Stopping $prog: "

    killproc $prog -QUIT

    retval=$?

    echo

    [ $retval -eq 0 ] && rm -f $lockfile

    return $retval

}



restart() {

    configtest || return $?

    stop

    start

}


reload() {

    configtest || return $?

    echo -n $"Reloading $prog: "

    killproc $nginx -HUP

    RETVAL=$?

    echo

}

force_reload() {

    restart

}


configtest() {

  $nginx -t -c $NGINX_CONF_FILE

}



rh_status() {

    status $prog

}


rh_status_q() {

    rh_status >/dev/null 2>&1

}

case "$1" in

    start)

        rh_status_q && exit 0
        $1
        ;;

    stop)


        rh_status_q || exit 0
        $1
        ;;

    restart|configtest)
        $1
        ;;

    reload)
        rh_status_q || exit 7
        $1
        ;;


    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;


    condrestart|try-restart)

        rh_status_q || exit 0
            ;;

    *)

        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2

esac

    2、PHP-FPM搭建:PHP -v 是有版本信息的。如下:

 php -v

PHP 7.0.0 (cli) (built: Jun 19 2018 08:56:00) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies

    执行;service php-fpm start   报错

    1)Redirecting to /bin/systemctl start php-fpm.service Failed to start php-fpm.service: Unit not found.

    执行:systemctl start php-fpm  报错

    2)Failed to start php-fpm.service: Unit not found.

    执行:systemctl status php-fpm.service 报错

      3)Unit php-fpm.service could not be found.

所有这个都提示:php-fpm.service 没有找到。那因为就是因为不存在。于是从网上找到了如下代码:

php-fpm.service:

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target

然后结果还是不对,上面的代码或许放在你们那就是能用的。

报错如下:

systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor pre                                                                                                             set: disabled)
   Active: failed (Result: exit-code) since Tue 2018-06-19 13:28:16 CST; 16s ago
  Process: 12583 ExecStop=/bin/kill -SIGINT $MAINPID (code=exited, status=1/FAIL                                                                                                             URE)
  Process: 12581 ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-conf                                                                                                             ig /usr/local/php7/etc/php-fpm.conf (code=exited, status=203/EXEC)
 Main PID: 12581 (code=exited, status=203/EXEC)


Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: -q, --queue <sig>      use ...)
Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: -p, --pid              prin...m
Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: -l, --list [=<signal>] list...e
Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: -L, --table            list...s
Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: -h, --help     display this...t
Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: -V, --version  output versi...t
Jun 19 13:28:16 vm10-0-0-19.ksc.com kill[12583]: For more details see kill(1).
Jun 19 13:28:16 vm10-0-0-19.ksc.com systemd[1]: php-fpm.service: control pro...1
Jun 19 13:28:16 vm10-0-0-19.ksc.com systemd[1]: Unit php-fpm.service entered....
Jun 19 13:28:16 vm10-0-0-19.ksc.com systemd[1]: php-fpm.service failed.
Hint: Some lines were ellipsized, use -l to show in full
 ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[19-Jun-2018 16:17:46] ERROR: FPM initialization failed
 failed因为这个  php7 这个文件再/usr/local下不存在。而我的是php 而不是php7.所有请注意;

原因:因为这个  php7 这个文件再/usr/local下不存在。而我的是php 而不是php7.所有请注意;

改正后:[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别

[Unit] 
Description=php-fpm 
After=syslog.target network.target

[Service] 
Type=forking 
PIDFile=/var/run/php-fpm.pid 
ExecStart=/usr/local/php/sbin/php-fpm 
ExecReload=/bin/kill -USR2 MAINPIDExecStop=/bin/kill−INTMAINPID 
PrivateTmp=true

[Install] 
WantedBy=multi-user.target

我不明白为什么上面的description 改成 php-fpm后,还是不行,以及为甚么php-fpm不可以?希望懂的人,能给我解析一下

关于php-fpm.service 最终的代码内容:

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
上面改后,然后就是下面的报错。
systemctl status php-fpm.service -l
● php-fpm.service - php-fpm
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Tue 2018-06-19 16:05:46 CST; 2min 19s ago
  Process: 21559 ExecStart=/usr/local/php/sbin/php-fpm (code=exited, status=0/SUCCESS)
 Main PID: 20629 (code=exited, status=203/EXEC)

Jun 19 16:04:16 vm10-0-0-19.ksc.com systemd[1]: Starting php-fpm...
Jun 19 16:04:16 vm10-0-0-19.ksc.com systemd[1]: PID file /var/run/php-fpm.pid not readable (yet?) after start.
Jun 19 16:05:46 vm10-0-0-19.ksc.com systemd[1]: php-fpm.service start operation timed out. Terminating.
Jun 19 16:05:46 vm10-0-0-19.ksc.com systemd[1]: Failed to start php-fpm.
Jun 19 16:05:46 vm10-0-0-19.ksc.com systemd[1]: Unit php-fpm.service entered failed state.
Jun 19 16:05:46 vm10-0-0-19.ksc.com systemd[1]: php-fpm.service failed.

这个是因为php-fpm.pid根本就不存在与/var/run/所以报错。这个是源于我百度时候,让修改/usr/local/php/etc/php-fpm.conf   

;pid = run/php-fpm.pid

让去掉前面的  ;这个是不存在的。所以我又关掉。

改过php-fpm.service 后 记得systemctl deamon-restart:然后再开启systemtcl start php-fpm.service

最后问题还有  PID file /var/run/php-fpm.pid not readable (yet?) after start.  报错:

百度别人解决办法:

在/etc/init.d/下添加php-fpm 内容如下:直接复制粘贴到php-fpm下。参考:php-fpm服务启动脚本

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr/local/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
    try=0

    while test $try -lt 35 ; do

        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;

            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac

        echo -n .
        try=`expr $try + 1`
        sleep 1

    done

}

case "$1" in
    start)
        echo -n "Starting php-fpm "

        $php_fpm_BIN --daemonize $php_opts

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi

        wait_for_pid created $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -QUIT `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if [ ! -r $php_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi

        PID=`cat $php_fpm_PID`
        if ps -p $PID | grep -q $PID; then
            echo "php-fpm (pid $PID) is running..."
        else
            echo "php-fpm dead but pid file exists"
        fi
    ;;

    force-quit)
        echo -n "Terminating php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -TERM `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    reload)

        echo -n "Reload service php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -USR2 `cat $php_fpm_PID`

        echo " done"
    ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
        exit 1
    ;;

esac
解析:/etc/init.d/这个是目录,下面存放着很多的服务程序(当然都是可执行的),他基本上只做一件事,但是这件事是为你的整个系统服务的,所以init.d目录非常重要。这个目录里面包含了一系列系统里面服务的开启和停止的脚本
chmod 755 /etc/init.d/php-fpm
# php-fpm加入服务
chkconfig --add php-fpm
# php-fpm 234级别下设置为启动
chkconfig php-fpm on

到这里在重启php-fpm时,杀掉已经占用端口的进程。应该active了。当然很有可能还有其他问题,但是耐下心来,总会解决。


有关MySQL 的安装,待更新

 类似资料: