Networking

How to install NGINX on your CentOS server

If you're looking to replace Apache with NGINX on your CentOS server, look no further. Learn how to make the switch to this performance-oriented web server.

Image: Jack Wallen

NGINX has gained significant popularity over the years as a high-performance, easier to manage replacement for Apache. If you happen to be using a CentOS server, most likely that server is installed with Apache. If you want to make the switch to NGINX, the process isn't that challenging.

Let's walk through the process of getting NGINX up and running on CentOS 7.

First steps

Because NGINX isn't included in the standard CentOS repositories, you must install an extra package to make sure that, once installed, NGINX is always up to date. To do that, we'll install the Extra Packages For Enterprise Linux (EPEL) package. Follow these steps to install the EPEL package.

  1. Open a terminal.
  2. Issue the su command and then enter the root password.
  3. Install the package with the command yum install epel-release.
  4. Allow the installation to complete.

Installing NGINX

You can now install NGINX with a single command:

yum install nginx

That's it—NGINX is installed. During the installation, however, NGINX isn't automatically started. In order to run the server, you have to first stop the Apache server with the command:

service httpd stop

Now you can start NGINX with the command:

service nginx start

If you point a browser to the IP address of your CentOS server, you should see the NGINX welcome page (Figure A).

Figure A

Figure A
The NGINX welcome page.
Image: Jack Wallen

Auto starting NGINX

With NGINX up and running, you'll want to have it automatically start at server boot. To do so, first, you must prevent Apache from starting at boot with the command:

chkconfig httpd off

Next enable NGINX to start at boot with the command:

chkconfig nginx on

Configuring NGINX

The main configuration file for NGINX is /etc/nginx/nginx.conf. This file is broken into contexts. Out of the box, you'll find the following contexts:

  • Events is used to set global options that affect how Nginx handles connections on a general level.
  • Http is used to define directives that define how the server will handle HTTP or HTTPS connections.
  • Server is declared within the http context and defines server ports, names, document root, etc.

There are more contexts you can add to the nginx.conf file, but you'll first want to get up to speed on how each of the above works. An example context would look like:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

If you want to set up virtual hosts in NGINX, those files are saved in /etc/nginx/conf.d.

The basics

You now have a very basic NGINX web server running on CentOS. Dive into the main configuration file and begin the process of fine-tuning the server to meet your needs. In upcoming posts I'll discuss setting up virtual hosts, setting up a full LEMP (Linux ENGINX MySQL PHP) server, and more.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox