Deploy Nginx on Centos 7

  • by Haozheng Li
  • 1 likes

Deploy Nginx on Centos 7

Nginx (pronounced "engine-x") is an open-source, high-performance HTTP and reverse proxy server, which can also be used as a mail proxy server. It is renowned for its stability, rich feature set, straightforward configuration, and low resource consumption. Nginx is good at handling high concurrency, and thanks to its event-driven and asynchronous architecture. This design allows Nginx to manage a large number of concurrent connections with minimal resource usage, making it an ideal choice for hosting high-traffic websites and applications. Nginx is also widely used for load balancing and HTTP caching, enhancing the performance of websites and applications.

We have two ways to install Nginx: Yum installation and source code installation.


Yum installation (Recommended)

Update yum version

$ sudo yum -y update

Parameter -y is used to set all answer of confirmation question to 'yes'. It ensures that we can complete instruction steps continuously without being interrupted by confirmation question. If you want to view and confirm each question, do not use this parameter.

Install EPEL repository

Nginx is not in the default yum source, we can use EPEL repository. Run this command to insatll EPEL repository:

$ sudo yum install -y epel-release

(In some Centos-like Linux system, ep: Redhat, Amazon Linux, installation of EPEL is not a necessary. If you find No match for argument: epel-release , you can just move to next step. )

Install Nginx

Once the EPEL respsitory has been installed, we can now install Nginx:

$ sudo yum install -y nginx

After successful installation: The default nginx website directory is: /usr/share/nginx/html The default configuration file is: /etc/nginx/nginx.conf The custom configuration file directory is: /etc/nginx/conf.d/

Start Nginx Service

After Nginx benn installed, Nginx will not start automatically. To get Nginx running, use the systemctl command:

$ sudo systemctl enable nginx  // Set to start Nginx on boot
$ sudo systemctl start nginx  // Run nginx service

After Nginx service has been started, we can use this command to check its status:

$ sudo systemctl status nginx

The output should look like:

$ * 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 Wed 2023-01-25 15:31:54 EST; 1min 20s ago
$   Process: 22547 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
$   Process: 22544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
$   Process: 22542 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
$  Main PID: 22550 (nginx)
$    CGroup: /system.slice/nginx.service
$            * 22550 nginx: master process /usr/sbin/nginx
$            * 22551 nginx: worker process
$            * 22552 nginx: worker process
$ 
$ Jan 25 15:31:54 User systemd[1]: Starting The nginx HTTP and reverse proxy server...
$ Jan 25 15:31:54 User nginx[22544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
$ Jan 25 15:31:54 User nginx[22544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
$ Jan 25 15:31:54 User systemd[1]: Started The nginx HTTP and reverse proxy server.

The active attribute on second row should be active (running)

Also, there are some useful systemctl command:

$ sudo systemctl restart nginx  // stop Nginx service
$ sudo systemctl reload nginx  // Reload, because generally after reconfiguration, you do not want to restart the service, you can use reload at this time.

Config Nginx

Nginx configurations are located in /etc/nginx/nginx.conf. You might also want to look into the /etc/nginx/conf.d/ directory for specific site configurations. Use a text editor like vi or nano to edit these files

Configure firewall to allow traffic (Optional)

If your server has opened a firewall, you need to run the following command to open ports 80 and 443

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

You can now test the Nginx installation by opening http://server_ip_or_domain_name/ in your web browser. You should see the default Nginx welcome page.

Or you can check Nginx process:

$ ps -ef | grep nginx

The output should look like:

$ root     22550     1  0 15:31 ?        00:00:00 nginx: master process /usr/sbin/nginx
$ nginx    22551 22550  0 15:31 ?        00:00:00 nginx: worker process
$ nginx    22552 22550  0 15:31 ?        00:00:00 nginx: worker process
$ root     22558 22147  0 15:58 pts/0    00:00:00 grep --color=auto nginx
$ 

Source code installaton

Install dependencies

Nginx depend on folloing module:

  1. gcc
  2. pcre-devel
  3. zlib-devel
  4. openssl
  5. openssl-devel

Use this command to install necessery dependencies:

$ yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

Download and unzip the Nginx installation package

$ cd /
$ mkdir -p  www/server/nginx
$ cd /www/server/nginx
$ wget http://nginx.org/download/nginx-1.21.0.tar.gz
$ tar -xvf nginx-1.21.0.tar.gz

Install Nginx service

$ cd /www/server/nginx/nginx-1.21.0	//Enter nginx directory
$ ./configure --prefix=/usr/local/nginx	// installation configuration, check the system environment
$ make
$ make install

Firewall enabled

Just like yum installation, use the same commands:

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

Start Nginx service

$ cd /usr/local/nginx/sbin/
$ ./nginx

Some useful command for change status of Nginx:

$ cd /usr/local/nginx/sbin	// Enter Nginx directory
$ ./nginx				// Start Nginx service
$ ./nginx -s stop		// Stop Nginx service
$ ./nginx -s reload	// Reload Nginx service
$ ./nginx -v			// Check Nginx version

After Nginx been installed, you can use the same way as yum installation to check if nginx has started successfully. Also, use the same way to set Nginx automaticlly start on boot.

Setting Up Python Virtual Environments

Comments

0 Comments