You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dev-protocol/http/nginx/Nginx系统服务配置.md

61 lines
1.8 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Nginx 系统服务配置
## 1. Nginx配置成系统服务
- 把Nginx应用服务设置成为系统服务方便对Nginx服务的启动和停止等相关操作具体实现步骤:
- (1) 在`/usr/lib/systemd/system`目录下添加nginx.service,内容如下:
```shell
vim /usr/lib/systemd/system/nginx.service
```
- 具体内容
```shell
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.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
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
```
- (2)添加完成后如果权限有问题需要进行权限设置
```shell
chmod 755 /usr/lib/systemd/system/nginx.service
```
- (3)使用系统命令来操作Nginx服务
```shell
启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx
```
## 2. Nginx命令配置到系统环境
- Nginx安装目录下的二级制可执行文件`nginx`的很多命令要想使用这些命令前提是需要进入sbin目录下才能使用很不方便
如何去优化我们可以将该二进制可执行文件加入到系统的环境变量这样的话在任何目录都可以使用nginx对应的相关命令。具体实现步骤如下:
- (1)修改`/etc/profile`文件
```shell
vim /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
```
- (2)使之立即生效
```shell
source /etc/profile
```
- (3)执行nginx命令
```shell
nginx -V
```