Oct 6, 2023 5 min read

Install Nginx on CentOS 7

Install Nginx on CentOS 7 with our step-by-step tutorial. It is a powerful web server and reverse proxy that efficiently handles online traffic.

Install Nginx on CentOS 7
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Nginx on CentOS 7, let's briefly understand – What is Nginx?

Nginx is a powerful web server and reverse proxy that efficiently handles online traffic. It effectively distributes requests to servers, enhancing website performance and reliability. This open-source software is widely used for its speed, low resource consumption, and robust security features. Nginx is a popular choice for hosting websites and managing internet traffic effectively.

In this tutorial, you will install and manage Nginx on your CentOS 7 machine.

Advantages of Nginx

  1. Speed: Nginx's efficient event-driven architecture enables fast handling of multiple connections, resulting in quick response times and improved website performance.
  2. Scalability: Nginx can handle numerous concurrent connections, making it ideal for high-traffic websites and applications.
  3. Load Balancing: It evenly distributes incoming requests across multiple servers, preventing overload and ensuring optimal resource utilization.
  4. Reverse Proxy: Nginx acts as a middleman between clients and servers, enhancing security, and protecting sensitive information.
  5. High Availability: Nginx's built-in features like health checks and automatic server failover ensure continuous service availability, minimizing downtime and improving reliability.

Prerequisites to Install Nginx on CentOS 7

Make sure that Apache or any other service is not currently operating on port 80 or 443, and that you are signed in as a user with Sudo rights.

Installing Nginx on CentOS

The installation of Nginx on a CentOS server may be accomplished by following the instructions below:

1) Packages for Nginx may be found in the EPEL archives. This will install the EPEL repository if it hasn't previously been done:

sudo yum install epel-release

2) To install Nginx, run the yum command shown below:

sudo yum install nginx

When installing a package from the EPEL repository for the first time, yum could ask you to import the EPEL GPG key:

Output

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid     : "Fedora EPEL (7) <[email protected]>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package    : epel-release-7-9.noarch (@extras)
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:

If so, type y and press Enter.

3) When the installation is finished, you may start the Nginx service by typing:

sudo systemctl enable nginx
sudo systemctl start nginx

Use the following command to see the Nginx service's current status:

sudo systemctl status nginx

The output should look something like this:

Output

● nginx.service - The nginx HTTP and reverse proxy server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Active: active (running) since Mon 2018-03-12 16:12:48 UTC; 2s ago
  Process: 1677 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1673 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1680 (nginx)
  CGroup: /system.slice/nginx.service
          ├─1680 nginx: master process /usr/sbin/nginx
          └─1681 nginx: worker process

4) In order for your server to be accessible, you must allow traffic across both the HTTP (80) and HTTPS (443) ports if it is behind a firewall.

In order to enable the required services, run the following commands:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

5) Open http://YOUR IP in your browser of choice and you should see the default Nginx welcome page, as seen in the picture below, proving that your Nginx installation was successful:

nginx installed

Manage the Nginx Service with systemctl

The Nginx service may be controlled in the same manner as any other systemd unit.

Run the following command to terminate the Nginx service:œ

sudo systemctl stop nginx

To start it again, type:

sudo systemctl start nginx

To restart the Nginx service :

sudo systemctl restart nginx

After you have made any configuration changes, reload the Nginx service:

sudo systemctl reload nginx

If you don't want the Nginx service to start when the computer boots:

sudo systemctl disable nginx

And to re-enable it again:

sudo systemctl enable nginx

Nginx Configuration File’s Structure and Best Practices

  • /etc/nginx/ directory contains all Nginx configuration files.
  • /etc/nginx/nginx.conf is the main Nginx configuration file.
  • It is advised to construct a distinct configuration file for each domain in order to make Nginx setup easier to maintain.
  • New Nginx server block files must be kept in the /etc/nginx/conf.d directory and must finish with .conf. As many server blocks as you require are permissible.
  • It is a good idea to adhere to a consistent naming convention. For instance, your configuration file should be titled /etc/nginx/conf.d/mydomain.com.conf if your domain name is mydomain.com.
  • Creating a directory called /etc/nginx/snippets, restructuring repeated configuration portions into snippets, and then adding the snippet file to the server blocks is a smart practice if you utilize server blocks for several domains.
  • /var/log/nginx/ directory contains the Nginx log files (access.log and error.log) . It is preferred to have a different access and error log files for each server block.
  • Your domain's document root directory can be adjusted to any location you like. Webroot can be found most frequently in: /home/<user_name>/<site_name>, /var/www/<site_name>, /var/www/html/<site_name>, /opt/<site_name>, /usr/share/nginx/html

FAQs to Install Nginx on CentOS 7

After installing Nginx, how can I start or stop its service?

You can start Nginx using: sudo systemctl start nginx. To stop it, use: sudo systemctl stop nginx.

How can I check the status of Nginx service on CentOS 7?

Use the command: sudo systemctl status nginx. This will display whether Nginx is running or not, along with some additional information.

Can I configure Nginx to serve multiple websites on CentOS 7?

Yes, you can create separate server blocks (virtual hosts) for each website in Nginx configuration to serve multiple sites.

Where is the Nginx configuration file located on CentOS 7?

The main configuration file is usually located at: /etc/nginx/nginx.conf, and individual server block configurations can be found in the /etc/nginx/conf.d/ directory.

How can I test if my Nginx configuration is valid before applying changes?

You can check the configuration syntax with: sudo nginx -t. If there are no errors, it will confirm that the configuration is valid.

Can I enable Nginx to start automatically on system boot?

Yes, you can enable this by running: sudo systemctl enable nginx.

Are there any firewall settings I need to configure after installing Nginx on CentOS 7?

Yes, you may need to allow HTTP (port 80) and HTTPS (port 443) traffic through the firewall. Use commands like: sudo firewall-cmd --add-service=http --permanent and sudo firewall-cmd --add-service=https --permanent, then reload the firewall with: sudo firewall-cmd --reload.

Conclusion

You have completed the Nginx installation on your CentOS 7 server successfully. You may now use Nginx as a web or proxy server and begin deploying your apps. Learning how to configure Nginx server blocks is essential if you want to host numerous domains on a single CentOS server.

These days, no website can get away without installing some kind of trusted security certificate.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.