在Ubuntu系统中,我们可以使用`update-rcd`命令来实现Nginx的开机自启动。首先,我们需要在`/etc/init.d/`目录下创建一个Nginx的脚本文件。可以通过以下命令创建:
```bash
sudo vi /etc/init.d/nginx
```
然后输入以下内容:
```bash
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: enable service provided by nginx
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
PIDFILE=/var/run/$NAME.pid
USER=www-data
GROUP=www-data
LIBEXECDIR=/libexec
ARGS="--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log"
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --make-pidfile --user $USER --group $GROUP --chuid $USER --startas $DAEMON --exec $DAEMON -- $ARGS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --retry 10
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --retry 10
sleep 1 # wait for processes to be killed before starting again (otherwise they will just start back up immediately)
start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --make-pidfile --user $USER --group $GROUP --chuid $USER --startas $DAEMON --exec $DAEMON -- $ARGS
echo "$NAME."
;;
*)
echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
exit 0
```
保存并退出。接下来,给脚本添加可执行权限:
```bash
sudo chmod +x /etc/init.d/nginx
```
现在,Nginx已经设置为开机自启动。你可以使用以下命令查看Nginx的状态:
```bash
sudo service nginx status
```