Error when starting Nginx

March 19, 2015 3.9k views

I have been following the instructions on this article:


When I enter the command on Step Seven—Start nginx I get the following error:

sudo service nginx start
/etc/init.d/nginx: 2: /etc/init.d/nginx: Syntax error: newline unexpected

When inspecting the /etc/init.d/nginx file it is just an html welcome page.

I'm a newbie to nginx and don't know how to resolve this. Can anyone provide some help?

6 Answers

Just to be clear, when you run cat /etc/init.d/nginx, the output is HTML?
If so, there's something wrong with your init.d script. And by wrong, I mean, completely bananas.

  • When I do a cat /etc/init.d/nginx I get:

    <title>Welcome to nginx!</title>
    <body bgcolor="white" text="black">
    <center><h1>Welcome to nginx!</h1></center>

    So, if this is the expected output I'm not sure why I get the error:
    /etc/init.d/nginx: 2: /etc/init.d/nginx: Syntax error: newline unexpected

    Any ideas on how to troubleshoot further?

Something is wrong with your installation, try start over.
also you can try: service nginx start

try again wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh my guess is you type it wrong first time and you got a 404 error, so thats what was saved to the init-deb.sh file

  • As you suggested I did:

    sudo wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
    --2015-03-20 15:07:03-- http://library.linode.com/assets/660-init-deb.sh
    Resolving library.linode.com (library.linode.com)...,,, ...
    Connecting to library.linode.com (library.linode.com)||:80... connected.
    HTTP request sent, awaiting response... 400 Bad Request
    2015-03-20 15:07:08 ERROR 400: Bad Request.

    It didn't look like it worked since there is a 400 Error, but just to be sure I did:

    vagrant@vagrant:/vagrant$ sudo service nginx start
    nginx: unrecognized service

    Any more suggestions on how to troubleshoot?

yes, you are getting an error 400 and that's what is storing on the file, no the real script. What is weird is I can access the file at http://library.linode.com/assets/660-init-deb.sh so you droplet must be having trouble accessing that host from linode.

So, I will recommend you installing Nginx from a Repo, there is reason why repos exist. is to have the latest and fully functional approved package by your OS maker, maybe not the newest, but is the one they know it works like it should on your OS. pretty sure ubuntu has a pretty recent version on its repos, I use CentOs and even with a not so new version of Nginx I can be sure is pretty reliable.

Now if you still want to do as some random person suggest and use scripts and stuff modified or made by others without knowing whats on it, here is the content of the file at http://library.linode.com/assets/660-init-deb.sh

#! /bin/sh

# 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


test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx

set -e

case "$1" in
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /opt/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile     /opt/nginx/logs/$NAME.pid \
              --exec $DAEMON
          echo "$NAME."
            echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
            exit 1

    exit 0

Just copy to the file manually and continue with the tutorial where you left.

Have another answer? Share your knowledge.