Linux下Nginx编译安装后的开机自启动设置

举报
江湖有缘 发表于 2023/01/18 17:42:38 2023/01/18
【摘要】 Linux下Nginx编译安装后的开机自启动设置

一、查看当前Nginx启动状态

[root@node1 sbin]# ps -ef |grep nginx
root      39424      1  0 14:01 ?        00:00:00 nginx: master process ./nginx
nobody    39425  39424  0 14:01 ?        00:00:00 nginx: worker process
root      40313   1884  0 15:41 pts/0    00:00:00 grep --color=auto nginx

二、而配置Nginx相关服务文件

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
   
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
   
[Install]
WantedBy=multi-user.target

三 、设置nginx命令

[root@node1 sbin]# cat  ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin


export PATH
[root@node1 sbin]# source ~/.bash_profile 
[root@node1 sbin]# nginx -v
nginx version: nginx/1.18.0
[root@node1 sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

四、设置开机启动

root@node1 ~]# systemctl enable --now  nginx.service
[root@node1 ~]# ps -ef |grep nginx
root        879      1  0 16:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody      881    879  0 16:17 ?        00:00:00 nginx: worker process
root       1738   1645  0 16:20 pts/0    00:00:00 grep --color=auto nginx
[root@node1 ~]# 

五、测试开机启动

[root@node1 ~]# systemctl status nginx
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-05-08 16:17:54 CST; 45s ago
  Process: 872 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 879 (nginx)
    Tasks: 2 (limit: 12404)
   Memory: 2.5M
   CGroup: /system.slice/nginx.service
           ├─879 nginx: master process /usr/local/nginx/sbin/nginx
           └─881 nginx: worker process

May 08 16:17:54 node1 systemd[1]: Starting nginx...
May 08 16:17:54 node1 systemd[1]: Started nginx.

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。