Nginx-配置系统服服务&环境变量

Nginx配置成系统服务

把Nginx应用服务设置成为系统服务,方便对Nginx服务的启动和停止等相关操作,具体实现步骤:

  1. 在/usr/lib/systemd/system目录下添加nginx.service文件,内容如下:
    vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. 添加完成后如果权限有问题需要进行权限设置
    chmod 777 /usr/lib/systemd/system/nginx.service

  2. 使用系统命令来操作Nginx服务

开启nginx服务 systemctl start nginx
停止nginx服务 systemctl stop nginx
重启nginx服务 systemctl restart nginx
查看nginx服务 systemctl status nginx
加入开机自启 systemctl enable nginx
退出开机自启 systemctl disable nginx

Nginx命令配置到系统环境

前面我们介绍过Nginx安装目录下的二级制可执行文件nginx的很多命令,要想使用这些命令前提是需要进入sbin目录下才能使用,很不方便,如何去优化,我们可以将该二进制可执行文件加入到系统的环境变量,这样的话在任何目录都可以使用nginx对应的相关命令。具体实现步骤如下:

  1. 推荐在/etc/profiel.d/目录下新建文件 nginx.sh 文件,内容如下:
    vim /etc/profiel.d/nginx.sh
export NGINX_HOME=/usr/local/nginx/sbin
export PATH=$PATH:$NGINX_HOME
  1. 刷新,使之立即生效
source /etc/profile
  1. 执行nginx命令
nginx -V
posted @   BEJSON  阅读(349)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示