网站首页 > 技术教程 正文
利用 systemctl 命令:
如果用 yum install 命令安装的 nginx,yum 命令会自动创建 nginx.service 文件,直接用命令:
systemctl enable nginx # 设置开机启动
Nginxsystemctl disable nginx # 关闭开机启动 Nginx
就可以设置开机自启,否则需要在系统服务目录里创建 nginx.service 文件。
创建并打开 nginx.service 文件:
vim /lib/systemd/system/nginx.service
内容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s
reloadExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq 保存退出,运行 systemctl daemon-reload 使文件生效。
这样便可以通过以下命令进行操作 nginx 了:
systemctl start nginx.service # 启动nginx服务
systemctl enable nginx.service # 设置开机启动
systemctl disable nginx.service # 停止开机自启动
systemctl status nginx.service # 查看服务当前状态
systemctl restart nginx.service # 重新启动服务
systemctl is-enabled nginx.service #查询服务是否开机启动
通过开机启动命令脚本实现开机自启
创建开机启动命令脚本文件:
vim /etc/init.d/nginx
在这个 nginx 文件中插入一下启动脚本代码,启动脚本代码来源网络复制,实测有效:
#! /bin/bash#
chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pidscript
NAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop(){
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
设置所有人都有对这个启动脚本 nginx 文件的执行权限:
chmod a+x /etc/init.d/nginx
把 nginx 加入系统服务中:
chkconfig --add nginx
把服务设置为开机启动:
chkconfig nginx on
reboot 重启系统后,就可以使用上面的 systemctl 方法相同的是:
systemctl start nginx.service # 启动nginx服务
systemctl enable nginx.service # 设置开机启动
systemctl disable nginx.service # 停止开机自启动
systemctl status nginx.service # 查看服务当前状态
systemctl restart nginx.service # 重新启动服务
systemctl is-enabled nginx.service #查询服务是否开机启动
如果服务启动的时候出现
Restarting nginx daemon: nginxnginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) nginx not running 的错误,通过 nginx -c 参数指定配置文件即可解决
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果服务启动中出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的错误,可以先通过 service nginx stop 停止服务,再启动就好。
欢迎大家提出不一样的观点,我们一起讨论,
我是辣个男人,一个运维人。
猜你喜欢
- 2024-10-10 nginx安装部署操作(nginx安装与配置详解)
- 2024-10-10 nginx端口怎么修改(nginx端口修改为80就成Welcome)
- 2024-10-10 快速扫盲容器和服务器之间的区别和联系
- 2024-10-10 使用 PM2 部署 ASP.NET Core 程序
- 2024-10-10 apache&http(apache官网)
- 2024-10-10 LayuiCMSluyun后台模板 拿来即用(layui后端注册登录界面)
- 2024-09-14 安装 Discuz! Q的三种方法(discuz安装使用教程)
- 2024-09-14 centos7稳定nginx搭建,快速搭建nginx
- 2024-09-14 Nginx教程:概念+安装+SSL安装,通过调优Nginx来提高应用性能
- 2024-09-14 Nginx的10个使用场景 # 强化学习(nginx常用功能)
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)