The best VPN 2024

The Best VPS 2024

The Best C# Book

How to install nginx on centos 7

Spread the love

How to install nginx on centos 7? In my recent work, I need to install nginx on a centos 7 reverse proxy server for my customers to resolve different domain names to that server for their own business.

If you wnat to check the centos 7 server whether installed nginx, you just only execute this command.

How to install nginx on centos 7
How to install nginx on centos 7

Execute ps -ef | grep nginx command (ps -ef: lists all processes, grep nginx: filters out processes that are not related to nginx)

ps -ef | grep nginx

View Nginx ProcessID

ps -C nginx -o pid

These are some of the non-essential steps, if you are sure that you do not have nginx installed on your server, then the next are the core steps.

Install nginx on centos 7

Before you start, you need to make sure that your account has sudo privileges, preferably if you can use a root account.

1. Adding the EPEL Software Repository

Connect to your centos 7 server wia SSH(I like use putty), then use the yum command to install the extended package repository:

sudo yum install epel-release
How to install nginx on centos 7
How to install nginx on centos 7

You’ll be prompted to verify that you want to install the software. Type y then ENTER to continue.

-
Total                                                                       1.8 MB/s | 2.3 MB  00:00:01
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-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y

How to install nginx on centos 7

Next, you’ll install the actual nginx software package.

2. Installing Nginx

Enter this command on putty/others, means execute this command. Completing the first step means that the EPEL repository installation is successful.

sudo yum install nginx

3. Starting Nginx

sudo systemctl enable nginx
sudo systemctl start nginx

The above 2 instructions indicate enabling and starting Nginx, respectively.

4. Check Nginx Status

sudo systemctl status nginx
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 Sat 2023-05-13 10:10:28 CST; 10s ago
  Process: 250717 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 250714 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 250711 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 250719 (nginx)
   CGroup: /system.slice/nginx.service
           ├─250719 nginx: master process /usr/sbin/nginx
           ├─250720 nginx: worker process
           ├─250721 nginx: worker process
           ├─250722 nginx: worker process
           ├─250723 nginx: worker process
           ├─250724 nginx: worker process
           ├─250725 nginx: worker process
           ├─250726 nginx: worker process
           └─250727 nginx: worker process

May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net systemd[1]: Starting The nginx HTTP and revers....
May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net nginx[250714]: nginx: the configuration file /...k
May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net nginx[250714]: nginx: configuration file /etc/...l
May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net systemd[1]: Started The nginx HTTP and reverse....
Hint: Some lines were ellipsized, use -l to show in full.

How to install nginx on centos 7

Some Nginx Useful Command

View the nginx conf file directory

nginx -t
sudo systemctl stop nginx – stop nginx
Sudo Systemctl Start nginx – Start nginx
sudo systemctl restart nginx – restart nginx
sudo systemctl reload nginx -- overloaded conf
sudo systemctl disable nginx -- disables auto-start
sudo systemctl enable nginx -- enables auto-start

Configure Nginx

server{
#	listen   443;
        listen   80;
	server_name  znlive.com;
	client_max_body_size 1G;
	location / {
		proxy_set_header X-Forwarded-For $remote_addr;
		proxy_set_header Host            'znlive.com';
		proxy_set_header   X-Forwarded-Proto $scheme;              
	        proxy_pass http://172.3.2.12:8080;
		index index.html index.htm;
	}
}

server{
#	listen   443;
        listen   80;
	server_name  jhrs.com;
	client_max_body_size 1G;
	location / {
		proxy_set_header X-Forwarded-For $remote_addr;
		proxy_set_header Host            'jhrs.com';
		proxy_set_header   X-Forwarded-Proto $scheme;              
	        proxy_pass http://172.3.2.12:8081;
		index index.html index.htm;
	}
}

server{
#	listen   443;
        listen   80;
	server_name  by3.org;
	client_max_body_size 1G;
	location / {
		proxy_set_header X-Forwarded-For $remote_addr;
		proxy_set_header Host            'by3.org';
		proxy_set_header   X-Forwarded-Proto $scheme;              
	        proxy_pass http://172.3.2.12:8082;
		index index.html index.htm;
	}
}

server{
#	listen   443;
        listen   80;
	server_name  guowaivps.org;
	client_max_body_size 1G;
	location / {
		proxy_set_header X-Forwarded-For $remote_addr;
		proxy_set_header Host            'guowaivps.org';
		proxy_set_header   X-Forwarded-Proto $scheme;              
	        proxy_pass http://172.3.2.12:8083;
		index index.html index.htm;
	}
}

How to install nginx on centos 7

How to parse HTML with C#
How to install nginx on centos 7

Firewall Port Settings

Usually we don’t need to set up additional ports, but for example, if you need to open ports 80 and 443, or whatever, you can execute the following command. If the firewall blocks ports 80 and 443, use the following command to enable them.

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

Leave a Comment