How can I install Nginx web server On CentOS Linux 6 or Red Hat Enterprise Linux 6 using the yum command?

nginx is a free and open-source high-performance web server. This page shows how to install Nginx on a CentOS 6 or RHEL 6 using yum command. Recently, nginx web project started to distribute binary packages using nginx yum repository. You can either create /etc/yum.repos.d/nginx.repo or directly install rpm package. This package contains yum configuration file and a public PGP key necessary to authenticate signed RPMs.

How to install nginx on CentOS 6 or RHEL 6 with yum

The procedure is as follows:

  1. Update CentOS/RHEL 6 system by running yum update
  2. Install official nginx repo
  3. To install nginx, run: yum install nginx
  4. Configure nginx service
  5. Update firewall configuration to open port 80
  6. To get nginx running and start on a CentOS 6 or RHLE 6, run: service nginx start

Let us see all examples in details.

Step 1: Install nginx repo

Type the following wget command to install nginx yum configuration file:
# cd /tmp
CentOS Linux v6.x user type the following command:
# wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

Sample outputs:

warning: nginx-release-centos-6-0.el6.ngx.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ########################################### [100%] 1:nginx-release-centos ########################################### [100%]

RHEL v6.x user type the following command:

# wget http://nginx.org/packages/rhel/6/noarch/RPMS/nginx-release-rhel-6-0.el6.ngx.noarch.rpm
# rpm -ivh nginx-release-rhel-6-0.el6.ngx.noarch.rpm

Sample outputs:

warning: nginx-release-rhel-6-0.el6.ngx.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ########################################### [100%] 1:nginx-release-rhel ########################################### [100%]

Step 2: Install nginx web-server on CentOS 6 or RHEL 6

Type the following yum command to install nginx web-server:
# yum install nginx
Sample outputs:

Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile * base: mirror.horizon.vn * extras: centos-hcm.viettelidc.com.vn * updates: centos-hcm.viettelidc.com.vn
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.16.0-1.el6.ngx will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size
================================================================================
Installing: nginx x86_64 1.16.0-1.el6.ngx nginx 971 k Transaction Summary
================================================================================
Install 1 Package(s) Total download size: 971 k
Installed size: 2.6 M
Is this ok [y/N]: y
Downloading Packages:
nginx-1.16.0-1.el6.ngx.x86_64.rpm | 971 kB 00:00 Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum. Installing : nginx-1.16.0-1.el6.ngx.x86_64 1/1 ---------------------------------------------------------------------- Thanks for using nginx! Please find the official documentation for nginx here:
* http://nginx.org/en/docs/ Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html Commercial subscriptions for nginx are available on:
* http://nginx.com/products/ ---------------------------------------------------------------------- Verifying : nginx-1.16.0-1.el6.ngx.x86_64 1/1 Installed: nginx.x86_64 0:1.16.0-1.el6.ngx Complete!

How do I verify and display nginx version?

Simply type the following command:
# nginx -v
nginx version: nginx/1.16.0

Step 3: Turn on nginx web service

Type the following chkconfig command:
# chkconfig nginx on

How do I start / stop / restart nginx web-server?

Type the following commands:
# service nginx start
# service nginx stop
# service nginx restart
# service nginx status
# service nginx reload

How To Install nginx on CentOS 6 with yum

Step 4: Configuration files

  1. Default configuration directory: /etc/nginx/
  2. Default SSL and vhost config directory: /etc/nginx/conf.d/
  3. Default log file directory: /var/log/nginx/
  4. Default document root directory: /usr/share/nginx/html
  5. Default configuration file: /etc/nginx/nginx.conf
  6. Default server access log file: /var/log/nginx/access.log
  7. Default server access log file: /var/log/nginx/error.log

To edit the nginx configuration file, enter:
# vi /etc/nginx/nginx.conf
Set or update worker_processes as follows (this must be set to CPU(s) in your system. Use the lscpu | grep ‘^CPU(s)’ command to list the number of CPUs in the server)

worker_processes 2;

Turn on gzip support:

Save and close the file. Edit the file /etc/nginx/conf.d/default.conf, enter:
# vi /etc/nginx/conf.d/default.conf
Set IP address and TCP port number:

 listen 202.54.1.1.1:80;

Set server name:

 server_name www.cyberciti.biz;

Save and close the file. Start or restart the nginx server:
# service nginx restart
Verify that everything is working:
# netstat -tulpn | grep :80
# ps aux | grep nginx

Firewall configuration: Open TCP port # 80

Edit the file /etc/sysconfig/iptables, enter:
# vi /etc/sysconfig/iptables
Add the following lines, ensuring that they appear before the final LOG and DROP lines for the INPUT chain to open port 80:

-A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

Save and close the file. Finally, restart the firewall:
# service iptables restart

How do I configure nginx+php5?

See this tutorial for more information.

How do I configure Nginx as a reverse proxy server?

See our previous tutorials:

  1. CentOS / Redhat Linux: Install Keepalived To Provide IP Failover For Web Cluster
  2. CentOS / Redhat: Install nginx As Reverse Proxy Load Balancer
  3. Handling nginx Failover With KeepAlived
  4. nginx: Setup SSL Reverse Proxy (Load Balanced SSL Proxy)
  5. mod_extforward: Lighttpd Log Clients Real IP Behind Reverse Proxy / Load Balancer
  6. HowTo: Merge Apache / Lighttpd / Nginx Server Log Files
  7. Linux nginx: Chroot (Jail) Setup

How do I secure nginx web-server?

  1. See: Top 20 Nginx webServer best security practices blog post for more information.

Check out related media

This tutorial is also available in a quick video format.



(Video 01: HowTo: Install Nginx On Centos / Red Hat Enterprise Linux 6.x )

Conclusion

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Similar Posts