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

举个栗子:nginx

现在我要为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文件位置

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 /usr/local/nginx/sbin/nginx /usr/bin/nginx

参考:
【CentOS】 软链接
【CentOS】pid文件

3、进入到 /usr/lib/systemd/system 目录下,编辑文件 nginx.service

cd /usr/lib/systemd/system

vim nginx.service

[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常用命令

启动nginx服务

systemctl start nginx

停止nginx服务

systemctl stop nginx

重启nginx服务

systemctl restart nginx

加载配置

systemctl reload nginx

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。