LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

NGINX on CentOS 7

Tags: , Date/Time: September 21, 2015 @ 1:00 am, by Maarten Kossen

 lowendtutorial

Welcome back for another tutorial! This time I’m going to show you how to get started with NGINX on CentOS 7.

CentOS 7 has been out for quite a while now, but it’s not become a standard quite yet. Since they made some big changes to the way CentOS works, migrations aren’t exactly what you’d call easy. In the end, though, CentOS 7 will be used by more and more people. Including those willing to run a web server!

NGINX is a popular option these days for a web server, mostly because it’s able to handle a very high number of concurrent requests. A popular set up is to use it as a proxy for Apache, which can then serve application requests. That will not be covered in this tutorial, though.

So, let’s set up NGINX on CentOS 7. I’m expecting a clean install here!

This tutorial has been updated on October 3rd.

Preparing your environment

First let’s start by ensuring your system is up-to-date. This will not only update any packages that may have an update pending, but will also update the repository caches ensuring that any packages added meanwhile have been added:

sudo yum update

When is asks you to continue, please type ‘y’ to ensure everything gets updated properly.

Now, where in former CentOS versions you often had to add a separate repository to get any kind of decent version of NGINX (or NGINX at all), with CentOS 7 it’s just in the main repositories.

On most servers, nginx is not available immediately without performing some additional steps. If, however, you are using an OpenVZ server and are lucky enough to have installed it with a template with the proper repository added, you are ready to go. There’s a simple check to perform to see if you need an additional repository. Run:

sudo yum install nginx

If this returns the following:

No package nginx available.

Then you need to follow these additional steps. If not, continue to the part ‘Installation’.

CentOS doesn’t ship with NGINX in its repositories by default. Therefore we need to add a different repository, called the ‘EPEL’ repository. This repository contains a lot of packages, amongst which NGINX. To add it, run:

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

You are now set to install nginx!

Installation

Installation is easy:

sudo yum install nginx

It again asks you if you want to continue. Several other packages have been selected as well, as NGINX requires those. Type ‘y’ again to start the installation.

Once the installation is finished, NGINX will have been installed but is not yet running. In order to start it, type:

sudo systemctl start nginx

You should now be able to go to you server’s IP address and you should see NGINX’s default page. That’s how easy it was!

Two more things, though:

First of all, if you reboot, NGINX won’t be started automatically. In order to achieve that, run the following command:

sudo systemctl enable nginx

The command itself doesn’t start NGINX, but ensures it starts automatically at boot. And it would be quite convenient if that were the case, of course.

Second, the default page may not be what you are looking for. So, if you want to replace the default files with something else, go to the following directory:

/usr/share/nginx/html

And modify/replace the files in there. Either do this as root (‘sudo su root’) or use sudo before every command to ensure things go well. Alternatively, you can make all the files owned by www-data, by running:

sudo chown -R www-data:www-data /usr/share/nginx/html


That’s it for this time! Hope you liked the tutorial!

 

 

Maarten Kossen was the administrator of LowEndBox from 2013 to 2015, and brought many ideas and improvements to the website during his leadership. Today he is member of our community and LowEndTalk.

3 Comments

  1. lolz
    just last week I tried similer tutorial with virtualmin but it gave directory error

    September 21, 2015 @ 7:21 pm | Reply
  2. Nice tutorial, btw if you want nginx installer with php/hhvm – mysql on centos 7, check otoinstall.com :)

    September 27, 2015 @ 4:08 am | Reply
  3. I followed the steps, and here is the error I got:
    This site can’t be reached
    192.168.1.30 took too long to respond.

    June 13, 2017 @ 7:01 am | Reply

Leave a Reply to Morris R. Cancel reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

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