≡ Menu

Ubuntu Linux: Start / Restart / Stop Nginx Web Server

How do I restart / stop / start the nginx web server under a Ubuntu Linux operating systems using command line option?


The nginx web server can be restarted using any one of the following command line syntax:

sudo service nginx restart

OR

sudo /etc/init.d/nginx restart

The same commands can be used to start / stop / restart the nginx server on a Ubuntu Linux:

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

OR

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

It is also possible to use the following syntax to reload nginx server after you made changes to the config file such as nginx.conf:
sudo nginx -s reload

Dealing with error messages on screen

If the nginx server failed to start or stop or restart, check for the syntax error:

nginx -t
## OR set path to config file and test for the errors ##
nginx -c /etc/nginx/nginx.conf -t

Sample outputs:

Fig.01: Fixing config file errors

Fig.01: Fixing config file errors


The following error indicate that the username “nginxf” does not exists on line # 2:

nginx: [emerg] getpwnam(“nginxf”) failed in /etc/nginx/nginx.conf:2

To fix this error, edit the file with a text editor such as vi/vim/joe etc:

sudo vi /etc/nginx/nginx.conf
 
## or edit and jump to line no. 2 ###
sudo vi +2 /etc/nginx/nginx.conf

Update config file. Save and close the file. Test is again:

nginx -t

I also recommend that you check for nginx server log files for more info:

sudo tail -f /var/log/nginx/error.log 
2015/03/19 02:49:30 [emerg] 43130#0: getpwnam("nginxf") failed in /etc/nginx/nginx.conf:2
2015/03/19 02:49:44 [emerg] 43145#0: getpwnam("nginxf") failed in /etc/nginx/nginx.conf:2
Share this tutorial on:
{ 5 comments… add one }
  • Thanh Nguyen July 21, 2014, 4:33 pm

    Hi. i just use command: sudo service nginx restart but i get an error
    * Restarting nginx nginx [fail]
    Please help me how to solve? thanks

    • Clayton March 19, 2015, 3:37 am

      Same problem here. Any ideas?

      • nixCraft March 19, 2015, 8:03 am

        Check for error.log file:
        tail -f /var/log/nginx/error.log
        or
        nginx -t

  • Glitchdata October 6, 2015, 10:30 pm

    Hi! This is really useful. Do you have an ansible equivalent of these commands?

  • Mallikarjunreddy July 27, 2016, 5:37 am

    Hi this error due to default configuration file issue, please check default file is correctly configured or not. path:check /etc/nginx/sites-available/defalut

    Ex:

    server {
      listen   80;
      root /usr/share/nginx/html;
      index index.php index.html index.htm;
      server_name example.com;
      location / {
        try_files $uri $uri/ /index.html;
      }
      error_page 404 /404.html;
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root /usr/share/nginx/www;
      }
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
    }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , ,