title: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument ubuntu 16.04 xenial
body: <p>I know this is a known bug, but i dont know how to resolve that because i do lot of googling and after that finally back to stack to get some real solution :</p>
  
  <p>Here are configuration:</p>
  
  <p>First i install nginx by:</p>
  
  <pre><code>sudo apt-get install nginx
  
  lsb_release -rd
  Description:    Ubuntu 16.04.2 LTS
  Release:        16.04
  
  apt-cache policy nginx-core
  nginx-core:
    Installed: 1.10.3-0ubuntu0.16.04.2
    Candidate: 1.10.3-0ubuntu0.16.04.2
    Version table:
   *** 1.10.3-0ubuntu0.16.04.2 500
          500 http://sg.mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 Packages
          500 http://sg.mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 Packages
          100 /var/lib/dpkg
  
  /status
       1.9.15-0ubuntu1 500
          500 http://sg.mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 Packages
  </code></pre>
  
  <p>My nginx service config is like:</p>
  
  <pre><code>nano /lib/systemd/system/nginx.service
  
  # Stop dance for nginx
  # =======================
  #
  # ExecStop sends SIGSTOP (graceful stop) to the nginx process.
  # If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
  # and sends SIGTERM (fast shutdown) to the main process.
  # After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
  # SIGKILL to all the remaining processes in the process group (KillMode=mixed).
  #
  # nginx signals reference doc:
  # http://nginx.org/en/docs/control.html
  #
  [Unit]
  Description=A high performance web server and a reverse proxy server
  After=network.target
  
  [Service]
  Type=forking
  PIDFile=/run/nginx.pid
  ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
  ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
  ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
  ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
  TimeoutStopSec=5
  KillMode=mixed
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>My nginx.conf file is like:
       nano /etc/nginx/nginx.conf</p>
  
  <pre><code>user www-data;
  worker_processes auto;
  pid /run/nginx.pid;
  
  events {
          worker_connections 768;
          # multi_accept on;
  }
  
  http {
  
          ##
          # Basic Settings
          ##
  
          sendfile on;
          tcp_nopush on;
          tcp_nodelay on;
          keepalive_timeout 65;
          types_hash_max_size 2048;
          # server_tokens off;
  
          # server_names_hash_bucket_size 64;
          # server_name_in_redirect off;
  
          include /etc/nginx/mime.types;
          default_type application/octet-stream;
  
          ##
          # SSL Settings
          ##
   tcp_nodelay on;
          keepalive_timeout 65;
          types_hash_max_size 2048;
          # server_tokens off;
  
          # server_names_hash_bucket_size 64;
          # server_name_in_redirect off;
  
          include /etc/nginx/mime.types;
          default_type application/octet-stream;
  
          ##
          # SSL Settings
          ##
  
          ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
          ssl_prefer_server_ciphers on;
  
          ##
          # Logging Settings
          ##
  
          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log;
  
          ##
          # Gzip Settings
          ##
  
          gzip on;
          gzip_disable "msie6";
    ##
          # Logging Settings
          ##
  
          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log;
  
          ##
          # Gzip Settings
          ##
  
          gzip on;
          gzip_disable "msie6";
  
          # gzip_vary on;
          # gzip_proxied any;
          # gzip_comp_level 6;
          # gzip_buffers 16 8k;
          # gzip_http_version 1.1;
          # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  
          ##
          # Virtual Host Configs
          ##
  
          include /etc/nginx/conf.d/*.conf;
          include /etc/nginx/sites-enabled/*;
  }
  
  
  #mail {
  # gzip_comp_level 6;
          # gzip_buffers 16 8k;
          # gzip_http_version 1.1;
          # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  
          ##
          # Virtual Host Configs
          ##
  
          include /etc/nginx/conf.d/*.conf;
          include /etc/nginx/sites-enabled/*;
  }
  
  
  #mail {
  #       # See sample authentication script at:
  #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  #
  #       # auth_http localhost/auth.php;
  #       # pop3_capabilities "TOP" "USER";
  #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
  #
  #       server {
  #               listen     localhost:110;
  #               protocol   pop3;
  #               proxy      on;
  #       }
  #
  #       server {
  #               listen     localhost:143;
  #               protocol   imap;
  #               proxy      on;
  </code></pre>
  
  <p>When i start nginx server i get 502 error on my web page and status of the nginx is:</p>
  
  <pre><code> sudo service nginx status
  sudo: unable to resolve host iZt4ndf6hh48rtirr7qry9Z
  ● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2017-10-15 16:34:10 CST; 4h 42min ago
    Process: 2682 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 2703 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 2700 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2706 (nginx)
     CGroup: /system.slice/nginx.service
             ├─2706 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
             └─2707 nginx: worker process
  
  Oct 15 16:34:10 iZt4ndf6hh48rtirr7qry9Z systemd[1]: Starting A high performance web server and a reverse proxy server...
  Oct 15 16:34:10 iZt4ndf6hh48rtirr7qry9Z systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
  Oct 15 16:34:10 iZt4ndf6hh48rtirr7qry9Z systemd[1]: Started A high performance web server and a reverse proxy server.
  </code></pre>
  
  <p>Now i search on google and find that pid bug but i try to resolve that error but i am unable to do so.</p>
  
  <p>Thanks for reading and your precious time.</p>