目录
Nginx配置成系统服务
之前每次想要启动、停止、重启Nginx时需要先进入sbin目录下才能执行相应的命令来操作Nginx,比较麻烦,我们可以通过将Nginx配置成系统服务的方式来简化这些操作。
配置方式
在/usr/lib/systemd/system目录下添加nginx.service,内容如下:
[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
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
如果权限不足可以赋权
chmod 755 /usr/lib/systemd/system/nginx.service
使用
启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx
Nginx命令配置到系统环境
我们在根目录执行 nginx -V查看nginx命令,会发现报如下错误:
-bash: nginx: command not found
为了在任意目录执行nginx的所有命令我们需要配置一下环境变量。
配置
修改/etc/profile文件
vim /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
执行 source /etc/profile 使配置生效。
使用
nginx -V