linux

Linux PHP-FPM的启动脚本怎么写

小樊
45
2025-07-23 13:07:56
栏目: 编程语言

在Linux系统中,PHP-FPM(FastCGI Process Manager)是一个用于管理PHP FastCGI进程的工具。要编写一个启动脚本,你需要遵循以下步骤:

  1. 创建一个新的脚本文件,例如:/etc/init.d/php-fpm
#!/bin/bash
  1. 为脚本添加可执行权限:
chmod +x /etc/init.d/php-fpm
  1. 编辑脚本,添加以下内容:
#!/bin/bash

# PHP-FPM configuration file path
PHPRC="/etc/php-fpm.conf"

# PHP-FPM process manager user and group
PHP_FPM_USER="www-data"
PHP_FPM_GROUP="www-data"

# PHP-FPM pool configuration file path
PHP_POOL_CONF="/etc/php-fpm.d/www.conf"

# Start PHP-FPM
start() {
    if [ -f $PHPRC ]; then
        # Check if PHP-FPM is already running
        if ps aux | grep '[p]hp-fpm: master process' > /dev/null; then
            echo "PHP-FPM is already running."
        else
            # Start PHP-FPM with the specified user and group
            start-stop-daemon --start --quiet --user $PHP_FPM_USER --group $PHP_FPM_GROUP --exec /usr/sbin/php-fpm -- --nodaemonize --fpm-config $PHPRC
            echo "PHP-FPM started."
        fi
    else
        echo "PHP-FPM configuration file not found: $PHPRC"
    fi
}

# Stop PHP-FPM
stop() {
    if ps aux | grep '[p]hp-fpm: master process' > /dev/null; then
        # Stop PHP-FPM
        start-stop-daemon --stop --quiet --exec /usr/sbin/php-fpm
        echo "PHP-FPM stopped."
    else
        echo "PHP-FPM is not running."
    fi
}

# Restart PHP-FPM
restart() {
    stop
    start
}

# Check PHP-FPM status
status() {
    if ps aux | grep '[p]hp-fpm: master process' > /dev/null; then
        echo "PHP-FPM is running."
    else
        echo "PHP-FPM is not running."
    fi
}

# Parse command line arguments
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit 0
  1. 根据你的系统和PHP-FPM安装路径,修改脚本中的配置文件路径、用户和组。

  2. 将脚本添加到系统服务:

chkconfig --add php-fpm
  1. 启动PHP-FPM服务:
service php-fpm start

现在你已经创建了一个基本的PHP-FPM启动脚本。你可以使用service php-fpm startservice php-fpm stopservice php-fpm restartservice php-fpm status命令来控制PHP-FPM服务。

0
看了该问题的人还看了