
Nginx is a free, high performance and open-source HTTP and reverse proxy server. It can be used as a standalone web server, and as a reverse proxy for Apache and other web servers. It is a more flexible and lightweight program than Apache HTTP Server that’s why it powers some of the largest sites on the Internet. Nginx can handle the bigger amount of connection than Apache and consuming smaller memory. In this guide, we will show important and frequently used Nginx commands.
Prerequisites
You should logged in with a non-root sudo enabled user account with any Linux distro such as Ubuntu 18.04 and CentOS 7 and Debian 9.
Install Nginx Server
First of all if you want to install Nginx then following commands for different distro:
sudo yum install epel-release && yum install nginx #For CentOS/RHEL
sudo dnf install nginx #For Debian/Ubuntu
sudo apt install nginx #For Fedora
Check Nginx Version
You can check installed version of Nginx on your system using below command:
nginx -v
It will show output as below:
Output
nginx version: nginx/1.14.0 (Ubuntu)
Above command will only show version number. If you want to show version and configuration details then run below command:
nginx -V
Output
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.0g 2 Nov 2017 (running with OpenSSL 1.1.1b 26 Feb 2019)
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/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
Check Nginx Configuration Syntax
It’s best practice to check configuration syntax of nginx when you have made changes in configuration file. Run the below command to check:
sudo nginx -t
It will display output as below:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Start Nginx
It’s a very simple to start nginx service. Use the below command to start it:
sudo systemctl start nginx
If you Nginx configuration is correct then it will start and will not display any output. If your Linux distribution have not systemd then you should use below command to start service:
sudo service start nginx
Enable Nginx Service
Nginx start command will start the service only for a while. If you don’t want to start service manually each time then you can enable auto-start at boot time using below command:
sudo systemctl enable nginx
Same as above command, if your Linux distribution have not systemd then you should use below command:
sudo service enable nginx
Stop Nginx
If you have requirement to stop Nginx service for any reason you can run below command:
sudo systemctl stop nginx
For non systemd system execute below command:
sudo service nginx stop
Restart Nginx
The restart option is a quick way of stopping and then starting the Nginx server.
Following is command to perform an Nginx restart for systemd and non systemd accordingly:
sudo systemctl restart nginx
sudo service restart nginx
This command is most frequently used for Nginx service.
Reload Nginx
When you have made any changes to Nginx configuration files then you need to reload Nginx service. It will load new configuration and start new process and stop all old processes.
To reload Nginx, use one of the following commands:
sudo systemctl reload nginx
Run below command if not systemd available on system:
sudo service reload nginx
View Nginx Service Status
You can check the status of Nginx service using status command. It will show the run time status information about the service.
Use below command to check status:
sudo systemctl status nginx #systemd
Output
● 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 Mon 2019-04-29 04:57:52 UTC; 1 weeks 0 days ago
Docs: man:nginx(8)
Main PID: 9561 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service
├─9561 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─9564 nginx: worker process
Apr 29 04:57:51 tecnstuff.net systemd[1]: Stopped A high performance web server and a reverse proxy server.
Apr 29 04:57:51 tecnstuff.net systemd[1]: Starting A high performance web server and a reverse proxy server…
Apr 29 04:57:52 tecnstuff.net systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Apr 29 04:57:52 tecnstuff.net systemd[1]: Started A high performance web server and a reverse proxy server.
Conclusion
Here, you learned use of most used Nginx commands. You can learn more about Nginx command line at Nginx documentation.
If you have any questions or suggestion, feel free to leave a comment below.
Leave a Reply