千家信息网

如何部署Nginx服务

发表于:2024-09-28 作者:千家信息网编辑
千家信息网最后更新 2024年09月28日,本篇内容主要讲解"如何部署Nginx服务",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何部署Nginx服务"吧!1. Nginx介绍:1.1 Nginx
千家信息网最后更新 2024年09月28日如何部署Nginx服务

本篇内容主要讲解"如何部署Nginx服务",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何部署Nginx服务"吧!

1. Nginx介绍:

1.1 Nginx是什么?

Nginx("engine x")是一个开源的,支持高性能、高并发的www服务和代理服务软件。

由俄罗斯人Igor Sysoev开发,最初应用于俄罗斯大型网站www.rambler.ru上。

Nginx具有高并发、占用系统资源少等特性。

Nginx可以运行在UNIX、Linux、DSB、Mac OS X、Solaris及Windows等操作系统上。

1.2 Nginx主要特性

支持高并发:能支持几万并发连接

资源消耗少:三万并发连接下,开始10个线程消耗内存不到200MB。

可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能

具备Squid等专业缓存软件的缓存功能

支持异步网络I/O事件模型

1.3 Nginx软件的主要功能应用

作为Web服务软件

反向代理及负载均衡服务

前端业务数据缓存服务

2. Nginx Web服务

2.1 Nginx作为Web服务器应用场景

使用Nginx运行HTML、JS、CSS、小图片等静态数据

Nginx结合FastCGI运行PHP等动态程序

Nginx结合Tomcat/Resin等支持Java动态程序

2.2 如何选择Web服务器

工作中,根据需求来选择合适的业务服务软件:

  • 静态业务:高并发场景,首选采用Nginx

  • 动态业务:Nginx与Apache都可,建议Nginx

  • 静态+动态业务:推荐Nginx

3 编译安装Nginx

安装方法多种,本文使用编译安装方式。如果需要大规模部署,可将业务需求定制好rpm包,然后通过Ansible安装。

3.1 安装pcre库

查看当前系统版本:

cat /etc/redhat-releaseuname -r

结果:

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

采用yum方式安装pcre:

yum -y install pcre pcre-develrpm -qa pcre pcre-devel

结果:

pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64

3.2 安装Nginx

检查是否装有openssl、openssl-devel:

rpm -qa openssl openssl-devel

结果:如果没有,使用yum安装

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

创建nginx包存放目录:

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

下载nginx软件包:

官方地址:www.nginx.rog

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

创建nginx用户:

useradd nginx -s /sbin/nologin -M

解压软件包并进入解压后的目录:

tar xf nginx-1.8.1.tar.gzcd nginx-1.8.1

进行编译:
编译模块可以通过./configure --help查看

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

安装:

makemake install

创建软链接:方便使用以及版本升级

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

启动前测试:

/app/nginx/sbin/nginx -t

结果:

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

启动Nginx服务并检查端口:

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

结果:

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13689/nginx

检查Nginx启动结果:以下内容代表启动成功

curl 192.168.1.31

结果:

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

4. Nginx目录结构与配置文件

4.1 Nginx目录结构说明

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主配置文件

去注释显示配置文件:

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

结果:

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区块结束

注:server区块和location区块可以是多个。

到此,相信大家对"如何部署Nginx服务"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0