Nginx Not Port Listening when it runs as service
<p>l'm configuring Nginx on my CentOS 7. l could run the nginx through the command but no through the service. l appreciate any help.</p>

<p><strong>Run Nginx through command</strong></p>

<p>When l start the nginx with</p>

<pre><code>$ sudo nginx
</code></pre>

<p>l could see the port is listening, and l've connected to nginx with lynx successfully.</p>

<pre><code>$ netstat -nap | grep 8000
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      -                   
</code></pre>

<p>No issue with wget as well,</p>

<pre><code>$ wget http://127.0.0.1:8000
--2016-04-05 13:33:01--  http://127.0.0.1:8000/
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.2’

    [ &lt;=&gt;                                                                                 ] 11          --.-K/s   in 0s      

2016-04-05 13:33:01 (1.53 MB/s) - ‘index.html.2’ saved [11]
</code></pre>

<p><strong>Run Nginx through Systemd</strong></p>

<p>However, when l start the nginx through systemd</p>

<pre><code>$ sudo systemctl start nginx
</code></pre>

<p>Nothing is listening on the port 8000.</p>

<pre><code>$ netstat -nap | grep 8000
(No info could be read for "-p": geteuid()=1000 but you should be root.)
</code></pre>

<p>This is the result of wget</p>

<pre><code>$ wget http://127.0.0.1:8000
--2016-04-05 13:34:52--  http://127.0.0.1:8000/
Connecting to 127.0.0.1:8000... failed: Connection refused.
</code></pre>

<p>l've checked the error log (/var/log/nginx/error.log), </p>

<pre><code>Apr  5 12:57:24 localhost systemd: Starting The NGINX HTTP and reverse proxy server...
Apr  5 12:57:24 localhost nginx: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr  5 12:57:24 localhost nginx: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr  5 12:57:24 localhost systemd: Failed to read PID from file /var/run/nginx.pid: Invalid argument
Apr  5 12:57:24 localhost systemd: Started The NGINX HTTP and reverse proxy server.
</code></pre>

<p>The config file has passed the test</p>

<pre><code>$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
</code></pre>

<p>This is the main config file <code>/etc/nginx/nginx.conf</code></p>

<pre><code>$ cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
</code></pre>

<p>This is the nginx config file <code>/etc/nginx/conf.d/test_nginx.conf</code></p>

<pre><code># the upstream component nginx needs to connect to
upstream django {
    server 0.0.0.0:8001; 
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;

    # the domain name it will serve for
    server_name 0.0.0.0; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /static {
        alias /src/frontend/DjangoServer/static;
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /src/frontend/DjangoServer/uwsgi_params; # the uwsgi_params file you installed
    }
}
</code></pre>

<p>This is the nginx systemd config file</p>

<pre><code>$ cat /etc/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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
</code></pre>