How To Install Nginx on CentOS 7 – DigitalOcean

Introduction

Nginx is a popular high-performance web server. This tutorial will teach you how to install and launch Nginx on your CentOS 7 server.

Prerequisites

The steps in this tutorial require a non-root user with sudo privileges. See our tutorial Initial Server Setup with CentOS 7 to learn how to set up this user.

Step 1 — Add

the EPEL software repository To add the CentOS 7 EPEL repository,

first connect to your CentOS 7 machine via SSH, then use the yum command to install the extended package repository:

  1. sudo yum install epel-release

You will be prompted to verify that you want to install the software. Type y, and then ENTER to continue.

Next, you will install the actual nginx software package.

Step 2 — Installing Nginx

Now that the EPEL repository is installed on your server

, install Nginx using the following yum command:

  1. sudo yum install nginx

Again, answer yes to the verification message, then

Nginx will finish installing.

Step 3 — Nginx startup

Nginx

will not start automatically after it is installed. To run Nginx, use the command

systemctl: sudo systemctl start nginx

You can check the status of the service with

systemctl status:

  1. sudo systemctl status

nginx Output● nginx.service – The nginx HTTP server and reverse proxy Loaded: loaded (/usr/lib/systemd/system/

  1. nginx.service

; disabled; provider preset: disabled) Active: active (running) since Mon 2022-01-24 20:14:24 UTC; 5s Aug Process: 1898 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1896 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1895 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1900 (nginx) CGroup: /system.slice/nginx.service ├─1900 nginx: master process /usr/sbin/nginx └─1901 nginx: worker process Jan 24 20:14:24 centos-updates systemd[1]: Starting nginx HTTP server and reverse proxy… Jan 24 20:14:24 centos-updates nginx[1896]: nginx: the syntax of the configuration file /etc/nginx/nginx.conf is fine Jan 24 20:14:24 centos-updates nginx[1896]: nginx: configuration file /etc/nginx/nginx.conf test is successful Jan 24 20:14:24 centos-updates systemd[1]: Started http server and nginx reverse proxy.

The service must be active.

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic

: sudo firewall-cmd -permanent -zone=public -add-service=http sudo firewall-cmd -permanent -zone=

    public –

  1. add-service=https sudo firewall-cmd –
  2. reload

You can do a random check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser:

http://server_domain_name_or_IP/

You will see the default webpage of CentOS 7 Nginx, which is there for informational and testing purposes. It should look something like this:

CentOS 7 Nginx Default

If you see this page, then your web server is now installed correctly

.

Before proceeding, you’ll probably want to enable Nginx to launch when your system boots. To do this, type the following command:

  1. sudo systemctl enable nginx Nginx

is now installed and running

.

Step 4 — Explore and configure

Nginx

If you want to start serving your own pages or applications through Nginx, you’ll want to know the locations of the Nginx configuration files and the default root directory of the

server.

Default

server root The default server root directory is /usr/share/nginx/html. The files that are placed there will be served on your web server. This location is specified in the default server block configuration file that is included with Nginx, which is located in /etc/nginx/conf.d/default.conf.

Any

additional server blocks, known as virtual hosts in Apache, can be added by creating new configuration files in /etc/nginx/conf.d. Files ending with .conf in that directory will be loaded when Nginx starts.

Nginx

Global Settings

The main Nginx configuration file is located at /etc/nginx/nginx.conf. This is where you can change settings such as the user running the Nginx daemon processes and the number of worker processes that are generated when Nginx is running, among other things.

Conclusion

Once you have installed Nginx on your CentOS 7 server, you can continue to install the full LEMP stack on CentOS 7.