Install Nginx on CentOS

There are many ways to Install Nginx on your CentOS machine, but whenever possible, I will try to install using a repository and yum whenever possible.  If you wish to install Nginx, there are a couple of different repos that can be used.

Installing Nginx using the EPEL Repository

The Fedora project has been sponsoring a great repository named Extra Packages for Enterprise Linux or EPEL which includes many popular software packages for Scientific Linux, CentOS or Fedora.  The current EPEL includes a version of Nginx which you can easily install with yum.  The current EPEL will install nginx.x86_64 1.0.15-5.el6 which is a bit old today.

You can follow these instructions if you wish to install the EPEL repository for installing Nginx:


# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -Uvh ./epel-release-6-8.noarch.rpm
Preparing...                ########################################### [100%]
package epel-release-6-8.noarch is already installed

Then you would install Nginx using “yum install nginx”.

Installing Nginx using the Nginx Community Repo

The best way to get the most updated version of Nginx on your box would be to use the Community Repository supported by the Nginx community.  You can get more information about the Nginx community and their repositories at the Nginx Community Page:  http://wiki.nginx.org/Install.

Install the Nginx Community Repository

To install the Nginx Community Repository, you just need to create a repo file in your /etc/yum.repos.d directory with the correct information:

cat >> /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
EOF

That completes the repository installation, Now, we can install Nginx using the Nginx Community Repository:

# yum install nginx
Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
 * base: dist1.800hosting.com
 * epel: linux.mirrors.es.net
 * extras: mirror.pac-12.org
 * updates: mirror.pac-12.org
Setting up Install Process
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
--> Running transaction check
---> Package nginx.x86_64 0:1.4.4-1.el6.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package        Arch            Version                    Repository      Size
================================================================================
Installing:
 nginx          x86_64          1.4.4-1.el6.ngx            nginx          311 k

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 311 k
Installed size: 770 k
Is this ok [y/N]: y
Downloading Packages:
nginx-1.4.4-1.el6.ngx.x86_64.rpm                         | 311 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : nginx-1.4.4-1.el6.ngx.x86_64                                 1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : nginx-1.4.4-1.el6.ngx.x86_64                                 1/1

Installed:
  nginx.x86_64 0:1.4.4-1.el6.ngx

Complete!

Nginx is now installed on our machine.  Our next task is to put together a simple configuration for our nginx installation and get it started.

Simple Nginx Configuration Example

Nginx installs its configuration files in /etc/nginx and you will typically create a configuration file specific to your website in /etc/nginx/conf.d.  In my case, I created a simple configuration file for my website:

vi /etc/nginx/conf.d/uptimemadeeasy.conf

Now, I put the following lines into my new nginx configuration file:

server {
    listen       80;
        server_name uptimemadeeasy.com www.uptimemadeeasy.com;

	access_log   /var/log/nginx/uptimemadeeasy.com.access.log;
	error_log    /var/log/nginx/uptimemadeeasy.com.error.log;

        root /www/uptimemadeeasy;
        index index.php;

        location / {
                try_files $uri/index.html $uri $uri/;
        }

}

Now, create the www directory and the index.html file:


# mkdir -p /www/uptimemadeeasy
# cd /www/uptimemadeeasy
# echo "Hello World!" >> index.html

Remember to configure your iptables and other firewalls to allow your nginx service port!

Set Nginx to Start at Boot and start it up


# chkconfig nginx on
# service nginx start

 Verify our Nginx WebPage is Up and Running

Now, visit our new Nginx webpage

Now, visit our new Nginx webpage

 

 

Now, let’s use our browser to visit our new webpage.

If everything went well, you should see a very simple page displayed like the one in the image to the right.

And that’s how you Install Nginx on CentOS!

 

 

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: http://uptimemadeeasy.com. You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *