Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Install NGINX In Linux / Ubuntu And Managing


Share On     Share On WhatsApp     Share On LinkedIn


Many of you might be wondering how to install NGINX and manage it in Linux / Ubuntu OS. In this article I will walk you through the installation process and once the installation is done I will tell you how to manage the NGINX server.


In NGINX management you will learn how to check NGINX version, verbose output, test configuration, start, stop, restart & reload the NGINX configuration file.


NOTE: Also I will cover NGINX reload vs restart

Prerequisites


I hope you might have setup Ubuntu 18.04 server in Digital Ocean, Linode or any other cloud servers.


Step 1 - NGINX Installation


First update your OS package libraries using the following command


sudo apt-get update


Then run the following command to install the NGINX from Ubuntu package manager


sudo apt-get install nginx



Step 2 - Manage NGINX Server


NGINX Help List (-h)


This command lists all the available options for NGINX


sudo nginx -h



Check Installed NGINX Version & Exit (-v)


With this command you can check which version of NGINX is installed in your server


sudo nginx -v



NGINX Configurations Verbose Output (-V)


By default you wont get to know which all libraries are enabled with NGINX, to know installed libraries then use the following.


sudo nginx -V


You must get the output similar to the following


built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module



Test If NGINX Configuration Is Correct (-t)


sudo nginx -t


If everything is okay then you will get similar to the following output


#Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful



Test If NGINX Configuration Is Correct & Print The Configuration (-T)


With -t you will get to know if the configuration is proper or not. But -T NGINX it will print the configuration to console

sudo nginx -T



Check NGIXN Status


To check the NGINX status use the following command


sudo service nginx status


You will get similar output as below which tell the status of NGINX and even few recent logs of 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 2020-04-22 16:12:15 UTC; 3 days ago
   Docs: man:nginx(8)
 Main PID: 14023 (nginx)
  Tasks: 2 (limit: 1152)
  CGroup: /system.slice/nginx.service
      ├─ 838 nginx: worker process
      └─14023 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

Apr 22 16:12:15 stackcoder systemd[1]: Stopped A high performance web server and a reverse proxy server.
Apr 22 16:12:15 stackcoder systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 22 16:12:15 stackcoder systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Apr 22 16:12:15 stackcoder systemd[1]: Started A high performance web server and a reverse proxy server.
Apr 22 16:13:51 stackcoder systemd[1]: Reloading A high performance web server and a reverse proxy server.
Apr 22 16:13:51 stackcoder systemd[1]: Reloaded A high performance web server and a reverse proxy server.



Stop NGINX Server (stop)


sudo service nginx stop



Start NGINX Server (start)


sudo service nginx start



Reload NGINX Configuration (reload)


After modification of NGINX configuration you can start, stop, reload or restart NGINX configuration.


sudo service nginx reload



Restart NGINX Configuration (restart)


Similar to NGINX reload you can restart the NGINX configuration file with the following command


sudo service nginx restart



Difference Between NGINX reload vs restart


NGINX reload (Safer)


When you reload NGINX configuration file and if NGINX configuration has any errors or issues with the configuration settings then it will abort the restarting NGINX server with errors displayed to resolve those.



NGINX restart (Not Safer)


When you restart NGINX configuration file and if NGINX configuration has any errors or issues then it will stop the server and may not possibly restart NGINX server.


Conclusion


You have learnt how to install and play around with NGINX configuration file. You will also be interested in my other articles related to NGINX


How To Cache Static Files With NGINX Server

Redirect www to a non-www website or vice versa

How To Create Free SSL Certificate With Lets Encrypt/Certbot In Linux (Single / Multiple Domains)

How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu







Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing