ubuntu

Ubuntu LNMP如何设置自动重启

小樊
45
2025-07-01 07:38:33
栏目: 智能运维

在 Ubuntu 上为 LNMP(Linux, Nginx, MySQL, PHP)设置自动重启,可以通过以下几种方法实现:

方法一:使用 systemd 服务

  1. 创建 systemd 服务文件

    对于 Nginx:

    sudo nano /etc/systemd/system/nginx.service
    

    添加以下内容:

    [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
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    对于 MySQL:

    sudo nano /etc/systemd/system/mysql.service
    

    添加以下内容:

    [Unit]
    Description=MySQL Community Server
    After=network.target
    
    [Service]
    ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    对于 PHP-FPM:

    sudo nano /etc/systemd/system/php7.4-fpm.service
    

    添加以下内容(根据你的 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
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  2. 重新加载 systemd 配置

    sudo systemctl daemon-reload
    
  3. 启用服务自动启动

    sudo systemctl enable nginx
    sudo systemctl enable mysql
    sudo systemctl enable php7.4-fpm
    
  4. 启动服务

    sudo systemctl start nginx
    sudo systemctl start mysql
    sudo systemctl start php7.4-fpm
    

方法二:使用 cron 任务

虽然 systemd 是更现代和推荐的方法,但你也可以使用 cron 任务来定期重启服务。

  1. 编辑 crontab 文件

    crontab -e
    
  2. 添加重启任务

    0 * * * * /usr/sbin/systemctl restart nginx
    0 * * * * /usr/sbin/systemctl restart mysql
    0 * * * * /usr/sbin/systemctl restart php7.4-fpm
    

    这将每小时的第一分钟重启这些服务。

方法三:使用 Upstart(适用于旧版本 Ubuntu)

如果你使用的是较旧的 Ubuntu 版本(如 16.04 及之前),可以使用 Upstart 来管理服务。

  1. 创建 Upstart 配置文件

    对于 Nginx:

    sudo nano /etc/init/nginx.conf
    

    添加以下内容:

    description "The NGINX HTTP and reverse proxy server"
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    
    exec /usr/sbin/nginx -g "daemon off;"
    

    对于 MySQL:

    sudo nano /etc/init/mysql.conf
    

    添加以下内容:

    description "MySQL Community Server"
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    
    exec /usr/sbin/mysqld_safe --user=mysql --datadir=/var/lib/mysql
    

    对于 PHP-FPM:

    sudo nano /etc/init/php7.4-fpm.conf
    

    添加以下内容:

    description "The PHP FastCGI Process Manager"
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    
    exec /usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/php-fpm.conf
    
  2. 重新加载 Upstart 配置

    sudo initctl reload-configuration
    
  3. 启动服务

    sudo start nginx
    sudo start mysql
    sudo start php7.4-fpm
    

通过以上方法,你可以确保 LNMP 环境在系统重启后自动启动,并且可以定期重启以保持服务的稳定性。

0
看了该问题的人还看了