
Introduction
Nginx, pronounced "engine x," is a highly efficient web server that also serves as a reverse proxy, load balancer, and HTTP cache. It's renowned for handling high-traffic websites effectively and excels at serving static content and media streams. This guide will walk you through the process of installing Nginx on CentOS 7, though the instructions are applicable to other CentOS versions as well.

Nginx vs. Apache
Both Nginx and Apache are powerful and flexible web servers, but they excel in different areas. Nginx is superior for serving static content and advanced features like media streaming and reverse proxying for non-HTTP protocols. Apache, on the other hand, performs well for dynamic content. Choosing between them depends on your specific needs—Nginx might be better for high-traffic sites or media-heavy applications, while Apache is a strong contender for more varied use cases. In many scenarios, a combination of both might be ideal.
Disabling Apache
CentOS typically includes Apache by default, which needs to be disabled before installing Nginx. Follow these steps:
Stop Apache:
service httpd stop
Prevent Apache from Starting on Boot:
systemctl disable httpd
Installing Nginx
Add the EPEL Repository:
To access the Nginx package, install the EPEL repository:
yum install epel-release -y
Install Nginx:
Use the package manager to install Nginx:
yum install nginx -y
Configure Nginx for IPv6:
Since VPSHosting.lk servers don’t support IPv6, remove the related configuration line:
sed -i '/\[::\]:80/d' /etc/nginx/nginx.conf
Start Nginx and Enable It on Boot:
service nginx start
systemctl enable nginx
Verifying Nginx Installation
To ensure Nginx is running properly, visit your server’s hostname or IP address in a web browser:
http://hostname_or_IP_of_your_server
You should see the default Nginx landing page, indicating that the server is operational.

Server Root and Configuration
To start serving your own content with Nginx, you need to understand its configuration and server root directory:
Default Server Root:
The default directory for serving files is:
/usr/share/nginx/html
Files placed here will be accessible via your web server. This location is defined in the default server block configuration file at:
/etc/nginx/conf.d/default.conf
Server Block Configuration:
For additional virtual hosts or server blocks, create new configuration files in:
/etc/nginx/conf.d
Nginx will load any .conf
files in this directory when it starts.
Global Configuration:
The main configuration file is located at:
/etc/nginx/nginx.conf
Here, you can modify global settings such as the user running Nginx processes and the number of worker processes.
Conclusion
With Nginx installed and configured, your CentOS 7 server is ready to efficiently handle web traffic, whether serving static content or acting as a reverse proxy. By understanding and adjusting configuration files and server blocks, you can tailor Nginx to meet your specific needs.