1 前言
随着web应用在系统维护工作中的日益增加,为了统一web维护中的nginx的安装部署,特编写此份文档。
2 适用范围及对象
l 本手册是用于web系统维护人员。
l 单台nginx的安装请参见nginx安装部分。
l 多台nginx的部署请参见nginx的部署及迁移部分。
3 操作系统安装及设置
3.1 操作系统安装
本文档软件安装基于Rhel 5.8 X86_64 系统,系统安装详见Rhe5系统安装手册。
3.2 服务器优化
3.2.1 系统校时
所有服务器都需要精确的时间,所以要保证服务器时间准确,必须设置自动对时功能。
与时间授权服务器进行时间同步
# ntpdate 203.117.180.36
# hwclock -w
为了保证每天都能正确对时,请在crontab中加入:
0 7 * * * /usr/sbin/ntpdate 203.117.180.36
1 7 * * * /usr/sbin/hwclock -w
3.2.2 服务器优化
所有服务器都有进行必要的优化
修改/root/.bashrc文件,在文件最后加入:
ulimit -c unlimited
ulimit -n 204800
退出重新登陆使上面的参数生效。
3.2.3 yum源配置
# mv /etc/yum.repos.d/rhel-debuginfo.repo rhel-debuginfo.repo.bak
# vi /etc/yum.repos.d/CentOS-Base.repo
[base] name=CentOS-$releasever - Base baseurl=http://mirrors.163.com/centos/5/os/$basearch/ gpgcheck=1 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-5
#released updates [update] name=CentOS-$releasever - Updates baseurl=http://mirrors.163.com/centos/5/updates/$basearch/ gpgcheck=1 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-5
#packages used/produced in the build but not released [addons]
name=CentOS-$releasever - Addons baseurl=http://mirrors.163.com/centos/5/addons/$basearch/ gpgcheck=1 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-5
#additional packages that may be useful [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.163.com/centos/5/extras/$basearch/ gpgcheck=1 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-5
#additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus baseurl=http://mirrors.163.com/centos/5/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-5
#contrib - packages by Centos Users [contrib] name=CentOS-$releasever - Contrib baseurl=http://mirrors.163.com/centos/5/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-5 |
4 nginx安装
4.1 安装nginx依赖程序
l yum安装部分
# yum install pcre pcre-devel gd gd-devel GeoIP GeoIP-devel
注:也可以将以上程序的rpm包下载后独立安装。(独立安装包如下)
pcre-6.6-9.el5.x86_64.rpm pcre-devel-6.6-9.el5.x86_64.rpm gd-2.0.33-9.4.el5_4.2.x86_64.rpm gd-devel-2.0.33-9.4.el5_4.2.x86_64.rpm GeoIP-1.4.5-1.el5.centos.x86_64.rpm GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm |
l 编译安装部分
安装libunwind和google-perftools
# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-beta.tar.gz
# tar -xzvf libunwind-0.99-beta.tar.gz
# cd libunwind-0.99-beta
# ./configure
# make
# make install
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# ldconfig
# wget http://down1.chinaunix.net/distfiles/google-perftools-1.9.tar.gz
# tar -xzvf google-perftools-1.9.tar.gz
# cd google-perftools-1.9
# ./configure
# make
# make install
# ln -s /usr/local/lib/libprofiler.so.0 /lib64/libprofiler.so.0
# ldconfig
4.2 nginx配置及编译
4.2.1 创建用户和组
# groupadd users
# useradd -g users -s /bin/false -M webuser
# mkdir -p /opt/nginx/temp/client_body_temp
4.2.2 stable版nginx编译配置选项
# tar -xzvf nginx-1.6.0.tar.gz
# cd nginx-1.6.0
# ./configure \
--user=webuser \
--group=users \
--prefix=/opt/nginx \
--with-http_addition_module \
--with-google_perftools_module \
--with-http_flv_module \
--with-http_geoip_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-poll_module \
--http-client-body-temp-path=/opt/nginx/temp/client_body_temp \
--http-fastcgi-temp-path=/opt/nginx/temp/fastcgi_temp \
--http-proxy-temp-path=/opt/nginx/temp/proxy_temp \
--http-scgi-temp-path=/opt/nginx/temp/scgi_temp \
--http-uwsgi-temp-path=/opt/nginx/temp/uwsgi_temp
4.2.3 编译及安装
# make && make install
4.2.4 运行、停止及重新加载nginx
l 运行nginx
# /opt/nginx/sbin/nginx
l 停止nginx
# /opt/nginx/sbin/nginx -s stop
l 重新加载nginx配置
# /usr/sbin/nginx -s reload
至此,已经完成了nginx的安装部署。
5 nginx部署及迁移
5.1 部署及迁移场景
l 批量部署
生产环境中有时需要同时在相同硬件配置的大批量服务器上进行nginx安装,需要进行批量部署。
l 迁移
生产环境中有时需要对已有的nginx服务器进行程序和数据迁移,将程序及数据迁移至硬件配置更高的服务器上,需要进行迁移。
5.2 部署及迁移的步骤
5.2.1 依赖程序的部署及迁移
l 相关依赖程序部署迁移
将首台安装的nginx服务器上将yum源配置文件压缩并复制到新服务器上在新服务器上进行解压并复制到/etc/yum.repos.d/目录中。
在首台安装的服务器上
# cd /etc/yum.repos.d
# tar zcvf CentOS-Base.repo rhel.tar.gz
在新服务器上
# tar zxvf rhel.tar.gz
# cp CentOS-Base.repo /etc/yum.repos.d/
将原rhel的yum官方源配置文件改名后保存
# mv /etc/yum.repos.d/rhel-debuginfo.repo rhel-debuginfo.repo.bak
使用Centos源安装相关依赖程序
# yum install pcre pcre-devel gd gd-devel GeoIP GeoIP-devel
注:也可以将s以上程序的rpm包下载后独立安装。(独立安装包如下)
pcre-6.6-9.el5.x86_64.rpm pcre-devel-6.6-9.el5.x86_64.rpm gd-2.0.33-9.4.el5_4.2.x86_64.rpm gd-devel-2.0.33-9.4.el5_4.2.x86_64.rpm GeoIP-1.4.5-1.el5.centos.x86_64.rpm GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm |
l 相关依赖库部署迁移
将首台安装的nginx服务器上的libunwind和google-perftools安装所生成的库文件压缩并复制到新服务器上进行解压。
在首台安装的服务器上
# cd /usr/local/lib
# tar zcvf libs.tar.gz *
在新服务器上
# cd /usr/local/lib
# tar zxvf libs.tar.gz
因为默认的libunwind安装在/usr/local/lib目录下,需要将这个目录添加到系统动态库缓存中。
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
对google-perftools 库文件做符号链接。
# ln -s /usr/local/lib/libprofiler.so.0 /lib64/libprofiler.so.0
# ldconfig
l 创建用户、用户组及相关目录
在新服务器上
# groupadd users
# useradd -g users -s /bin/false -M webuser
5.2.2 nginx程序及应用数据的部署及迁移
l nginx安装程序部署及迁移
将首台安装的nginx程序目录进行压缩并复制到新服务器上进行解压。
在首台安装的服务器上
# cd /opt
# tar zcvf nginx.tar.gz nginx
在新服务器上
# cd /opt
# tar zxvf nginx.tar.gz
# mkdir -p /opt/nginx/temp/client_body_temp
l web应用的部署及迁移
web应用程序一般都部署在nginx程序的html目录下,可以将应用程序压缩后复制到新服务器的相同目录解压
到此,部署迁移已经完成,运行、停止与重载,请参考4.2.4。
6 Nginx的日常维护
6.1 Nginx基本信息检查
6.1.1 检查Nginx配置文件的正确性
Nginx提供的配置文件调试功能非常有用,可以快速定位配置文件存在的问题。执行如下命令检测配置文件的正确性:
# /opt/nginx/sbin/nginx –t
或者
# /opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
其中,“-t”参数用于检查配置文件是否正确,但并不执行。“-c”参数用于指定配置文件路径,如果不指定配置文件路径,Nginx默认会在安装时指定的安装目录下查找conf/nginx.conf文件。
如果检测结果显示如下信息,说明配置文件正确。
the configuration file /opt/nginx/conf/nginx.conf syntax is ok
configuration file /opt/nginx/conf/nginx.conf test is successful
6.1.2 显示Nginx的版本以及相关编译信息
在命令行执行以下命令可以显示安装Nginx的版本信息。
# /opt/nginx/sbin/nginx -v
执行以下命令显示安装的Nginx版本和相关编译信息:
# /opt/nginx/sbin/nginx -V
不但显示Nginx的版本信息,同时显示nginx在编译时指定的相关模块信息。
6.2 备份配置文件
备份配置文件,该命令会生成一个以当前日期时间为后缀的备份,后缀形如1405121832,
14年5月12日18点32分。
# cp -R /opt/nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf.`date +%y%m%d%H%M`
程序备份要求:每天定时备份nginx的所有程序。
6.3 日志管理
每天分割一下日志,否则用不了多久日志文件大小就会达到GB级别。首先把日志文件复制到其他目录,然后nginx重新生成日志。
shell脚本如下:
# vi /home/cut_nginx_log.sh
#!/bin/bash
logs_path="/opt/nginx/logs/"
nginx_id=`netstat -nlpt|grep nginx|awk '{print $7}'|cut -d'/' -f 1`
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 $nginx_id
代码中的/opt/nginx/logs/指的是nginx的log日志文件所在目录,生成了以昨天日期命名的日志文件。
为了达到每天自动分割的目的,在crontab中加入自动执行该脚本的计划任务。
1 0 * * * sh /home/cut_nginx_log.sh
这样就每天的0点1分自动把nginx日志重命名为日期格式,并重新生成今天的新日志文件。
1 附录:Nginx编译常用参数汇总
--help 显示本提示信息
--prefix=PATH 设定安装目录
--sbin-path=PATH 设定程序文件目录
--conf-path=PATH 设定配置文件(nginx.conf)目录
--error-log-path=PATH 设定错误日志目录
--pid-path=PATH 设定pid文件(nginx.pid)目录
--lock-path=PATH 设定lock文件(nginx.lock)目录
--user=USER 设定程序运行的用户环境
--group=GROUP 设定程序运行的组环境
--builddir=DIR 设定程序编译目录
--with-rtsig_module 允许rtsig模块
rtsig模块是一种实时信号,在Linux 2.2.19 默认情况下,实时信号连接数不超过1024,但是对于高负载是肯定不够的。因此通过调整内核参数/proc/sys/kernel/rtsig-max达到效果。但是Linux 2.6.6-mm2开始,这个参数不再可用,并为每个进程有一个独立的信号队列,数字是由RLIMIT_SIGPENDING确定。当队列变得满载时,nginx开始抛弃连接并使用poll方法,直到负载恢复正常。
--with-select_module 允许select模块(一种轮询模式,不推荐用在高载环境)
--without-select_module 不使用select模块
标准连接模式。默认情况下自动编译方式。您可以启用或禁用通过使用-select_module和不带- select_module配置参数这个模块。
--with-poll_module 允许poll模块(一种轮询模式,不推荐用在高载环境)标准连接模式。默认情况下自动编译方式。
--without-poll_module 不使用poll模块
--with-http_ssl_module 允许ngx_http_ssl_module模块(Apache对应:mod_ssl)
--with-http_realip_module 允许ngx_http_realip_module模块(mod_rpaf)此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用。
--with-http_addition_module 允许ngx_http_addition_module模块(mod_layout)游戏服务器不必安装,门户网站可以安装,有利于被搜索引擎收录页面信息。
--with-http_geoip_module 允许ngx_http_geoip_module模块,这个模块基于客户端的IP地址创建一些ngx_http_geoip_module变量,并与MaxMindGeoIP文件进行匹配。
--with-http_xslt_module 允许ngx_http_xslt_module模块
这个模块是一个过滤器,它可以通过XSLT模板转换XML应答。0.7.8后面版本才可以使用。
--with-http_sub_module 允许ngx_http_sub_module模块
这个模块可以能够在nginx的应答中搜索并替换文本。
--with-http_dav_module 允许ngx_http_dav_module模块(mod_dav)
为文件和目录指定权限,限制不同类型的用户对于页面有不同的操作权限
--with-http_flv_module 允许ngx_http_flv_module模块(mod_flvx)这个模块支持对FLV(flash)文件的拖动播放。
--with-http_secure_link_module 此模块计算和检查要求所需的安全链接网址。
--with-http_gzip_static_module 允许ngx_http_gzip_static_module模块(mod_dflate)。
这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩。
--with-http_random_index_module 允许ngx_http_random_index_module模块(mod_autoindex),从目录中选择一个随机主页
--with-http_stub_status_module 允许ngx_http_stub_status_module模块(mod_status)这个模块可以取得一些nginx的运行状态,如果是工业状况,可以直接取消。
--without-http_charset_module 不使用ngx_http_charset_module模块
这个模块将在应答头中为"Content-Type"字段添加字符编码
--without-http_gzip_module 不使用ngx_http_gzip_module模块,文件压缩模式。
--without-http_ssi_module 不使用ngx_http_ssi_module模块,此模块处理服务器端包含文件(ssi)的处理.
--without-http_userid_module 不使用ngx_http_userid_module模块
The module ngx_http_userid_module gives out cookies for identification of clients
--without-http_access_module 不使用ngx_http_access_module模块
--without-http_auth_basic_module 不使用ngx_http_auth_basic_module模块
--without-http_autoindex_module 不使用ngx_http_autoindex_module模块
--without-http_geo_module 不使用ngx_http_geo_module模块
这个模块基于客户端的IP地址创建一些ngx_http_geoip_module变量,并与MaxMindGeoIP文件进行匹配,该模块仅用于0.7.63和0.8.6版本之后。但效果不太理想,对于城市的IP记录并不是特别准确,不过对于网站的来源访问区域的分析大致有一定参考性。
--without-http_map_module 不使用ngx_http_map_module模块
这个模块允许你分类或者同时映射多个值到多个不同值并储存到一个变量中,可用于页面跳转其他域名使用。
--without-http_referer_module 不使用ngx_http_referer_module模块
当一个请求头的Referer字段中包含一些非正确的字段,这个模块可以禁止这个请求访问站点。可以禁止盗链的情况发生。
--without-http_rewrite_module 不使用ngx_http_rewrite_module模块,跳转模块
--without-http_proxy_module 不使用ngx_http_proxy_module模块,代理模块
--without-http_fastcgi_module 不使用ngx_http_fastcgi_module模块
--without-http_memcached_module 不使用ngx_http_memcached_module模块
--without-http_limit_zone_module 不使用ngx_http_limit_zone_module模块,此模块可以限制并发连接,达到减少攻击的效果
--without-http_empty_gif_module 不使用ngx_http_empty_gif_module模块,这个模块在内存中保存一个能够很快传递的1×1透明GIF
--without-http_browser_module 不使用ngx_http_browser_module模块,识别客户端浏览器版本来制定计划。可以实现更加来源浏览器的版本访问不同页面的效果。
--without-http_upstream_ip_hash_module,不使用ngx_http_upstream_ip_hash_module模块
--with-http_perl_module 允许ngx_http_perl_module模块,这个模块允许nginx使用SSI调用perl或直接执行perl
--with-perl_modules_path=PATH 设置perl模块路径
--with-perl=PATH 设置perl库文件路径
--http-log-path=PATH 设置access log文件路径
--http-client-body-temp-path=PATH 设置客户端请求临时文件路径
--http-proxy-temp-path=PATH 设置http proxy临时文件路径
--http-fastcgi-temp-path=PATH 设置http fastcgi临时文件路径
--http-scgi-temp-path=PATH 设置http scgi临时文件路径
--http-uwsgi-temp-path=PATH 设置http uwsgi临时文件路径
--without-http 不使用HTTP server功能,如果只是做代理服务器,可以不提供http服务
--with-mail 允许POP3/IMAP4/SMTP代理模块
--with-mail_ssl_module 允许ngx_mail_ssl_module模块
这个模块使得POP3/IMAP/SMTP可以使用SSL/TLS.配置已经定义了HTTP SSL模块,但是不支持客户端证书检测
--without-mail_pop3_module 不允许ngx_mail_pop3_module模块
--without-mail_imap_module 不允许ngx_mail_imap_module模块
--without-mail_smtp_module 不允许ngx_mail_smtp_module模块
--with-google_perftools_module 允许ngx_google_perftools_module模块(调试用)这个模块可以启用google性能分析工具套件,模块在版本0.6.29增加。
--with-cpp_test_module 允许ngx_cpp_test_module模块
--add-module=PATH 允许使用外部模块,以及路径
--with-cc=PATH 设置C编译器路径
--with-cpp=PATH 设置C预处理路径
--with-cc-opt=OPTIONS 设置C编译器参数
--with-ld-opt=OPTIONS 设置连接文件参数
--with-cpu-opt=CPU 为指定CPU优化,可选参数有:
pentium, pentiumpro, pentium3, pentium4,athlon, opteron, sparc32, sparc64, ppc64
--without-pcre 不使用pcre库文件,pcre是包括 perl 兼容的正规表达式库
--with-pcre=DIR 设定PCRE库路径
--with-pcre-opt=OPTIONS 设置PCRE运行参数
--with-md5=DIR 设定md5库文件路径
--with-md5-opt=OPTIONS 设置md5运行参数
--with-md5-asm 使用md5源文件编译
--with-sha1=DIR 设定sha1库文件路径
sha1:安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要。
--with-sha1-opt=OPTIONS 设置sha1运行参数
--with-sha1-asm 使用sha1源文件编译
--with-zlib=DIR 设定zlib库文件路径,很多程序中的压缩或者解压缩函数都会用到这个库
--with-zlib-opt=OPTIONS 设置zlib运行参数
--with-zlib-asm=CPU 使zlib对特定的CPU进行优化,可选参数:pentium, pentiumpro
--with-openssl=DIR 设定OpenSSL库文件路径
Openssl作为一个基于密码学的安全开发包,OpenSSL提供的功能相当强大和全面,囊括了主要的密码算法、常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用。
--with-openssl-opt=OPTIONS 设置OpenSSL运行参数
--with-debug 允许调试日志