Nginx(“engine x”)是一个开源的,支持高性能、高并发的www服务和代理服务软件。
由俄罗斯人Igor Sysoev开发,最初应用于俄罗斯大型网站www.rambler.ru上。
Nginx具有高并发、占用系统资源少等特性。
Nginx可以运行在UNIX、Linux、DSB、Mac OS X、Solaris及Windows等操作系统上。
支持高并发:能支持几万并发连接
资源消耗少:三万并发连接下,开始10个线程消耗内存不到200MB。
可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能
具备Squid等专业缓存软件的缓存功能
支持异步网络I/O事件模型
作为Web服务软件
反向代理及负载均衡服务
前端业务数据缓存服务
使用Nginx运行HTML、JS、CSS、小图片等静态数据
Nginx结合FastCGI运行PHP等动态程序
Nginx结合Tomcat/Resin等支持Java动态程序
工作中,根据需求来选择合适的业务服务软件:
安装方法多种,本文使用编译安装方式。如果需要大规模部署,可将业务需求定制好rpm包,然后通过Ansible安装。
查看当前系统版本:
1 2 | cat /etc/redhat-release uname -r |
结果:
CentOS release 6.10 (Final)
2.6.32-754.el6.x86_64
采用yum方式安装pcre:
1 2 | 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
检查是否装有openssl、openssl-devel:
1 | rpm -qa openssl openssl-devel |
结果:如果没有,使用yum安装
openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64
创建nginx包存放目录:
1 2 3 | mkdir -p /app/nginx-1 .8.1 mkdir -p /server/tools cd /server/tools/ |
下载nginx软件包:
官方地址:www.nginx.rog
1 | wget -q http: //nginx .org /download/nginx-1 .8.1. tar .gz |
创建nginx用户:
1 | useradd nginx -s /sbin/nologin -M |
解压软件包并进入解压后的目录:
1 2 | tar xf nginx-1.8.1. tar .gz cd nginx-1.8.1 |
进行编译:
编译模块可以通过./configure --help查看
1 | . /configure --user=nginx --group=nginx --prefix= /app/nginx-1 .8.1/ --with-http_stub_status_module --with-http_ssl_module |
安装:
1 2 | make make install |
创建软链接:方便使用以及版本升级
1 | ln -s /app/nginx-1 .8.1/ /app/nginx |
启动前测试:
1 | /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服务并检查端口:
1 2 | /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
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!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 > < h1 >Welcome to nginx!</ h1 > < 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 > |
1 | tree /app/nginx |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /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 |
去注释显示配置文件:
1 | egrep - v "#|^$" /app/nginx/conf/nginx .conf.default |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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区块可以是多个。
以上就是小白也可以完成的0基础部署Nginx服务的详细内容,更多关于Nginx服务部署的资料请关注源码搜藏网其它相关文章!
热门源码