How to Install Nginx on CentOS 7

Introduction

Nginx pronounced engine x is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet.
Nginx can be used as a standalone web server, and as a reverse proxy for Apache and other web servers.

Compared to Apache, Nginx can handle a much large number of concurrent connections and has a smaller memory footprint per connection.
This tutorial will teach you how to install and manage Nginx on your CentOS 7 machine.


Prerequisites

  • Deploy a Cybree Instance with CentOS 7 image.
  • Connect to your instance via Cybree VNC terminal, or use other terminal (such as Putty SSH) to connect your instance
  • Login to account with sudo or root privileges. The default Centos administrator login name is "root"
  • You don’t have Apache or any other service running on port 80 or 443.

Step 1: Update Package Repository Cache

  • Before you start building the stack, be sure to update the packages on your CentOS 7 server using the command:
sudo yum update
  • Enter "y" when you are prompted to confirm the installation. And wait a few minute to download and process until it prompts "Complete!"

Step 2: Install Nginx

  1. Nginx packages are available in the EPEL repositories. If you don’t have EPEL repository already installed you can do it by typing:
sudo yum install epel-release
  1. Install Nginx by typing the following yum command:
sudo yum install nginx
  1. If this is the first time you are installing a package from the EPEL repository, yum may prompt you to import the EPEL GPG key. If that’s the case, type y and hit Enter.
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
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]:
  1. Once the installation is complete, enable and start the Nginx service with:
sudo systemctl enable nginx
sudo systemctl start nginx
  1. Check the status of the Nginx service with the following command:
sudo systemctl status nginx
  1. The output should look something like this:
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 Thu 2021-05-06 15:17:17 JST; 4min 35s ago
  Process: 393 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 390 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 388 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 396 (nginx)
   CGroup: /system.slice/nginx.service
           ├─396 nginx: master process /usr/sbin/nginx
           └─397 nginx: worker process
  1. To verify your Nginx installation, open http://YOUR_IP in your browser of choice, and you will see the default Nginx welcome page as shown in the image below:

截屏2021-07-01 上午11.37.58.png

Manage the Nginx Service with systemctl

  1. You can manage the Nginx service in the same way as any other systemd unit.
    To stop the Nginx service, run:
sudo systemctl stop nginx
  1. To start it again, type:
sudo systemctl start nginx
  1. To restart the Nginx service :
sudo systemctl restart nginx
  1. Reload the Nginx service after you have made some configuration changes:
sudo systemctl reload nginx
  1. If you want to disable the Nginx service to start at boot:
sudo systemctl disable nginx
  1. And to re-enable it again:
sudo systemctl enable nginx

Nginx Configuration File’s Structure and Best Practices

  • All Nginx configuration files are located in the /etc/nginx/ directory.

  • The main Nginx configuration file is /etc/nginx/nginx.conf.

  • To make Nginx configuration easier to maintain it is recommended to create a separate configuration file for each domain.

  • New Nginx server block files must end with .conf and be stored in /etc/nginx/conf.d directory. You can have as many server blocks as you need.

  • It is a good idea to follow a standard naming convention, for example if your domain name is mydomain.com then your configuration file should be named /etc/nginx/conf.d/mydomain.com.conf

  • If you use repeatable configuration segments in your domains server blocks then it is a good idea to create a directory named /etc/nginx/snippets refactoring those segments into snippets and include the snippet file to the server blocks.

  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have a different access and error log files for each server block.

  • You can set your domain document root directory to any location you want. The most common locations for webroot include:
    o/home/<user_name>/<site_name>
    o/var/www/<site_name>
    o/var/www/html/<site_name>
    o/opt/<site_name>
    o/usr/share/nginx/html


Conclusion

Congratulations, you have successfully installed Nginx on your CentOS 7 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server. If you intend to host multiple domains on your CentOS server, you should learn how to create Nginx server blocks.