The correct steps for installing Nginx in Linux

  • 2020-05-17 07:41:25
  • OfStack

preface

If you are like me 1, as a helpless and painful Java background, in addition to the implementation of a lot of project functions, but also account for the deployment of the project, operation and maintenance work. Install the new Nginx on the new server, before the installation look at the tutorial on the Internet, in the face of 5 spend 8 tutorials, various installation methods, the in the mind always asked what is the best way, or what method is the most suitable for their own? Let's take a look at the various Nginx installations that are appropriate for that situation.

Install using system 2 source mode

In Ubuntu/Debian department


sudo apt-get install nginx

Or RedHat/CentOS department


sudo yum install nginx 

It's the easiest, the fastest way, but it's not the best way, so let's talk about the main problem.

advantages

All of the installation 2 base dependencies have been taken care of, and don't worry about compatibility, out of the box You don't have to deal with the connection configuration of Nginx and user privileges. I've already written this for you Don't worry about Nginx Bug maintenance upgrades, just get the latest system Uninstall simple, direct 1 command can be Simple log maintenance, automatic truncation day log, compression and preservation

disadvantages

You cannot choose the installation version You cannot choose which module to compile Extending functionality becomes cumbersome and requires recompilation The configuration file is under /etc/, and the deployment file is under /var/www The root permission is required to restart the service and change the configuration Poor performance 1 point compilation installation

If you are new to Linux, it is absolutely recommended that you install it this way, without any compiler dependencies. However, this is not recommended if your server is used in a production environment that is being developed and refined. A third party module may be added in the future, when the installation must be compiled (discussed below). When restarting the server, do not use the root user, but use sudo to get root briefly. If your server is used to deploy 1 static file, mainly web space, usually mainly using the ftp tool to deploy the file, there is no problem with this way.

Compile the installation

The advantages and disadvantages, I will not write, is basically the top of the upside down can be. To install with this method, you must know a little bit about Linux compilation before you can use Linux moderate users. Most of the tutorials I've seen on the web have compiler dependencies installed directly on /usr/local/ which is not a good way to do it. If we want to uninstall these dependencies in the future, we will find it very troublesome. It is not easy to delete directly under the category. Some distributions of Linux will write the installation files to the configuration files. I don't know where to look for these configuration files. How to handle versioning if versioning affects other software. We only wanted to install Nginx, which resulted in a lot of problems.

Compile environment preparation

Before you start, make sure your Linux has gcc, make, wget,g++.

Create a category to hold the download files, go to the directory and download the dependent library source files

Download openssl mainly for ssl module encryption, support htps


wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz

Download pcre for address redirection, address rewriting, and support for localtion directives and regular expressions


wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz

Download the zlib gzip compression module


wget https://zlib.net/zlib-1.2.11.tar.gz

Download Nginx


wget http://nginx.org/download/nginx-1.17.1.tar.gz

Extract all files using tar


ls *.tar.gz | xargs -n1 tar xzvf 

Compiler options

Scripts that use./configure to set various Nginx parameters, including source and configuration file paths, compiler options, connection handlers, and module lists. This script is done by creating the compiled code and Makefile needed to install Nginx open source.

参数 描述
�prefix=<PATH> Nginx安装目录,以及有其他配置脚本选项的路径设置的所有相对路径的基本位置。默认值/usr/local/nginx
�sbin-path=<PATH Nginx2进制执行文件的名称,默认值:<prefix>/sbin/nginx
�conf-path=<PATH> Nginx配置文件的名称。但是,您可以通过在nginx命令行上使用选项指定其他文件来始终在启动时覆盖此值。默认值:<prefix> conf / nginx.conf-c <FILENAME>
�pid-path=<PATH> nginx.pid文件的名称,用于存储nginx主进程的进程ID 。安装后,可以使用Nginx配置文件中的pid指令更改文件名的路径。默认值:<prefix> /logs/nginx.pid
�error-log-path=<PATH> error,warn和诊断数据的日志文件的名称。安装后,可以使用Nginx配置文件中的error_log指令更改文件名。默认值:<prefix> /logs/error.log
�http-log-path=<PATH> HTTP服务器请求的主日志文件的名称。安装后,始终可以使用Nginx配置文件中的access_log指令更改文件名。默认值:<prefix> /logs/access.log
�user=<NAME> Nginx运行进程的拥有者。安装后,可以使用Nginx配置文件中的user指令更改名称。默认:nobody
�group=name nginx运行进程的拥有者用户组。安装后,可以使用NGINX配置文件中的user指令更改名称。默认值:�user选项设置的值
�with-pcre=<PATH> PCRE库源代码的路径,这是位置指令和Rewrite模块中正则表达式支持所必需的
�with-pcre-jit 使用“即时编译”支持(pcre_jit指令)构建PCRE库
�with-zlib=<PATH> zlib库的源代码路径,Gzip模块需要该路径
�with-http_ssl_modul 启用HTTPS支持
�with-http_v2_module 开启 HTTP/2请求支持

I'm not going to list 11 more compiler parameters, but if you're interested, you can do it yourself

nginx website

Compile the installation


./configure \
  --with-openssl=../openssl-1.0.2s \
  --with-pcre=../pcre-8.43 \
  --with-zlib=../zlib-1.2.11 \
  --with-pcre-jit --user=admin \
  --prefix=/home/admin/nginx \
  --with-http_ssl_module \
  --with-http_v2_module

Output the following information to show that dependencies are ok


Configuration summary
 + using PCRE library: ../pcre-8.43
 + using OpenSSL library: ../openssl-1.0.2s
 + using zlib library: ../zlib-1.2.11
 
 nginx path prefix: "/home/admin/nginx"
 nginx binary file: "/home/admin/nginx/sbin/nginx"
 nginx modules path: "/home/admin/nginx/modules"
 nginx configuration prefix: "/home/admin/nginx/conf"
 nginx configuration file: "/home/admin/nginx/conf/nginx.conf"
 nginx pid file: "/home/admin/nginx/logs/nginx.pid"
 nginx error log file: "/home/admin/nginx/logs/error.log"
 nginx http access log file: "/home/admin/nginx/logs/access.log"
 nginx http client request body temporary files: "client_body_temp"
 nginx http proxy temporary files: "proxy_temp"
 nginx http fastcgi temporary files: "fastcgi_temp"
 nginx http uwsgi temporary files: "uwsgi_temp"
 nginx http scgi temporary files: "scgi_temp"

compile


make

The installation


sudo yum install nginx 
0

Set the permissions

Since Linux is set to a normal user and cannot occupy the port under 10241, a failure will occur if nginx is started directly. Assign nginx to root users, assigning special permissions.


sudo yum install nginx 
1

conclusion


Related articles: