Nginx installation detailed tutorial

Nginx installation detailed tutorial

1. Brief Introduction of Nginx

Nginx is a free, open source, high-performance HTTP server and reverse proxy server; it is also an IMAP, POP3, and SMTP proxy server; Nginx can be used as an HTTP server to publish websites, and Nginx can be used as a reverse proxy to implement load balancing.

For more information about Nginx and its functions, please search on Baidu

2. Install Nginx basic dependency packages

[root@nginx ~]# yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y

3. Download related software packages 3.1 Download the stable version of nginx

Note: Go to the Nginx installation package download address and copy the version link you want. I take Nginx-1.14.0 as an example.

[root@nginx~]# cd /usr/local/src/  
[root@ nginx src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz

3.2 Download nginx-sticky module (optional installation, applied to cluster)

Function: Backend load balancing to solve session sticky problems.

[root@nginx src]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz

3.3 Download the ngx_cache_purge module (optional installation)

Function: Compile ngx_cache_purge into Nginx to clear the cache of the specified URL.

(Official website address: http://labs.frickle.com/nginx_ngx_cache_purge/)

[root@nginx src]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz 

4. Add Nginx user and group

[root@nginx ~]# groupadd www    
[root@nginx ~]# useradd -g www www -s /sbin/nologin

Note: Create the nginx running user www and add it to the www group. The www user is not allowed to log in to the system directly.

5. Unzip the Nginx source code package and enter the unzip directory to compile and install

[root@nginx src]# tar zxf nginx-1.14.0.tar.gz 
# Note the optional options for 3.2 [root@nginx src]# tar zxf ngx_cache_purge-2.3.tar.gz 
# Note the optional 3.3 [root@nginx src]# tar zxf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz      
[root@nginx src]# cd nginx-1.14.0/

Note: The '=' sign is followed by the absolute path of your installation package and the configuration file path.

[root@nginx-master nginx-1.14.0]# ./configure --prefix=/usr/local/nginx  
> --user=www --group=www       
> --with-http_stub_status_module      
> --with-http_realip_module --with-http_ssl_module    
> --with-http_gzip_static_module      
> --http-client-body-temp-path=/var/tmp/nginx/client    
> --http-proxy-temp-path=/var/tmp/nginx/proxy     
> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi
# Note the 3.2 options > --with-pcre --add-module=/usr/local/src/ngx_cache_purge-2.3 
# Note the 3.3 options> --add-module=/usr/local/src/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 

After the compilation is completed:

[root@nginx nginx-1.14.0]# make && make install

Note: All Nginx modules must be added at compile time and cannot be loaded dynamically at runtime.

6. Optimize the execution path of the Nginx program

[root@nginx nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
[root@nginx nginx-1.14.0]# nginx -t

If the following problems occur:

This directory needs to be created:

[root@nginx nginx-1.14.0]# mkdir -p /var/tmp/nginx/client 

7. Configure Nginx startup commands to facilitate our management of Nginx

The original command to manage nginx is not very convenient!

[root@nginx-master ~]# nginx Start nginx
[root@nginx-master ~]# nginx -s reload Restart nginx
[root@nginx-master ~]# nginx -s stop Stop nginx

Next, set up common management commands:

1) Create a startup command script file:

[root@nginx ~]# vim /etc/init.d/nginx

2) Insert the following startup script code into this Nginx file:

-----------------------------------The script is as follows----------------------------------
#!/bin/bash
# chkconfig: -85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
scriptNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
[root@nginx ~]# cd /etc/init.d/

3) Set everyone to have execution permissions for the startup script nginx file:

[root@nginx init.d]# chmod a+x nginx

4) Add nginx to the system service:

[root@nginx init.d]# chkconfig --add nginx

5) Set the service to start at boot:

[root@nginx init.d]# chkconfig nginx on

Note: reboot the system to take effect!

6) After restarting, you can execute the following command to manage it

systemctl start nginx.service # Start nginx
systemctl stop nginx.service # End nginx
systemctl restart nginx.service # Restart nginx
systemctl enable nginx.service #Startup

8. If Nginx fails to start, please try the following methods

1) If httpd has been installed before, you need to uninstall httpd first, otherwise the startup will fail unless the Nginx startup port is changed.

[root@nginx ~]# yum remove httpd -y

2) If it still fails to start, try this method: After killing Nginx related processes, start Nginx

[root@nginx ~]#ps -A | grep nginx           
[root@nginx ~]#kill -9 pid1            
[root@nginx ~]#kill -9 pid2

3) If the problem still does not work, check the log information to find a solution.

9. Start Nginx and access the test

Note: Nginx optimization and reverse proxy functions will continue to be updated in the future!

Summarize

The above is the detailed tutorial of Nginx installation introduced by the editor. I hope it will be helpful to everyone!

You may also be interested in:
  • Install and configure IIS + MySQL + nginx + php7.1.7 under Win10
  • How to install nginx on win10
  • How to install Nginx in Docker
  • How to install Nginx in a specified location in Centos system
  • Nginx installation error solution
  • How to install php7 + nginx environment under centos6.6
  • How to install PHP7.4 and Nginx on Centos
  • Detailed tutorial for installing nginx on centos8 (picture and text)
  • Detailed explanation of nginx installation, deployment and usage on Linux
  • Correct steps to install Nginx in Linux
  • The process of installing and configuring nginx in win10

<<:  How to compare two database table structures in mysql

>>:  jQuery implements employee management registration page

Recommend

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

Simple implementation of mini-vue rendering

Table of contents Preface Target first step: Step...

A simple way to implement all functions of shopping cart in Vue

The main functions are as follows: Add product in...

MySQL slow query pitfalls

Table of contents 1. Slow query configuration 1-1...

VMware and CentOS system installation method to reset the root password

Today's Tasks 1. Choice of Linux distribution...

The process of installing MySQL 8.0.26 on CentOS7

1. First, download the corresponding database fro...

Comprehensive understanding of html.css overflow

Comprehensive understanding of html.css overflow ...

The difference and execution method of select count() and select count(1)

Count(*) or Count(1) or Count([column]) are perha...

Sample code on how to implement page caching in vue mobile project

background On mobile devices, caching between pag...

MySQL trigger definition and usage simple example

This article describes the definition and usage o...

Use momentJs to make a countdown component (example code)

Today I'd like to introduce a countdown made ...

Solution to the Docker container being unable to access the host port

I recently encountered a problem at work. The doc...

MySQL index usage monitoring skills (worth collecting!)

Overview In a relational database, an index is a ...

How to build a redis cluster using docker

Table of contents 1. Create a redis docker base i...