ubuntu

Ubuntu LNMP如何实现自动重启

小樊
36
2025-03-26 10:27:54
栏目: 智能运维

在Ubuntu系统中,LNMP(Linux, Nginx, MySQL, PHP)堆栈的自动重启可以通过多种方式实现。以下是一些常见的方法:

1. 使用Systemd服务

Systemd是现代Linux发行版中用于管理系统服务的初始化系统和服务管理器。你可以为Nginx、MySQL和PHP-FPM创建Systemd服务,并配置它们在失败时自动重启。

Nginx

  1. 创建或编辑Nginx的Systemd服务文件:
    sudo nano /etc/systemd/system/nginx.service
    
  2. 确保文件内容如下:
    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  3. 重新加载Systemd配置并启动Nginx服务:
    sudo systemctl daemon-reload
    sudo systemctl start nginx
    sudo systemctl enable nginx
    

MySQL

  1. 创建或编辑MySQL的Systemd服务文件:
    sudo nano /etc/systemd/system/mysql.service
    
  2. 确保文件内容如下:
    [Unit]
    Description=MySQL Community Server
    After=network.target
    
    [Service]
    ExecStart=/usr/sbin/mysqld
    ExecReload=/bin/kill -s HUP $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  3. 重新加载Systemd配置并启动MySQL服务:
    sudo systemctl daemon-reload
    sudo systemctl start mysql
    sudo systemctl enable mysql
    

PHP-FPM

  1. 创建或编辑PHP-FPM的Systemd服务文件:
    sudo nano /etc/systemd/system/php7.4-fpm.service
    
  2. 确保文件内容如下(根据你的PHP版本调整):
    [Unit]
    Description=The PHP FastCGI Process Manager
    After=syslog.target network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/php-fpm.conf
    ExecReload=/bin/kill -USR2 $MAINPID
    
    [Install]
    WantedBy=multi-user.target
    
  3. 重新加载Systemd配置并启动PHP-FPM服务:
    sudo systemctl daemon-reload
    sudo systemctl start php7.4-fpm
    sudo systemctl enable php7.4-fpm
    

2. 使用Supervisor

Supervisor是一个进程控制系统,可以用来管理和监控多个进程。

  1. 安装Supervisor:
    sudo apt-get install supervisor
    
  2. 创建Supervisor配置文件:
    sudo nano /etc/supervisor/conf.d/nginx.conf
    
  3. 添加以下内容:
    [program:nginx]
    command=/usr/sbin/nginx
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/nginx.err.log
    stdout_logfile=/var/log/nginx.out.log
    
  4. 重复上述步骤为MySQL和PHP-FPM创建配置文件。
  5. 更新Supervisor配置并启动进程:
    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start nginx
    sudo supervisorctl start mysql
    sudo supervisorctl start php7.4-fpm
    

3. 使用Cron和Shell脚本

你可以编写一个简单的Shell脚本来检查进程是否运行,并在必要时重启它们,然后使用Cron定期运行这个脚本。

  1. 创建Shell脚本:
    sudo nano /usr/local/bin/lnmp_restart.sh
    
  2. 添加以下内容:
    #!/bin/bash
    
    # Check and restart Nginx
    if ! pgrep -x "nginx" > /dev/null
    then
        echo "Nginx is not running. Restarting..."
        sudo systemctl start nginx
    fi
    
    # Check and restart MySQL
    if ! pgrep -x "mysqld" > /dev/null
    then
        echo "MySQL is not running. Restarting..."
        sudo systemctl start mysql
    fi
    
    # Check and restart PHP-FPM
    if ! pgrep -x "php-fpm" > /dev/null
    then
        echo "PHP-FPM is not running. Restarting..."
        sudo systemctl start php7.4-fpm
    fi
    
  3. 赋予脚本执行权限:
    sudo chmod +x /usr/local/bin/lnmp_restart.sh
    
  4. 使用Cron每分钟运行一次脚本:
    crontab -e
    
  5. 添加以下行:
    * * * * * /usr/local/bin/lnmp_restart.sh
    

通过以上方法,你可以确保在Ubuntu系统中LNMP堆栈的各个组件在失败时能够自动重启。

0
看了该问题的人还看了