配置nginx的systemctl管理方式
时间: 2023-11-09 16:07:36 浏览: 89
1. 创建nginx.service文件
在/etc/systemd/system/目录下,创建一个名为nginx.service的文件,具体命令如下:
```
sudo nano /etc/systemd/system/nginx.service
```
2. 编辑nginx.service文件
将以下内容复制到nginx.service文件中:
```
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
```
3. 重新加载systemctl配置文件
在终端中输入以下命令,重新加载systemctl配置文件:
```
sudo systemctl daemon-reload
```
4. 设置nginx开机自动启动
在终端中输入以下命令,设置nginx开机自动启动:
```
sudo systemctl enable nginx
```
5. 启动nginx
在终端中输入以下命令,启动nginx:
```
sudo systemctl start nginx
```
6. 检查nginx状态
在终端中输入以下命令,检查nginx状态:
```
sudo systemctl status nginx
```
如果nginx正在运行,输出结果应该类似以下内容:
```
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-10-22 07:48:36 UTC; 2s ago
Process: 1099 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 1100 (nginx)
CGroup: /system.slice/nginx.service
├─1100 nginx: master process /usr/sbin/nginx
└─1101 nginx: worker process
Oct 22 07:48:36 myserver systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Oct 22 07:48:36 myserver nginx[1099]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 22 07:48:36 myserver nginx[1099]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 22 07:48:36 myserver systemd[1]: Started The NGINX HTTP and reverse proxy server.
```
阅读全文