y0ngb00n
y0ngb00n

Reputation: 11

Nginx Not Port Listening when it runs as service

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.

Run Nginx through command

When l start the nginx with

$ sudo nginx

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

$ 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      -                   

No issue with wget as well,

$ 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’

    [ <=>                                                                                 ] 11          --.-K/s   in 0s      

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

Run Nginx through Systemd

However, when l start the nginx through systemd

$ sudo systemctl start nginx

Nothing is listening on the port 8000.

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

This is the result of wget

$ 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.

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

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.

The config file has passed the test

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

This is the main config file /etc/nginx/nginx.conf

$ 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;
}

This is the nginx config file /etc/nginx/conf.d/test_nginx.conf

# 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
    }
}

This is the nginx systemd config file

$ 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

Upvotes: 1

Views: 2722

Answers (2)

Moobie
Moobie

Reputation: 1654

This answer is specific to Docker.

I experienced the same issue I could run the nginx through the command but no through the service. on Docker (Debian).

The cause is that daemon tools (init.d, service, systemd) don't work on Docker by default. In Linux, the init process has to have PID 1. However, Docker doesn't run them as PID 1. PID 1 is occupied by dumb-init -- sh -c ...... which executes the CMD statement in you Docker config file. That was why my nginx didn't start as a service.

You can either 'hack' Docker to use systemd, which I don't think is a recommended practice at least according to what I've read on SO, or you could include the nginx start command sorta via terminal as in:

CMD ["sh", "-c", "systemctl start nginx (or "service nginx start" etc.) && (your original command)"]

Upvotes: 0

German Gomez
German Gomez

Reputation: 21

Probably SELinux is not allowing nginx to read the configs under /etc/nginx/sites-enabled/, I had the same problem when copying the configuration from another site.

chcon -R -t httpd_config_t /etc/nginx 

should fix it. If not check in /var/log/audit to see if there is any other problem related to SELinux

Upvotes: 2

Related Questions