【CentOS】创建启动服务(nginx、php-fpm、mysql、tomcat等)

1、nginx 举例

操作系统:CentOS Linux 7.4.1708 (Core)
前提:要添加的必须是服务进程,不能是命令工具,比如:mkdir 命令 就是工具,不能添加服务。
pid: linux 每个进程都会创建一个.pid文件,一般服务nginx 会在nginx.conf,指定pid文件存储位置
现在我要为nginx添加一个服务。

1、nginx 安装目录 /usr/local/nginx
2、nginx 可执行程序目录 /usr/local/nginx/sbin
3、nginx.conf 目录 /usr/local/nginx/conf 可以指定pid文件位置
4、nginx.pid 目录 /usr/local/nginx/pid

1、在nginx.conf中指定pid文件位置
提示:如果找不到pid文件就直接find / -name nginx.pid 其他服务同样的道理。
user  www www;
worker_processes auto;
error_log /usr/local/nginx/logs/nginx_error.log  crit;
pid /usr/local/nginx/pid/nginx.pid;  #这里
2、创建软链接
提示:注意创建的语法:ln -s是必须的,然后后面跟一个目标文件夹,最后是一个当前目录的软链接名。
ln -s /usr/local/nginx/sbin/nginx  /usr/bin/nginx
3、进入到 /usr/lib/systemd/system 目录下,编辑文件 nginx.service
cd /usr/lib/systemd/system
vim nginx.service
提示: 注意看清pid的路径,最好不要填错,否则启动不了!
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/pid/nginx.pid  # pid 文件
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/pid/nginx.pid # pid 文件
ExecStartPre=/usr/bin/nginx -t # nginx 软链接文件
ExecStart=/usr/bin/nginx # nginx 软链接文件
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target
4、重新加载 systemd
systemctl daemon-reload
5、设置nginx服务开机自启动
systemctl enable nginx.service
6、nginx常用命令
提示:启动或者重启出现错误请systemctl status nginx 查看报错原因
#启动nginx服务
systemctl start nginx
#停止nginx服务
systemctl stop nginx
#重启nginx服务
systemctl restart nginx
#加载nginx配置
systemctl reload nginx
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值