linux nignx自启
时间: 2023-12-09 17:36:56 浏览: 24
以下是在Linux中设置Nginx开机自启的步骤:
1. 在/usr/lib/systemd/system目录下创建nginx.service文件,命令如下:
```shell
sudo vim /usr/lib/systemd/system/nginx.service
```
2. 在nginx.service文件中输入以下内容:
```shell
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target 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
[Install]
WantedBy=multi-user.target
```
3. 保存并退出文件。
4. 启用Nginx服务,命令如下:
```shell
sudo systemctl enable nginx.service
```
5. 启动Nginx服务,命令如下:
```shell
sudo systemctl start nginx.service
```
6. 验证Nginx服务是否已经启动,命令如下:
```shell
sudo systemctl status nginx.service
```
如果服务已经启动,将会显示active (running)字样。