如何部署Nginx服务

发布时间:2022-03-21 18:46:58 作者:iii
来源:亿速云 阅读:230

本篇内容主要讲解“如何部署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服务器

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

3 编译安装Nginx

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

3.1 安装pcre库

查看当前系统版本:

cat /etc/redhat-release
uname -r

结果:

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

采用yum方式安装pcre:

yum -y install pcre pcre-devel
rpm -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_64
openssl-devel-1.0.1e-57.el6.x86_64

创建nginx包存放目录:

mkdir -p /app/nginx-1.8.1
mkdir -p /server/tools
cd /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.gz
cd 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

安装:

make
make 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/nginx
netstat -utpln | grep 80

结果:

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

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

curl 192.168.1.31

结果:

<!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目录结构与配置文件

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服务”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Centos 7部署Nginx网站服务
  2. saltstack部署nginx

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx

上一篇:CSS怎么把两个类选择器链接在一起

下一篇:PHP怎么操作SQL Server数据库实现表的改查与统计

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》