When you've compiled Nginx from source (typical path: /usr/local/nginx/sbin/nginx
), these are the most reliable restart approaches:
# Graceful restart (reloads config without dropping connections)
/usr/local/nginx/sbin/nginx -s reload
# Full restart (closes all active connections)
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
While killall nginx
works, it's considered abrupt because:
- Terminates worker processes immediately without finishing active requests
- May cause corruption if workers were writing logs or cache
- Bypasses Nginx's own shutdown sequence
For production systems, create a proper startup script. Example for Systemd:
# /etc/systemd/system/nginx.service
[Unit]
Description=NGINX Web Server
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
Then enable it:
sudo systemctl daemon-reload
sudo systemctl enable nginx
For older systems, use this classic init script:
#!/bin/sh
# Save as /etc/init.d/nginx
PATH=/usr/local/nginx/sbin:$PATH
case "$1" in
start)
nginx
;;
stop)
nginx -s quit
;;
restart)
nginx -s quit
nginx
;;
reload)
nginx -s reload
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
Always check status after restart:
ps aux | grep nginx
curl -I http://localhost
tail -f /usr/local/nginx/logs/error.log
When you compile Nginx from source, it doesn't automatically create the convenient service management scripts that package managers provide. This means common commands like service nginx restart
or systemctl restart nginx
won't be available.
The manual method you mentioned works, but could be improved:
# Current approach
killall nginx
/path/to/sbin/nginx
Potential issues with this method:
- Hard kill might drop active connections
- No process monitoring between steps
- Depends on killall being available
Method 1: Using Nginx's Built-in Signals
The proper way to restart Nginx is using signals:
# Graceful shutdown
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
# Start fresh
/usr/local/nginx/sbin/nginx
Alternatively, use the -s
option if you have direct access to the binary:
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx
Method 2: Creating a Simple Restart Script
Create /usr/local/bin/nginx-restart
:
#!/bin/sh
NGINX_BIN=/usr/local/nginx/sbin/nginx
PID_FILE=/usr/local/nginx/logs/nginx.pid
if [ -f $PID_FILE ]; then
$NGINX_BIN -s quit
sleep 1
fi
$NGINX_BIN
For production environments, consider creating a proper init script:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nginx init.d script
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
PIDFILE=/usr/local/nginx/logs/nginx.pid
case "$1" in
start)
echo -n "Starting $NAME: "
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON
echo "$NAME."
;;
stop)
echo -n "Stopping $NAME: "
start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
echo "$NAME."
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reloading $NAME configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE --exec $DAEMON
echo "$NAME."
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
;;
esac
exit 0
Remember that often you just need to reload configuration:
/usr/local/nginx/sbin/nginx -s reload
This is preferred over a full restart because:
- Maintains existing connections
- Zero downtime
- Preserves runtime state