Home >Operation and Maintenance >Nginx >How to deploy Nginx service

How to deploy Nginx service

王林
王林forward
2023-05-12 10:13:171601browse

1. Introduction to Nginx:

1.1 What is Nginx?

Nginx ("engine x") is an open source that supports high-performance, high-concurrency www service and proxy service software.

was developed by Russian Igor Sysoev and was originally used on the large Russian website www.rambler.ru.

Nginx has the characteristics of high concurrency and low system resource usage.

Nginx can run on UNIX, Linux, DSB, Mac OS X, Solaris and Windows operating systems.

1.2 Nginx main features

Supports high concurrency: can support tens of thousands of concurrent connections

Low resource consumption: under 30,000 concurrent connections, the first 10 threads consume less than 10% of memory 200MB.

Can do HTTP reverse proxy and accelerated caching, that is, load balancing function, built-in health check function for RS node server

Have caching function of professional caching software such as Squid

Support asynchronous network I/O event model

1.3 Main functional applications of Nginx software

As Web service software

Reverse proxy and load balancing service

Front-end business data caching service

2. Nginx Web service

2.1 Nginx as a web server application scenario

Use Nginx to run HTML, JS, CSS, small pictures and other static data

Nginx combines with FastCGI to run PHP and other dynamic programs

Nginx combines Tomcat/Resin, etc. to support Java dynamic programs

2.2 How to choose a Web server

At work, according to Need to choose the appropriate business service software:

  • Static business: High concurrency scenarios, Nginx is preferred

  • Dynamic business: Both Nginx and Apache Yes, it is recommended that Nginx

  • static dynamic business: recommended Nginx

##3 Compile and install Nginx

There are many installation methods, this article Use the compile and install method. If large-scale deployment is required, the rpm package can be customized according to business requirements and then installed through Ansible.

3.1 Install pcre library

Check the current system version:

cat /etc/redhat-release
uname -r

Result:

CentOS release 6.10 (Final)
2.6.32-754.el6.x86_64

Install pcre using yum method:

yum -y install pcre pcre-devel
rpm -qa pcre pcre-devel

Result:

pcre-devel-7.8-7.el6.x86_64

pcre-7.8-7.el6.x86_64

3.2 Install Nginx

Check whether openssl and openssl-devel are installed:

rpm -qa openssl openssl-devel

Result: If not, use yum to install

openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64

Create the nginx package storage directory:

mkdir -p /app/nginx-1.8.1
mkdir -p /server/tools
cd /server/tools/

Download nginx software Package:

Official address: www.nginx.rog

wget -q http://nginx.org/download/nginx-1.8.1.tar.gz

Create nginx user:

useradd nginx -s /sbin/nologin -M

Unzip the software package and enter the unzipped directory:

tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1

Compile:

The compiled module can be viewed through ./configure --help

./configure --user=nginx --group=nginx --prefix=/app/nginx-1.8.1/ --with-http_stub_status_module --with-http_ssl_module

Installation:

make
make install

Create a soft link: convenient for use and version upgrade

ln -s /app/nginx-1.8.1/ /app/nginx

Pre-start test:

/app/nginx/sbin/nginx -t

Result:

nginx: the configuration file /app/nginx-1.8.1//conf/nginx.conf syntax is oknginx: configuration file /app /nginx-1.8.1//conf/nginx.conf test is successful

Start the Nginx service and check the port:

/app/nginx/sbin/nginx
netstat -utpln | grep 80

Result:

tcp 0 0 0 0.0.0.0:80 0.0.0.0:* curl 192.168.1.31

Result:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h2>Welcome to nginx!</h2>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/" rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/" rel="external nofollow" >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4. Nginx directory structure and configuration file

4.1 Nginx directory structure description

tree /app/nginx
/app/nginx
├── client_body_temp
├── conf							#nginx配置文件目录
│   ├── fastcgi.conf				#fastcgi相关参数配置文件
│   ├── fastcgi.conf.default
│   ├── fastcgi_params				#fastcgi参数文件
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types					#媒体类型
│   ├── mime.types.default
│   ├── nginx.conf					#Nginx主配置文件
│   ├── nginx.conf.default
│   ├── scgi_params					#scgi配置文件
│   ├── scgi_params.default
│   ├── uwsgi_params				#uwsgi配置文件
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp					#fastcgi临时数据文件
├── html							#默认站点目录
│   ├── 50x.html					#错误页面显示文件
│   └── index.html					#默认的站点首页文件
├── logs							#默认日志路径
│   ├── access.log					#默认访问日志文件
│   ├── error.log					#默认错误日志文件
│   └── nginx.pid					#Nginx的pid文件
├── proxy_temp						#临时目录
├── sbin							#Nginx命令目录
│   ├── nginx						#启动命令
│   └── nginx.old
├── scgi_temp						#临时目录
└── uwsgi_temp						#临时目录

9 directories, 22 files
4.2 Nginx main configuration file

Go to comment and display the configuration file:

egrep -v "#|^$" /app/nginx/conf/nginx.conf.default

Result:

worker_processes  1;                            #worker进程数量
events {                                        #事件区块开始
    worker_connections  1024;                    #单worker进程支持的最大连接
}                                                #事件区块结束
http {                                            #HTTP区块开始
    include       mime.types;                    #支持的媒体类型库
    default_type  application/octet-stream;        #默认媒体类型
    sendfile        on;                            #开启高效传输模式
    keepalive_timeout  65;                        #连接超时
    server {                                    #server区块开始
        listen       80;                        #服务端口,默认80
        server_name  localhost;                    #域名主机名
        location / {                            #location区块开始
            root   html;                        #站点根目录
            index  index.html index.htm;        #默认首页文件
        }                                        #location区块结束
        error_page   500 502 503 504  /50x.html;#对应状态码及回应
        location = /50x.html {                    #location开始回应50x.html
            root   html;                        #站点目录为html
        }                                        
    }
}                                                #HTTP区块结束

Note: There can be multiple server blocks and location blocks.

The above is the detailed content of How to deploy Nginx service. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete