美文网首页centosphp
centos7开机自启动nginx,mysql,php-fpm

centos7开机自启动nginx,mysql,php-fpm

作者: Fizz_YF | 来源:发表于2020-08-11 15:03 被阅读0次

centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。 Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动。

  • 如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令systemcel enable nginx.service设置开机启动即可。

systemcel enable nginx.service
  • 源码安装的则需手动建立nginx.service和php-fpm.service服务文件

1、 在系统服务目录里创建nginx.service文件
vim /lib/systemd/system/nginx.service

写入以下内容(路径改成自己的)

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/tengine/sbin/nginx -c /usr/local/tengine/conf/nginx.conf
ExecReload=/usr/local/tengine/sbin/nginx -s reload
ExecStop=/usr/local/tengine/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

2、在系统服务目录里创建mysqld.service文件
vim /lib/systemd/system/mysqld.service

注意:写入以下内容(路径改成自己的),注意这里(/usr/local/mysql/bin/mysqld)是安装mysql的绝对路径,不要写成/etc/init.d/mysqld

[Unit]
Description=MySQL Server
After=network.target
 
[Install]
WantedBy=multi-user.target
 
[Service]
Type=forking
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonize
LimitNOFILE = 65535
Restart=on-failure
RestartSec=3
RestartPreventExitStatus=1
PrivateTmp=false

3、 在系统服务目录里创建php-fpm.service文件
vim /lib/systemd/system/php-fpm.service

写入以下内容(路径改成自己的)

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

测试并加入开机自启:

先关闭nginx,php-fpm

  • 使用以下命令开启
systemctl start nginx.service              #如果服务是开启状态,使用此命令会启动失败。
systemctl start php-fpm.service
  • 开启成功,将服务加入开机自启
systemctl enable nginx.service             #注意后面不能跟空格
systemctl enable php-fpm.service
  • 重启服务器,查看是否启动
shutdown -r now        #重启
systemctl list-units --type=service        #查看运行的服务
  • 其他命令
systemctl start nginx.service              #启动nginx服务
systemctl enable nginx.service             #设置开机自启动
systemctl disable nginx.service            #停止开机自启动
systemctl status nginx.service             #查看服务当前状态
systemctl restart nginx.service           #重新启动服务
systemctl list-units --type=service        #查看所有已启动的服务

如果还有问题,不妨和我一起讨论。请多多关注:python基础
干货不定时更新喔

相关文章

网友评论

    本文标题:centos7开机自启动nginx,mysql,php-fpm

    本文链接:https://www.haomeiwen.com/subject/nmfydktx.html