Linux里Nginx和php-fpm加入开机自启动(Centos)

时间:2024-03-21 16:26:53

开机自启动nginx,php-fpm

Systemd进行系统初始化的;

Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。Systemd服务文件以.service结尾;

现在我们把nginx加入开机自启动:

一、如果是yum安装nginx的话:直接:systemcel enable nginx.service

二、源码安装的手动建立nginx.service服务文件

你需要搞清楚自己的systemd是在什么位置,才能正常的启动:

命令解释:enable是打开、disable是关闭

使用命令查看:systemctl list-unit-files --type service |grep enabled

Linux里Nginx和php-fpm加入开机自启动(Centos)

查看当前自动启动的服务;

Linux里Nginx和php-fpm加入开机自启动(Centos)

如果没有在:vim /etc/systemd/system/multi-user.target.wants/nginx.service    

修改一下你的路径:加入

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
         
[Install]
WantedBy=multi-user.target
~                                                                                                          
~                              

Linux里Nginx和php-fpm加入开机自启动(Centos)

保存;

把nginx.service加入开机自启:systemctl enable nginx.service

下面我们看看php-fpm加入开机自启;

Linux里Nginx和php-fpm加入开机自启动(Centos)

如果没有:vim  /etc/systemd/system/multi-user.target.wants/php-fpm.service    

加入:

[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~                                                    

Linux里Nginx和php-fpm加入开机自启动(Centos)

把php-fpm.service加入开机自启:systemctl enable php-fpm.service

我们先把php-fpm和nginx服务关闭;

使用:systemctl start nginx.service     、systemctl start php-fpm.service

如果开启成功说明我们已经设置成功了。接下来可以重启服务器了

shutdown -r now 

查看当前开机自启服务列表:

systemctl list-units --type=service

Linux里Nginx和php-fpm加入开机自启动(Centos)

ok了