Ubuntu Linux: Start / Restart / Stop Nginx Web Server

247
0

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. Use systemctl on systemd based version such as Ubuntu Linux 16.04LTS and above:

sudo systemctl restart nginx

OR

sudo service nginx restart

OR (older Ubuntu Linux version):

sudo /etc/init.d/nginx restart

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

sudo systemctl start nginx 
sudo systemctl stop nginx 
sudo systemctl restart nginx

OR

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

To view status of your server

Use any one of the following command:

sudo service status nginx

OR

sudo systemctl status nginx
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 Wed 2016-11-02 22:17:38 UTC; 2 days ago
  Process: 303 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0
  Process: 264 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited,
 Main PID: 304 (nginx)
   CGroup: /system.slice/nginx.service
           ??304 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ??305 nginx: worker process                           
           ??306 nginx: worker process                           
           ??307 nginx: worker process                           
           ??308 nginx: worker process                           
           ??309 nginx: worker process                           
           ??310 nginx: worker process                           
           ??311 nginx: cache manager process                    

Nov 02 22:17:38 newcbz systemd[1]: Starting A high performance web server and a reverse proxy se
Nov 02 22:17:38 newcbz systemd[1]: Started A high performance web server and a reverse proxy ser

A note about reload nginx server

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

OR

sudo systemctl reload nginx

OR

sudo service nginx 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

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

wrong username nginxf

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

It is also possible to use the systemd systemctl and journalctl commands for details on errors:

sudo systemctl status nginx.service
sudo journalctl -xe

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.