gaofeng0210 发表于 2018-11-10 09:53:12

nginx简介及安装

  如果你听说过apache软件,那么就会很快熟悉nginx软件,与apache软件类似,nginx(engine x)是一个开源的,支持高性能,高并发的www的服务和代理服务软件
  nginx因具有高并发(特别是静态资源)、占用系统资源少等,且功能丰富
  在功能应用方面,nginx不但是一个优秀的web服务软件,还具有反向代理负载均衡和缓存服务功能,在反向代理、负载均衡方面,它类似于大名鼎鼎的LVS负载均衡及HAPROXY等专业代理软件,但是NGINX部署起来更方便,在缓存服务功能方面,它又类似于Squid等专业的缓存服务软件
  总结一下nginx的几大功能:
  1)www web服务
  2)负载均衡(也可以叫做反向代理,也可以叫做proxy)
  3)web 缓存(相当于squid,CDN主要使用squid)
  nginx特点:
  1、配置简单、灵活、轻量
  2、高并发(静态小文件),几万的并发
  3、占用资源少 2W并发,开10个线程服务,内存才消耗几百M
  nginx安装
  1.安装nginx需要安装pcre库与openssl
  pcre的全称为 perl compatible regular expressions ,中文译为“perl兼容正则表达式”,安装pcre库是为了使nginx支持具备URL重写功能的rewrite模块,如果不安装pcre库,则nginx无法使用rewrite功能,nginx的rewrite模块功能几乎是企业应用必须的
  yum install -y pcre pcre-devel
  rpm -qa pcre pcre-devel
  备注:rpm安装的pcre版本有些低,不过一般情况下不影响使用
  openssl是加密的作用,nginx在使用https服务的时候会使用到此模块
  yum install openssl openssl-devel
  rpm -qaopenssl openssl-devel
  
  依赖包安装
  yum -y install gcc gcc-c++ automake autoconf zlib zlib-devel openssl openssl-devel pcre pcre-devel
  
  yum install libjpeg libjpeg-devel -y
  yum install libxml2 libxml2-devel zlib zlib-develncurses ncurses-devel curl curl-devel -y
  yum install gd gd2 gd-devel gd2-devel -y
  
  有一些包是为了以后做nginx负载均衡而安装的
  
  
  2.选择并下载合适的nginx版本
  nginx官方网站为:www.nginx.org 可以看到nginx的所有版本
  nginx的软件有三种版本,稳定版、开发版、及历史稳定版
  选择标准如下:
  开发版更新快,新功能多,但bug多
  稳定版的更新很慢,但是bug较少,可以做为企业生产环境的首选。
  但是在实际工作中,尽量避免使用最新的稳定版,应该选择比已出版本来的最新版本晚6-10个月的版本比较好
  官网的解释是这样的:
  Nginx官网提供了三个类型的版本
  Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
  Stable version:最新稳定版,生产环境上建议使用的版本
  Legacy versions:遗留的老版本的稳定版
  综上所述,我们选择 1.8.1
  3.开始安装nginx
  mkdir -p /app/3rd/nginx
  cd /app/3rd/nginx
  wget http://nginx.org/download/nginx-1.8.1.tar.gz
  4.nginx优化选项
  内存优化jemalloc
  wget -P /opthttp://mirrors.linuxeye.com/oneinstack/src/jemalloc-4.2.1.tar.bz2
  cd /app/packages
  tar jxf jemalloc-4.2.1.tar.bz2
  cd jemalloc-4.2.1
  ./configure && make -j `grep processor /proc/cpuinfo|wc -l` && make install
  echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
  ldconfig
  查看jemalloc运行情况
  lsof -n | grep jemalloc
  (此项需要nginx安装启动完毕后才会可以看到)
  
  5.编译
  useradd -M -s /sbin/nologin www   
  
  ./configure --help 查看编译选项
  ./configure --prefix=/app/zpy/nginx/ \
  --user=www \
  --group=www \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-http_gzip_static_module \
  --with-http_realip_module \
  --with-http_flv_module \
  --with-pcre \
  --with-pcre-jit \
  --with-ld-opt='-ljemalloc'
  查看编译结果:
  echo $?
  0
  安装
  make
  make install
  6.启动nginx
  1)检查nginx的配置文件及语法:
  /app/zpy/nginx/sbin/nginx -t
  nginx: the configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf syntax is ok
  nginx: configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf test is successful
  2)检查80端口是否被占用:
  # netstat -tunpl | grep 80
  tcp      0      0 :::80       :::*      LISTEN      20804/httpd
  发现80已经被httpd apache占用了,所以我们停止 httpd
  service httpd stop
  chkconfig httpd off
  3)启动
  /app/zpy/nginx/sbin/nginx
  # netstat -tunpl | grep 80
  tcp   0    0 0.0.0.0:80    0.0.0.0:*   LISTEN      8398/nginx
  浏览器输入http://10.0.70.3可以看到nginx页面
  或者 curl 10.0.70.3
  如果你去一个公司之后发现nginx已经部署好了,你想知道nginx版本或者想知道nginx安装的时候使用了哪些模块怎么办??
  执行如下命令: /app/zpy/nginx/sbin/nginx -V
  输出:
  nginx version: nginx/1.8.1
  built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
  built with OpenSSL 1.0.1e-fips 11 Feb 2013
  TLS SNI support enabled
  configure arguments: --user=www --group=www --prefix=/app/zpy/nginx/ --with-http_stub_status_module --with-http_ssl_module

页: [1]
查看完整版本: nginx简介及安装