How to Install Nginx Web Server on Linux

Nginx (pronounced “engine X”). NGINX is a free, open-source, high-performance HTTP web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

NGNX is used by millions of websites, including major companies such as Airbnb, Box, Dropbox, Netflix, Tumbler and WordPress.

Also read the following useful tips for Nginx.

1) How to Install Nginx on Linux

The latest version of Nginx can be easily installed on Linux systems from the official NGINX repository.

Installing Nginx from the distribution official repository is the quickest way, but the package that is normally provided is outdated.

How to Install Nginx on CentOS 6/7/8 Systems

Add the following official NGINX repository and use the yum command to install the latest version of Nginx on the CentOS system.

# vi /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Update the repository

# yum update

Install the Nginx on CentOS 6/7.

# yum install nginx

Install the Nginx on CentOS 8.

# dnf install nginx

1a) How to Install Nginx on RHEL 6/7/8 Systems

Add the following official NGINX repository and use the yum command to install the latest version of Nginx on the Red Hat system.

# vi /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1

Update the repository

# yum update

Install the Nginx on RHEL 6/7.

# yum install nginx

Install Nginx on the Red Hat 8 system with the help of the dnf command.

# dnf install nginx

1b) How to Install Nginx on Ubuntu

Add the following official NGINX repository and use the apt-get command to install the latest version of Nginx on the Ubuntu system.

$ sudo sh -c "echo 'deb https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx' >> /etc/apt/sources.list"
$ sudo sh -c "echo 'deb-src https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx' >> /etc/apt/sources.list"
$ curl http://nginx.org/keys/nginx_signing.key | apt-key add -
$ sudo apt-get update
$ sudo apt-get install nginx

1c) How to Install Nginx on Debian

Add the following official NGINX repository and use the apt command to install the latest version of Nginx on the Debian system.

$ sudo sh -c "echo 'deb https://nginx.org/packages/mainline/debian/ `lsb_release -cs` nginx' >> /etc/apt/sources.list"
$ sudo sh -c "echo 'deb-src https://nginx.org/packages/mainline/debian/ `lsb_release -cs` nginx' >> /etc/apt/sources.list"
$ curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
$ sudo apt update
$ sudo apt install nginx

For Arch Linux systems, use the pacman command to install Nginx. You can always get the latest version of Nginx on the Arch Linux system, as this is a rolling release distribution.

$ sudo pacmna -S nginx

1d ) How to Install Nginx on openSUSE & Fedora Systems

I haven’t seen the Nginx repository for Fedora and openSUSE systems, so you can install it from the distribution official repository.

For Fedora system, use the dnf command to install Nginx.

$ sudo dnf install -y nginx

For openSUSE systems, use the zypper command to install Nginx.

$ sudo zypper install -y nginx

2) How to Allow the Nginx port 80 and 443 on Linux

Once you have installed the Nginx web server, you must allow the following tcp ports in the firewall to work Nginx.

For ufw firewall.

$ sudo ufw allow 80/tcp comment 'accept Apache'
$ sudo ufw allow 443/tcp comment 'accept HTTPS connections'

or

$ sudo ufw allow 'Nginx Full'

For iptables.

$ sudo iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$ sudo iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
$ sudo service iptables save

For firewalld.

# firewall-cmd --zone=public --permanent --add-service={http,https}
# firewall-cmd --reload

3) How to Start Nginx Service on Linux

Use the below commands to start Nginx server in Linux.

For SysVinit systems.

# service nginx start
or
# /etc/init.d/nginx start

For systemd systems.

# systemctl start nginx.service
or
# systemctl start nginx

4) How to Enable Nginx Service On Boot in Linux

Use the below commands to enable Nginx service on boot in Linux.

For SysVinit systems.

# chkconfig nginx on

For systemd systems.

# systemctl enable nginx.service
or
# systemctl enable nginx

5) How to Check Nginx Version on Linux

Run the command below to check the version of Nginx installed on your Linux system. You can get a different version in the output, depending on your distribution and installation method.

$ nginx -v

nginx version: nginx/1.13.8

Finally, test your Nginx web server installation on Linux.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...