lnmp是linux、nginx、mysql和php的縮寫,它們一起構(gòu)成了一個強大的web應(yīng)用平臺。要設(shè)置lnmp自動重啟,您可以使用以下方法:
- 使用systemd服務(wù)(推薦)
對于現(xiàn)代的Linux發(fā)行版,如Ubuntu 16.04及更高版本,建議使用systemd服務(wù)來管理LNMP組件。首先,為每個組件創(chuàng)建一個systemd服務(wù)文件。
以Nginx為例,創(chuàng)建一個名為/etc/systemd/system/nginx.service的文件,內(nèi)容如下:
[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 TERM $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
sudo systemctl enable nginx sudo systemctl start nginx
對于mysql和php-FPM,也可以創(chuàng)建類似的systemd服務(wù)文件,并使用systemctl命令啟用和啟動它們。
- 使用Supervisor
Supervisor是一個進(jìn)程管理工具,可以用來管理和監(jiān)控多個進(jìn)程。首先,確保已安裝Supervisor:
sudo apt-get install supervisor
接下來,創(chuàng)建一個名為/etc/supervisor/conf.d/lnmp.conf的文件,內(nèi)容如下:
[program:nginx] command=/usr/sbin/nginx autostart=true autorestart=true stderr_logfile=/var/log/nginx.err.log stdout_logfile=/var/log/nginx.out.log [program:mysql] command=/usr/sbin/mysqld_safe --user=mysql autostart=true autorestart=true stderr_logfile=/var/log/mysql.err.log stdout_logfile=/var/log/mysql.out.log [program:php-fpm] command=/usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/pool.d/www.conf autostart=true autorestart=true stderr_logfile=/var/log/php-fpm.log stdout_logfile=/var/log/php-fpm.out.log
請根據(jù)您的實際PHP版本和配置修改command和fpm-config路徑。保存文件后,運行以下命令重新加載Supervisor配置并啟動所有進(jìn)程:
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start all
這樣,當(dāng)系統(tǒng)啟動時,LNMP組件將自動重啟。如果某個組件意外停止,Supervisor也會自動重啟它。