linux compiling and installing nginx and detailed explanation of the parameters of each step

Posted by rupam_jaiswal on Mon, 06 Apr 2020 17:36:47 +0200

1. Environment and installation of dependent packages

[root@localhost ~]# systemctl stop firewalld && setenforce 0 #Turn off the firewall and selinux first
[root@localhost ~]# yum -y install gcc gcc-c++    #Install build environment
[root@localhost ~]# yum install -y pcre pcre-devel   #Install pcre package (make nginx support http rewrite Module)
[root@localhost ~]# yum install -y openssl openssl-devel  #Install OpenSSL devel (enable nginx to support ssl)
[root@localhost ~]# yum install -y zlib zlib-devel  #Install zlib
[root@localhost ~]# useradd nginx #Create user nginx
[root@localhost ~]# passwd nginx #Set user password

2. Download and install

[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
#Download. You can also directly download the compressed package on the official website and then upload rz to the virtual machine
[root@localhost ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/   
#Note that I have version 1.16.0 here. If you are in another version, you need to change the name of the decompression
[root@localhost ~]# cd /usr/local/nginx-1.16.0/
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
#If you copy all of them, you may have some problems with the web page format. You can copy one line at a time
[root@localhost nginx-1.16.0]# make && make install

3. Check whether the nginx configuration file is correct

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# mkdir -p /tmp/nginx
[root@localhost ~]# mkdir /usr/local/nginx/logs

4. Start nginx service

[root@localhost ~]# /usr/local/nginx/sbin/nginx   #Absolute path start
#You can also set up a soft connection and start it directly with nginx
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
[root@localhost ~]# nginx

Here is a detailed description of some compilation parameters and configuration files
5. Various compilation parameters

# View the modules installed by nginx
[root@localhost ~]#/usr/local/nginx/sbin/nginx -V
# Specific functions of module parameters
--with-cc-opt='-g -O2 -fPIE -fstack-protector //Setting additional parameters will be added to the CFLAGS variable. (FreeBSD or
//Used by ubuntu)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
--prefix=/usr/local/nginx //Point to installation directory
--conf-path=/etc/nginx/nginx.conf //Specify profile
--http-log-path=/var/log/nginx/access.log //Specify access log
--error-log-path=/var/log/nginx/error.log //Specify error log
--lock-path=/var/lock/nginx.lock //Specify lock file
--pid-path=/run/nginx.pid //Specify pid file
--http-client-body-temp-path=/var/lib/nginx/body //Set http client request temporary file path
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi //Set http fastcgi temporary file path
--http-proxy-temp-path=/var/lib/nginx/proxy //Set http proxy temporary file path
--http-scgi-temp-path=/var/lib/nginx/scgi //Set http scgi temporary file path
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi //Set http uwsgi temporary file path
--with-debug //Enable debug logging
--with-pcre-jit //Compile PCRE contains "just in time compilation"
--with-ipv6 //Enable ipv6 support
--with-http_ssl_module //Enable ssl support
--with-http_stub_status_module //Get the status of nginx since it was last started
--with-http_realip_module //Allows changing the IP address value of the client from the request header, which is off by default
--with-http_auth_request_module //Implement client authorization based on the result of a sub request. If the sub request returns
2xx Response code, the access is allowed. If it returns 401 or 403, access is denied with the corresponding error code. Any other response generation returned by the child request
//The code is considered an error.
--with-http_addition_module //As an output filter, it supports incomplete buffering and partial response to requests
--with-http_dav_module //Add PUT,DELETE,MKCOL: set creation, COPY and MOVE methods are off by default
//Close, compile to open
--with-http_geoip_module //Use the precompiled MaxMind database to resolve the client IP address and get the variable value
--with-http_gunzip_module //It decompresses the response with the 'ContentEncoding: gzip' header for clients that do not support the 'gzip' encoding method.
--with-http_gzip_static_module //Online real-time compression of output data stream
--with-http_image_filter_module //A filter for transferring JPEG/GIF/PNG pictures (not enabled by default). gd Library
//To be used)
--with-http_spdy_module //SPDY can shorten the loading time of web pages
--with-http_sub_module //Allow some text in nginx response to be replaced with some other text
--with-http_xslt_module //Filter transform XML requests
--with-mail //Enable POP3/IMAP4/SMTP proxy module support
--with-mail_ssl_module //Enable NGX mail SSL module support enable external module support

6. Configuration file

# Global parameter settings
user nginx; #Designated user
worker_processes 4; #Set the number of nginx startup processes to be the same as the number of logical CPUs
error_log logs/error.log; #Specify error log
worker_rlimit_nofile 10240; #Set the maximum number of files a nginx process can open
pid /var/run/nginx.pid;
events {
worker_connections 1024; #Set the maximum number of concurrent connections for a process
}
# http service related settings
http {
include mime.types;
default_type application/octet-stream;
log_format main 'remote_addr - remote_user [time_local] "request" '
'status body_bytes_sent "$http_referer" '
'"http_user_agent" "http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #Set the location and format of the access log
sendfile on; #Whether to call the sendfile function to output the file is generally set to on. If nginx is used for disk IO load application
//It can be set to off to reduce the system load
gzip on; #Do you want to enable gzip compression? Remove the comments and enable it
keepalive_timeout 65; #Set the timeout for long connections
# Related settings of virtual server
server {
listen 80; #Set listening port
server_name localhost; #Set the bound host name, domain name, or ip address
# charset koi8-r; # Set encoding character
charset utf-8;
location / {
root /var/www/nginx; #Set the root directory location of the server's default web site, which needs to be created manually
index index.html index.htm; #Set default open document
}
error_page 500 502 503 504 /50x.html; #Set error message return page
location = /50x.html {
root html; #The absolute position here is / usr/local/nginx/html
}
}
}
nginx.conf consists of three parts: Global block, events block and http block. The http global is included in the http block
 Block, multiple server blocks. Each server block contains server global block and multiple location blocks. The configuration nested in the unified configuration block is fast, and the
 There is no order relationship.

7. Some common commands during nginx installation
I have established a soft connection here, so I use the nginx command directly

nginx -c /path/nginx.conf # Start nginx with a configuration file in a specific directory:
nginx -s reload # Reload takes effect after configuration modification
nginx -s stop # Fast stop nginx
nginx -t # Test if the current profile is correct
nginx -t -c /path/to/nginx.conf # Test whether the specific nginx configuration file is correct
chmod +x /etc/init.d/nginx #add permission
systemctl daemon-reload  #Reload system boot file
systemctl start nginx #Start and set power on

Your comments and compliments are my biggest motivation for writing, crab crab.

Topics: Linux Nginx yum OpenSSL SSL