Tutorial details | |
---|---|
Difficulty | Intermediate (rss) |
Root privileges | Yes |
Requirements | CentOS/RHEL yum |
Time | N/A |
How to install nginx on CentOS 6 or RHEL 6 with yum
The procedure is as follows:
- Update CentOS/RHEL 6 system by running yum update
- Install official nginx repo
- To install nginx, run: yum install nginx
- Configure nginx service
- Update firewall configuration to open port 80
- 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
Step 4: Configuration files
- Default configuration directory: /etc/nginx/
- Default SSL and vhost config directory: /etc/nginx/conf.d/
- Default log file directory: /var/log/nginx/
- Default document root directory: /usr/share/nginx/html
- Default configuration file: /etc/nginx/nginx.conf
- Default server access log file: /var/log/nginx/access.log
- 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:
gzip on;
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:
- CentOS / Redhat Linux: Install Keepalived To Provide IP Failover For Web Cluster
- CentOS / Redhat: Install nginx As Reverse Proxy Load Balancer
- Handling nginx Failover With KeepAlived
- nginx: Setup SSL Reverse Proxy (Load Balanced SSL Proxy)
- mod_extforward: Lighttpd Log Clients Real IP Behind Reverse Proxy / Load Balancer
- HowTo: Merge Apache / Lighttpd / Nginx Server Log Files
- Linux nginx: Chroot (Jail) Setup
How do I secure nginx web-server?
- 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
- CentOS / Redhat Linux: Install Keepalived To Provide IP Failover For Web Cluster
- CentOS / Redhat: Install nginx As Reverse Proxy Load Balancer
- Handling nginx Failover With KeepAlived
- nginx: Setup SSL Reverse Proxy (Load Balanced SSL Proxy)
- mod_extforward: Lighttpsd Log Clients Real IP Behind Reverse Proxy / Load Balancer
- HowTo: Merge Apache / Lighttpsd / Nginx Server Log Files
- Linux nginx: Chroot (Jail) Setup
- HowTo: SPDY SSL Installation and Configuration
- Install Nginx Using Yum Command on CentOS/RHEL
- Create a Self-Signed SSL Certificate on Nginx
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 7 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
The following 2 steps stated in your article:
can be made into one , like below:
its better to first download the file and check.
I don’t see you mentioned any checks in this web page.
I have to deploy a static web site but I cant figure out where place my files to be accessible by nginx. Usual /var/www folder and www-data user do not exist in my CentOS instance, to be precise I am using Amazon linux AMI that is based on CentOS.
cd to /usr/share/nginx/html/ and put your files:
I’m not getting anything, there is something missing. I am seeing a lot of talk about adding a “index.php” to the other index.* perimeters. I’m just getting a 403.
Can I say thank you very very much! Any problem I ran into installing/configuring nginx, I could fix by using this tutorial! Life saver! :) :)