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. Use the systemctl command on systemd based version such as Ubuntu Linux 16.04LTS and above.
How can I stop or restart nginx web-server on a Ubuntu Linux LTS server?

Ubuntu Linux restart nginx

Type the following command:
sudo systemctl restart nginx
sudo service nginx restart
OR (older Ubuntu Linux version):
sudo /etc/init.d/nginx restart

Start / Restart / Stop Nginx Commands

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

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

One can use the following service command to restart or start or stop Nginx web server on an older version of Ubuntu server:

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


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

To view status of your Nginx server

Use any one of the following command:
sudo service status nginx
## OR ##
sudo systemctl status nginx
Sample outputs:

? 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
sudo systemctl reload nginx
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

Sample outputs:

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

It is also possible to use the systemd systemctl and journalctl commands for details on errors:
$ sudo systemctl status nginx.service
$ sudo journalctl -xe


You learned how to restart, start, stop the Nginx web server on Ubuntu Linux using command line options. See nginx home page here for more info.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Thanh Nguyen Jul 21, 2014 @ 16:33

    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 Mar 19, 2015 @ 3:37

      Same problem here. Any ideas?

      • 🐧 nixCraft Mar 19, 2015 @ 8:03

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

  • Glitchdata Oct 6, 2015 @ 22:30

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

  • Mallikarjunreddy Jul 27, 2016 @ 5:37

    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


    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
      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;
  • Ashok Nov 2, 2016 @ 10:28

    Check your hosts file at etc/hosts and reboot(restart) your system and run the below command as root(sudo)

    * Restarting nginx nginx [ OK ]
    xxx@xxx:~$ sudo nginx -s reload
    xxx@xxx:~$ nginx -t
    nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
    2016/11/02 15:52:51 [warn] 2855#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    2016/11/02 15:52:51 [emerg] 2855#0: open() "/run/nginx.pid" failed (13: Permission denied)
    nginx: configuration file /etc/nginx/nginx.conf test failed
    xxx@xxx:~$ sudo nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum