When compiling Nginx 1.9.6 from source on Debian Jessie (8.2), you might encounter the service startup failure despite successful compilation. The key error message indicates systemd can't locate the service unit:
Failed to start nginx.service: Unit nginx.service failed to load: No such file or directory
Unlike package managers (apt/yum), source compilation doesn't automatically install systemd unit files. Here's how to verify:
# Check for existing unit files
ls -l /lib/systemd/system/nginx.service
ls -l /etc/systemd/system/nginx.service
Create a new service file at /etc/systemd/system/nginx.service
with these contents:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=myuser
Group=myuser
[Install]
WantedBy=multi-user.target
After creating the service file, execute these commands:
# Reload systemd manager configuration
systemctl daemon-reload
# Enable service startup on boot
systemctl enable nginx
# Start the service
systemctl start nginx
# Verify status
systemctl status nginx
Since you're running Nginx as non-root user (myuser), ensure proper permissions:
# Set ownership for PID directory
sudo mkdir -p /var/run/nginx
sudo chown myuser:myuser /var/run/nginx
# Update nginx.conf to match:
user myuser myuser;
pid /var/run/nginx/nginx.pid;
If problems persist, check compile-time dependencies:
# Essential build tools
sudo apt-get install build-essential
# Nginx dependencies
sudo apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
Recompile with verbosity for troubleshooting:
make clean
./configure [your_arguments_here] --with-debug
make -j4 2>&1 | tee compile.log
When compiling Nginx 1.9.6 from source on Debian Jessie (8.2), you might encounter this frustrating service startup error:
Failed to start nginx.service: Unit nginx.service failed to load: No such file or directory
This typically occurs because the systemd service file isn't properly registered after source compilation. Let's break down the solution.
First, create a proper systemd service file at /lib/systemd/system/nginx.service
:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=myuser
Group=myuser
[Install]
WantedBy=multi-user.target
After creating the service file, run these commands:
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
Check if Nginx is running properly:
systemctl status nginx
ps aux | grep nginx
curl -I localhost
If you prefer using the traditional init system, create this init script at /etc/init.d/nginx
:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/nginx.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
Then make it executable and register it:
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults