How to install Nginx on CentOS 7


Nginx, read as Engine X, is a free, open source, high-performance HTTP and reverse proxy service that loads heavily trafficked sites.

Nginx can be used as a standalone Web service or as a reverse proxy for Apache or other Web services.

Nginx can handle more concurrent connections than Apache, and the memory footprint per connection is very small.

This tutorial will teach you how to install and manage Nginx on Centos 7 servers.

Preparation before starting

Before you start reading this tutorial, make sure that you are logging in to the server as a user with sudo permission and that there is no Apache or other service on the server using ports 80 (HTTP) and 443 (HTTPS) in case the ports are occupied and Nginx cannot start properly.

Install Nginx on CentOS

Please follow the steps below to install Nginx in CentOS.

1. There are Nginx installation packages in EPEL warehouse. If you have not already installed EPEL, you can do so by running the following command:

sudo yum install epel-release
Copy the code

If you are currently logged in as a user other than root, you will be prompted to enter a password to run epel-Release. You will not be able to see the input password, so don’t worry, just keep typing. Sudo: sudo: sudo: sudo: sudo: sudo: sudo

2, Enter the following command to install Nginx:

sudo yum install nginx
Copy the code

If this is your first time installing software from an EPEL repository, Yum may prompt you to import an EPEL GPG key:

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]:
Copy the code

Similar to the above, in this case, type y and then Enter to continue the installation.

3. After the installation is complete, you can use the following command to set up and run the Nginx service:

Setup Nginx startup:

sudo systemctl enable nginx
Copy the code

After running the above command, output similar to the following, indicating that a soft connection has been created to associate Nginx, do not worry, not error, the next step is to start Nginx.

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Copy the code

Nginx start:

sudo systemctl start nginx
Copy the code

Check the running status of Nginx by running the following command:

sudo systemctl status nginx
Copy the code

It then outputs the following type

And low nginx. Service - The nginx HTTP reverse proxy server The Loaded: The 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: (nginx) CGroup: 1680 / system. Slice/nginx service ├ ─ 1680 nginx: Master process /usr/sbin/nginx ├ ─ 081 nginx: Manual manualCopy the code

4. If your server has a firewall enabled, you need to enable both port 80 (HTTP) and port 443 (HTTPS)

To open both ports, run the following command:

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

Domestic server manufacturers and security groups may also block these two ports by default, such as Ali Cloud and Tencent Cloud. If you find that you cannot access these two ports in step 5, you can search baidu for how to release these two ports.

5. To verify that Nginx started successfully, open http://YOUR_IP in your browser and you will see the default Nginx welcome page, similar to the following figure:

Note: The default Welcome page for Nginx comes in several styles, depending on the version you have installed, so just focus on the page header.

Manage Nginx through systemctl

You can manage Nginx just like any other service.

Start the Nginx

sudo systemctl start nginx
Copy the code

Stop Nginx

sudo systemctl stop nginx
Copy the code

Restart the Nginx

sudo systemctl restart nginx
Copy the code

After modifying the Nginx configuration, reload

sudo systemctl reload nginx
Copy the code

Set Nginx to start at boot

sudo systemctl enable nginx
Copy the code

Turn off the boot and start Nginx

sudo systemctl disable nginx
Copy the code

Nginx configuration files and best practices

  • When Nginx is installed in the above manner, all relevant configuration files are present/etc/nginx/Directory.
  • The main configuration file for Nginx is/etc/nginx/nginx.conf.
  • To make the Nginx configuration easier to maintain, it is recommended to create a separate configuration file for each service (domain name).
  • Each individual Nginx service configuration file must be configured with.confEnd and store in/etc/nginx/conf.dDirectory. You can create as many independent configuration files as you need.
  • Separate configuration files, it is recommended to follow the following naming convention, for example, your domain name iskaifazhinan.comYour configuration file should look like this/etc/nginx/conf.d/kaifazhinan.com.confIf you are deploying multiple services on a server, of course you can add Nginx forwarding port numbers to the file name, for examplekaifazhinan.com.3000.conf“It will look friendlier.
  • If you have a lot of duplicate code in your configuration, it is recommended that you create one/etc/nginx/snippetsFolder, which stores all code blocks that will be reused and then referenced in the various Nginx configuration files that need to be used for easier management and modification.
  • Nginx log file (access.logerror.log) is located in the/var/log/nginx/Directory. You are advised to configure different access permissions and error log files for each independent service to facilitate error search.
  • You can store the code files to be deployed anywhere you want, but it is generally recommended to store them in one of the following locations:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>
    • /usr/share/nginx/html

conclusion

You have successfully installed the Nginx service on CentOS 7. Now you can deploy your service code on your server using Nginx. If you want to deploy multiple services on your server, You may need To read How To Set Up Nginx Server Blocks on CentOS 7.

Oh, and security certificates (HTTPS) are a “must have” feature on every website these days. So you can read Secure Nginx with Let’s Encrypt on CentOS 7 to learn how to generate a free security certificate using Let’s Encrypt SSL.

Looking forward to seeing you next time:)

How to install Nginx on CentOS 7