By y0ngb00n


2016-04-05 03:41:53 8 Comments

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

2 comments

@Moobie 2019-01-25 03:09:46

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)"]

@German Gomez 2017-01-03 03:40:35

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

@datdinhquoc 2019-03-08 04:33:42

crappy SELinux, i have to disable it every time :(

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 329469 View
  • 935 Score
  • 11 Answer
  • Tags:   node.js nginx concept

1 Answered Questions

[SOLVED] How nginx pick the configuration order?

  • 2016-07-14 09:46:43
  • codemilan
  • 1173 View
  • 2 Score
  • 1 Answer
  • Tags:   nginx

0 Answered Questions

How to uninstall Nginx which is installed from its Source Code?

  • 2018-12-08 10:56:55
  • vivek._.s
  • 301 View
  • 0 Score
  • 0 Answer
  • Tags:   nginx

2 Answered Questions

[SOLVED] can't interpret PHP on RHEL + nginx

  • 2017-06-08 17:33:56
  • Night
  • 105 View
  • 1 Score
  • 2 Answer
  • Tags:   php linux nginx

1 Answered Questions

nginx enable authentication on specific port

1 Answered Questions

[SOLVED] docker with nginx and gunicorn not opening in browser

  • 2018-06-04 14:16:42
  • Anuj TBE
  • 167 View
  • 0 Score
  • 1 Answer
  • Tags:   docker nginx

2 Answered Questions

My NIGINX server is down. How do I restart it? I am fairly new to NGINX and CentOS

  • 2016-06-29 14:33:14
  • Firdous Alam
  • 4317 View
  • 0 Score
  • 2 Answer
  • Tags:   nginx centos

1 Answered Questions

1 Answered Questions

Django + uwsgi + nginx + CentOS 7 : connection refused on 8001 port

1 Answered Questions

Connect to nginx (VirtualBox, Fedora) from Windows host

  • 2012-04-12 11:10:49
  • Guard
  • 945 View
  • 1 Score
  • 1 Answer
  • Tags:   nginx virtualbox

Sponsored Content