How to install Nginx on CentOS 8

Nginx (Engine X) Is a most popular, powerful and high-performance open source HTTP web server and reverse proxy server with a scalable event-driven (asynchronous) architecture. It can be used as a standalone web server, load balancer, content cache, and reverse proxy for HTTP and non-HTTP servers. In this article we will explain How to install Nginx HTTP web server on CentOS 8 Linux server.

prerequisites

Before proceeding, make sure you are logged in as a user with sudo privileges and that you are not running Apache or any other process on port 80 or 443.

Install Nginx on CentOS 8

To install the latest version of Nginx web server, you need to update the system packages with the following yum command.

# dnf -y update

After installing the software update, you can install the latest stable version Nginx server Use the following command to download from the default package repository.

# dnf -y install nginx

Once Nginx is installed, you can start, enable and verify the status by running the following commands systemctl command

# systemctl enable nginx
# systemctl start nginx

To verify that the service is running, check its status:

# systemctl status nginx

Example output:

# systemctl status nginx
● 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 2020-02-13 18:03:33 UTC; 9s ago
  Process: 20429 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 20427 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 20426 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 20431 (nginx)
    Tasks: 2 (limit: 5067)
   Memory: 11.6M
   CGroup: /system.slice/nginx.service
           ├─20431 nginx: master process /usr/sbin/nginx
           └─20432 nginx: worker process

Feb 13 18:03:32 lintutloc systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 13 18:03:32 lintutloc nginx[20427]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 13 18:03:32 lintutloc nginx[20427]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 13 18:03:32 lintutloc systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 13 18:03:33 lintutloc systemd[1]: Started The nginx HTTP and reverse proxy server.

Command to start / stop / restart Nginx server Run commands as needed.

# systemctl start nginx ## <-- start the server ##
# systemctl stop nginx ## <-- stop the server ##
# systemctl restart nginx ## <-- restart the server ##
# systemctl reload nginx ## <-- reload the server ##
# systemctl status nginx ## <-- get status of the server ##

Adjust firewall

Open and enable ports 80 with 443 Allow network traffic Nginx In System firewall Use the following firewall-cmd command.

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

Now you can test your Nginx installationBy opening http://YOUR_IP In your web browser. You should see the default Nginx welcome page, Its appearance should be as shown below:

Verify Nginx web page

Structure and best practices of Nginx configuration file

  • All Nginx configuration files are located /etc/nginx/ table of Contents.
  • Nginx’s main configuration file is /etc/nginx/nginx.conf.
  • Creating separate profiles for each domain makes the server easier to maintain.
  • Nginx server blocked files must start with .conf And stored in /etc/nginx/conf.d table of Contents. You can have as many server blocks as you need.
  • It is a good practice to follow standard naming conventions. For example, if the domain name is mydomain.com The configuration file should then be named mydomain.com.conf
  • If you use repeatable configuration segments in a domain server block, it is best to refactor these segments into fragments.
  • Nginx log file (access.log with error.log)lie in /var/log/nginx/ table of Contents. Suggest a different access with error Log files for each server block.
  • You can set the root directory of the domain document to any location you want. The most common locations for webroot include:
    • /home//
    • /var/www/
    • /var/www/html/
    • /opt/
    • /usr/share/nginx/html

in conclusion

Congratulations, you have successfully installed Nginx on CentOS 8 server. Now you are ready to start deploying the application and using Nginx as a web or proxy server. If you have any questions or feedback, feel free to leave a comment.

Sidebar