Correct steps to install Nginx in Linux

Correct steps to install Nginx in Linux

Preface

If you are like me, as a hard-working Java backend, in addition to implementing a large number of project functions, you also have to take into account the deployment and operation and maintenance of the project. Install a new Nginx on a new server. Check out the online tutorials before installation. Faced with a variety of tutorials and installation methods, you will always wonder which method is the best, or which method is most suitable for you? Next, let's analyze the various Nginx installation methods and see which situation each is suitable for.

Install using system binary source

On Ubuntu/Debian

sudo apt-get install nginx

Or RedHat/CentOS

sudo yum install nginx

This method is the simplest and quickest way, but not the best way. Let’s talk about this main problem below.

advantage

  • All installation binary dependencies have been handled, no need to worry about compatibility issues, it can be used out of the box
  • Nginx connection configuration, user permissions, you don't need to deal with this, it has been written for you
  • Don't worry about Nginx Bug maintenance and upgrades, just get the latest system
  • Uninstallation is simple, just one command
  • Log maintenance is simple, automatically truncate the log of the day, and compress and save

shortcoming

  • Unable to choose the installation version independently
  • Cannot choose compiled modules independently
  • Extending functionality becomes cumbersome and requires recompilation
  • The directory structure is complex, the configuration files are in /etc/, and the deployment files are in /var/www
  • Restarting services and modifying configurations require root permissions
  • Compile and install with poor performance

If you are a Linux novice, it is absolutely recommended to use this installation method. You don’t need to worry about compilation dependencies and can use it directly after installation. However, if your server is used in a production environment and is being developed and gradually improved, this method is not recommended. Third-party modules may be added in the future, and they will definitely need to be compiled and installed (described below). When restarting the server, do not use the root user, but use sudo to temporarily gain root. If your server is used to deploy static files and web spaces, and you usually use FTP tools to deploy files, there will be no problem using this method.

Compile and install

I won’t write about the pros and cons, basically just reverse the above. To install using this method, you must have some knowledge of Linux compilation, and only moderate Linux users can handle it. I have seen that most tutorials online install compilation dependencies directly in /usr/local/, which is not a good approach. If we want to uninstall these dependencies in the future, we will find it very troublesome. It is not possible to simply delete the directory category. Some Linux distributions will write the installation files into configuration files, and I don’t know where to find these configuration files. If the dependent version affects other software, how to deal with the version problem. We just wanted to install Nginx, but it led to a lot of problems.

Compilation environment preparation

Before you start, make sure you have gcc, make, wget, and g++ installed on your Linux system.

Create a directory to store downloaded files, enter the directory to download the dependent library source files

Download openssl mainly for ssl module encryption, support http

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

Download pcre to implement support for address redirection, address rewriting, localtion instructions, and regular expressions

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

Download 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

Unzip all the files using tar

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

Compile options

A script that sets various Nginx parameters using ./configure, including paths to source and configuration files, compiler options, connection handling methods, and module lists. The script finishes by creating the Makefiles required to compile the code and install Nginx Open Source.

parameter describe
--prefix=<PATH> The base location for the Nginx installation directory, as well as all relative paths set by other configure script options. Default value /usr/local/nginx
–sbin-path=<PATH The name of the Nginx binary execution file, default value: <prefix>/sbin/nginx
--conf-path=<PATH> The name of the Nginx configuration file. However, you can always override this value at startup by specifying a different file using the option on the nginx command line. Default: <prefix>conf/nginx.conf -c <FILENAME>
--pid-path=<PATH> The name of the nginx.pid file, which is used to store the process ID of the nginx master process. After installation, the path to the file name can be changed using the pid directive in the Nginx configuration file. Default value: <prefix>/logs/nginx.pid
--error-log-path=<PATH> The name of the log file for error, warn, and diagnostic data. After installation, the file name can be changed using the error_log directive in the Nginx configuration file. Default value: <prefix>/logs/error.log
–http-log-path=<PATH> The name of the main log file for HTTP server requests. After installation, the file name can always be changed using the access_log directive in the Nginx configuration file. Default value: <prefix> /logs/access.log
--user=<NAME> The owner of the Nginx running process. After installation, the name can be changed using the user directive in the Nginx configuration file. Default: nobody
–group=name The owner user group of the nginx running process. After installation, the name can be changed using the user directive in the NGINX configuration file. Default value: the value set by the --user option
--with-pcre=<PATH> Path to the PCRE library source code, which is required for regular expression support in the location directive and the Rewrite module
--with-pcre-jit Build the PCRE library with "Just-In-Time Compilation" support (pcre_jit directive)
--with-zlib=<PATH> The source code path of the zlib library, which is required by the Gzip module
–with-http_ssl_modul Enable HTTPS support
–with-http_v2_module Enable HTTP/2 request support

There are too many compilation parameters that I will not list one by one. Students who are interested can go to

See nginx official website

Compile and install

./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, indicating that the dependency is 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

Install

make install

Set permissions

Because Linux sets ordinary users and cannot occupy ports below 1024, starting nginx directly will result in insufficient permissions errors. Assign nginx to the root user and assign special permissions.

sudo chown root nginx
sudo chmod u+s nginx

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to quickly install Nginx in Linux
  • Detailed steps to install Nginx on Linux
  • Tutorial on installing nginx in Linux environment

<<:  Simple usage of MySQL temporary tables

>>:  Example of Vue transition to achieve like animation effect

Recommend

Practical record of vue using echarts word cloud chart

echarts word cloud is an extension of echarts htt...

Detailed steps for using jib for docker deployment in Spring Cloud

Introduction to Jib Jib is a library developed by...

Centos7 installation of Nginx integrated Lua sample code

Preface The computer I use is a Mac, and the oper...

React's transition from Class to Hooks

Table of contents ReactHooks Preface WhyHooks? Fo...

How does MySQL ensure master-slave consistency?

Table of contents The basic principle of MySQL ma...

Implementation of crawler Scrapy image created by dockerfile based on alpine

1. Download the alpine image [root@DockerBrian ~]...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

MySQL Index Optimization Explained

In daily work, we sometimes run slow queries to r...

A comprehensive understanding of Vue.js functional components

Table of contents Preface React Functional Compon...

Correct steps to install Nginx in Linux

Preface If you are like me, as a hard-working Jav...

Detailed explanation of how to synchronize data from MySQL to Elasticsearch

Table of contents 1. Synchronization Principle 2....

jQuery realizes the full function of shopping cart

This article shares the specific code of jQuery t...

How to write high-quality JavaScript code

Table of contents 1. Easy to read code 1. Unified...

How to export mysql table structure to excel

The requirements are as follows Export the table ...