preface

This chapter is based on Centos 7.x

This chapter explains how to use NGINX when a project is deployed online. Some children’s shoes in the online Baidu similar LNMP installation followed the command a execution, if there is no error is ok, once the error is mengbi state. This is irresponsible to yourself and to your code. This chapter takes you through the “serious” installation of NGINX.

download

Download the NGINX source code package, do not look for a good NGINX website there, why blindly search?

Source code package download address: nginx.org/en/download…

  • Mainline Version Mainline Version, also a development Version test Version, do not download if you do not have enemies
  • Stable Version Indicates a Stable version. You can find the Stable version for downloading
  • Legacy Versions Indicates the previous version

The actual version does not have too many requirements, download stable version can, each version is divided. Gz source code package and. Exe Windows installation package, can download the game will not download a compression package?

Many people struggle with where to put the installation package, now tell you

/usr/src
Copy the code

Usr used to stand for user, but that’s not what it means anymore. No one’s gonna die in this place.

Wget HTTP: / / http://nginx.org/download/nginx-1.14.0.tar.gzCopy the code

Wget is simpler than eating.

And then it starts downloading, downloading, okay? Never downloaded a game?

The installation

After downloading nginx-1.14.0.tar.gz in /usr/src, there is a “scrub” command

unzip

Tar Decompress command

parameter instructions
-z Special decompression of gzip compression, did not see the compression package is. Gz
-x Unpack the
-v Decompression process, is to solve what file
-f Point to the file. Make sure it’s last
The tar - ZXVF nginx - 1.14.0. Tar. GzCopy the code

Then is the decompression of kaka kaka, and then out of some do not understand the file

Nginx-1.14.0 / nginx-1.14.0/auto/ nginx-1.14.0/conf/ nginx-1.14.0/ nginx-1.14.0/ SRC/nginx-1.14.0/configure README nginx-1.14.0/ HTML/nginx-1.14.0/man/ nginx-1.14.0/ changes.ru Nginx - 1.14.0 / CHANGES nginx - 1.14.0 / man/nginx. 8 nginx - 1.14.0 / HTML / 50 x. HTML nginx 1.14.0 / HTML/index HTML Nginx-1.14.0 / SRC /core/ nginx-1.14.0/ SRC /event/ nginx-1.14.0/ SRC/HTTP/nginx-1.14.0/ SRC /mail/ nginx-1.14.0/ SRC /misc/ Nginx 1.14.0 / SRC/OS/nginx 1.14.0 / SRC/stream/nginx - 1.14.0 / SRC/stream/ngx_stream_geo_module. C Nginx 1.14.0 / SRC/stream/ngx_stream c nginx - 1.14.0 / SRC/stream/ngx_stream. H Nginx 1.14.0 / SRC/stream/ngx_stream_limit_conn_module c nginx - 1.14.0 / SRC/stream/ngx_stream_access_module. C...Copy the code

You now have an nginx-1.14.0 folder in your /usr/src directory

Nginx - 1.14.0 nginx - 1.14.0. Tar. GzCopy the code

./configure

Directory is now installed, do not baidu do not Google, official website documents write so clear stem that redundant work has what to use. Primary school English can understand. Nginx.org/en/docs/con… Scroll down to the bottom, and there’s a demo on the official website

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=.. / pcre - 8.41 - with - zlib =.. / zlib - 1.2.11Copy the code

That’s how it’s installed, and if you don’t want to know about it, you can

./configure
Copy the code

That’s right, nothing, 9 letters to end the fight, why not add parameters? Because nginx has default values for many parameters.

parameter The default annotation
–prefix=path /usr/local/nginx Nginx installation path
–sbin-path=path prefix/sbin/nginx Nginx command path
–modules-path=path prefix/modules Nginx module path
–conf-path=path prefix/conf/nginx.conf Nginx configuration file path
–error-log-path=path prefix/logs/error.log Nginx default log directory

. / configure

Checking for OS + Linux 3.10.0-514.26.2.el7.x86_64 X86_64 Checking for C compiler... Checking for Gcc-PIPE Switch Found + using GNU C Compiler + GCC version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) Checking for Gcc-PIPE Switch... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found checking for gcc variadic macros ... found checking for gcc builtin 64 bit byteswap ... found checking for unistd.h ... found checking for inttypes.h ... found checking for limits.h ... found checking for sys/filio.h ... not found checking for sys/param.h ... found checking for sys/mount.h ... found checking for sys/statvfs.h ... found checking for crypt.h ... found checking for Linux specific features checking for epoll ... found checking for EPOLLRDHUP ... found checking for EPOLLEXCLUSIVE ... not found checking for O_PATH ... found checking for sendfile() ... found checking for sendfile64() ... found ....Copy the code

Check whether checking is supported by configure. Check whether checking is supported by configure. Have you installed all the dependencies? After the test is successful, you can proceed to the next step, otherwise you will still be reminded that XXX does not exist, XXX is not supported and so on

make && make install
Copy the code

Make is a compilation tool, just remember it’s a compilation tool, the output of the compilation process

cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_log.o \ src/core/ngx_log.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I  src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_palloc.o \ src/core/ngx_palloc.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_array.o \ src/core/ngx_array.c .... cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: exit directory '/usr/ SRC /nginx-1.14.0 'Copy the code

Finally, he copies the required files into the specified directory, and the nginx installation is almost complete.

configuration

If only this command is executed

./configure
Copy the code

The nginx directory is

/usr/local/nginx
-----------
cert  client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
Copy the code

All of the configuration described below is done in nginx.conf

Change the default directory

Uncomment the default parsing directory HTML and just comment it out. I usually point the project directory to /var/www in the virtual host file

Avoid generic parsing

There will always be someone who resolves a domain name to the wrong address or maliciously to your server. Such people must be severely punished

server {
    listen       80  default_server;
    server_name  _;
    return       403;
}
Copy the code

Screen him every minute

Adding a Virtual Host

Don’t write all the configuration files of the virtual host to nginx.conf

include /usr/local/nginx/conf/server/*.conf;
Copy the code

Configure Gzip compression

Nginx.org/en/docs/htt…

Adding SSL Support

Segmentfault.com/a/119000001…

Enabling Logging

Segmentfault.com/a/119000001…

Enabling Load Balancing

Segmentfault.com/a/119000001…

Enabling reverse Proxy

Segmentfault.com/a/119000001…

Set the permissions

It is best to set the nginx command to allow root users or your company’s operations to execute, nginx -s stop for no reason can not tolerate.

Official document

The nginx website is the most brief and detailed document I have ever written. The detailed addresses of each section are listed below

  • Compile configuration parameters nginx.org/en/docs/con…
  • Virtual host configuration nginx.org/en/docs/htt…
  • The measurement unit in the configuration file nginx.org/en/docs/syn…
  • Nginx command parameters nginx.org/en/docs/swi…
  • Nginx load balancing configuration nginx.org/en/docs/htt…
  • Nginx official blog www.nginx.com/blog/

Thank you

Thank you for reading this article, my language is too extreme, but also hope to forgive. I hope this article has helped you, and feel free to leave any questions in the comments section. thank you

** Don’t be afraid of English, primary school English level can understand, everything comes from persistence

communication

There is life, there is code.

Spread the positive energy of technology and continue to learn new knowledge.