CentOS下编译安装Nginx 1.10.2
Nginx是一款轻量级的Web服务器/反向代理服务器,在连接高并发的情况下,Nginx是Apache服务器不错的替代品。
一、Nginx与Apache对比
Nginx
1、轻量级,采用C进行编写,同样的web服务,会占用更少的内存及资源;
2、抗并发,nginx以epollandkqueue作为开发模型,处理请求是异步非阻塞的,负载能力比apache高很多,而apache则是阻塞型的。在高并发下nginx能保持低资源低消耗高性能,而apache在PHP处理慢或者前端压力很大的情况下,很容易出现进程数飙升,从而拒绝服务的现象;
3、nginx处理静态文件好,静态处理性能比apache高三倍以上;
4、nginx的设计高度模块化,编写模块相对简单;
5、nginx配置简洁,正则配置让很多事情变得简单,而且改完配置能使用-t测试配置有没有问题,apache配置复杂,重启的时候发现配置出错了,会很崩溃;
6、nginx作为负载均衡服务器,支持7层负载均衡;
7、nginx本身就是一个反向代理服务器,而且可以作为非常优秀的邮件代理服务器;
8、启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动,还能够不间断服务的情况下进行软件版本的升级;
9、社区活跃,各种高性能模块出品迅速。
Apache
1、apache的rewrite比nginx强大,在rewrite频繁的情况下,用apache;
2、apache发展到现在,模块超多,基本想到的都可以找到;
3、apache更为成熟,少bug,nginx的bug相对较多;
4、apache超稳定;
5、apache对PHP支持比较简单,nginx需要配合其他后端用;
6、apache在处理动态请求有优势,nginx在这方面是鸡肋,一般动态请求要apache去做,nginx适合静态和反向;
7、apache仍然是目前的主流,拥有丰富的特性,成熟的技术和开发社区。
二、系统软件版本
1、系统:CentOS Linux release 7.2.1511 (Core)
2、Apache:nginx-1.10.2 //下载地址:http://nginx.org/en/download.html
3、OpenSSL:openssl-1.0.1e
三、编译前准备工作
1、新建nginx运行账户
groupadd nginxs //创建apaches组 useradd -M -N -s /sbin/nologin -G nginxs nginx //创建nginx用户并附加nginxs组,-M不建立用户目录,-N不创建同名组,-s执行shell禁止登录
2、创建apache日志文件夹(非必须,只是为了方便维护,生产环境建议自定目录,以免日志增长导致系统盘爆满)
mkdir /data/http mkdir /data/http/logs mkdir /data/http/html
3、YUM安装所需依赖(也可用rpm包安装,但需要解决更多依赖)
yum install gd gd-devel pcre pcre-devel perl-ExtUtils-Embed zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libxslt libxslt-devel GeoIP GeoIP-devel GeoIP-data
四、编译Nginx
tar -zxvf nginx-1.10.2.tar.gz //解压 ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/usr/local/nginx/run/nginx.pid --error-log-path=/data/http/logs/nginx_error.log --http-log-path=/data/http/logs/nginx_access.log --http-client-body-temp-path=/usr/local/nginx/tmp/client-body --http-proxy-temp-path=/usr/local/nginx/tmp/proxy --http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi --http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi --http-scgi-temp-path=/usr/local/nginx/tmp/scgi --user=nginx --group=nginxs --with-openssl= --with-http_ssl_module --with-http_perl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_v2_module --with-http_slice_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_degradation_module --with-pcre --with-stream --with-stream_ssl_module --with-ld-opt=-Wl,-E --with-threads --with-file-aio --with-ipv6 --with-debug make -j4 //使用4核心进行编译 make install nginx参数: --prefix=:指向安装目录 --sbin-path=:指向(执行)程序文件(nginx) --conf-path=:指向配置文件(nginx.conf) --error-log-path=:指向错误日志目录 --pid-path=:指向pid文件(nginx.pid) --lock-path=:指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。) --user:指定程序运行时的非特权用户 --group=:指定程序运行时的非特权用户组 --builddir=:指向编译目录 --with-rtsig_module:启用rtsig模块支持(实时信号) --with-select_module:启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:--without-select_module --with-poll_module:启用poll模块支持(功能与select相同,与select特性相同,为一种轮询模式,不推荐在高载环境下使用) --with-file-aio:启用file:aio支持(一种APL文件传输格式) --with-ipv6:启用ipv6支持 --with-http_ssl_module:启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl) --with-http_realip_module:启用ngx_http_realip_module支持(这个模块允许从请求标头更改客户端的IP地址值,默认为关) --with-http_addition_module:启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求) --with-http_xslt_module:启用ngx_http_xslt_module支持(过滤转换XML请求) --with-http_image_filter_module:启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG:图片的一个过滤器)(默认为不启用。gd库要用到) --with-http_geoip_module:启用ngx_http_geoip_module支持(该模块创建基于与MaxMind:GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量) --with-http_sub_module:启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本) --with-http_dav_module:启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启 --with-http_flv_module:启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件) --with-http_gzip_static_module:启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流) --with-http_random_index_module:启用ngx_http_random_index_module支持(从目录中随机挑选一个目录索引) --with-http_secure_link_module:启用ngx_http_secure_link_module支持(计算和检查要求所需的安全链接网址) --with-http_degradation_module::启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444码) --with-http_stub_status_module:启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态) --without-http_charset_module:禁用ngx_http_charset_module支持(重新编码web页面,但只能是一个方向--服务器端到客户端,并且只有一个字节的编码可以被重新编码) --without-http_gzip_module:禁用ngx_http_gzip_module支持(该模块同-with-http_gzip_static_module功能一样) --without-http_ssi_module:禁用ngx_http_ssi_module支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持SSI命令的列表是不完整的) --without-http_userid_module:禁用ngx_http_userid_module支持(该模块用来处理用来确定客户端后续请求的cookies) --without-http_access_module:禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于ip地址) --without-http_auth_basic_module禁用ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于http基本认证方法来保护你的站点或其部分内容) --without-http_autoindex_module:禁用disable:ngx_http_autoindex_module支持(该模块用于自动生成目录列表,只在ngx_http_index_module模块未找到索引文件时发出请求。) --without-http_geo_module:禁用ngx_http_geo_module支持(创建一些变量,其值依赖于客户端的IP地址) --without-http_map_module:禁用ngx_http_map_module支持(使用任意的键/值对设置配置变量) --without-http_split_clients_module:禁用ngx_http_split_clients_module支持(该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等) --without-http_referer_module:禁用disable:ngx_http_referer_module支持(该模块用来过滤请求,拒绝报头中Referer值不正确的请求) --without-http_rewrite_module:禁用ngx_http_rewrite_module支持(该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级别设置该选项,那么他们将在:location之前生效。如果在location还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为location部分的规则造成的,那么:location部分会再次被执行作为新的URI。:这个循环会执行10次,然后Nginx会返回一个500错误。) --without-http_proxy_module:禁用ngx_http_proxy_module支持(有关代理服务器) --without-http_fastcgi_module:禁用ngx_http_fastcgi_module支持(该模块允许Nginx:与FastCGI:进程交互,并通过传递参数来控制FastCGI:进程工作。:)FastCGI一个常驻型的公共网关接口。 --without-http_uwsgi_module:禁用ngx_http_uwsgi_module支持(该模块用来医用uwsgi协议,uWSGI服务器相关) --without-http_scgi_module:禁用ngx_http_scgi_module支持(该模块用来启用SCGI协议支持,SCGI协议是CGI协议的替代。它是一种应用程序与HTTP服务接口标准。它有些像FastCGI但他的设计:更容易实现。) --without-http_memcached_module:禁用ngx_http_memcached_module支持(该模块用来提供简单的缓存,以提高系统效率) --without-http_limit_zone_module:禁用ngx_http_limit_zone_module支持(该模块可以针对条件,进行会话的并发连接数控制) --without-http_limit_req_module:禁用ngx_http_limit_req_module支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件) --without-http_empty_gif_module:禁用ngx_http_empty_gif_module支持(该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用) --without-http_browser_module:禁用ngx_http_browser_module支持(该模块用来创建依赖于请求报头的值。如果浏览器为modern:,则$modern_browser等于modern_browser_value指令分配的值;如:果浏览器为old,则$ancient_browser等于:ancient_browser_value指令分配的值;如果浏览器为:MSIE中的任意版本,则:$msie等于1) --without-http_upstream_ip_hash_module:禁用ngx_http_upstream_ip_hash_module支持(该模块用于简单的负载均衡) --with-http_perl_module:启用ngx_http_perl_module支持(该模块使nginx可以直接使用perl或通过ssi调用perl) --with-perl_modules_path=:设定perl模块路径 --with-perl=:设定perl库文件路径 --http-log-path=:设定access:log路径 --http-client-body-temp-path=:设定http客户端请求临时文件路径 --http-proxy-temp-path=:设定http代理临时文件路径 --http-fastcgi-temp-path=:设定http:fastcgi临时文件路径 --http-uwsgi-temp-path=:设定http:uwsgi临时文件路径 --http-scgi-temp-path=:设定http:scgi临时文件路径 --without-http:禁用http:server功能 --without-http-cache:禁用http:cache功能 --with-mail:启用POP3/IMAP4/SMTP代理模块支持 --with-mail_ssl_module:启用ngx_mail_ssl_module支持 --without-mail_pop3_module:禁用pop3协议(POP3即邮局协议的第3个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。是因特网电子邮件的第一个离线协议标:准,POP3协议允许用户从服务器上把邮件存储到本地主机上,同时根据客户端的操作删除或保存在邮件服务器上的邮件。POP3协议是TCP/IP协议族中的一员,主要用于:支持使用客户端远程管理在服务器上的电子邮件) --without-mail_imap_module:禁用imap协议(一种邮件获取协议。它的主要作用是邮件客户端可以通过这种协议从邮件服务器上获取邮件的信息,下载邮件等。IMAP协议运行在TCP/IP协议之上,:使用的端口是143。它与POP3协议的主要区别是用户可以不用把所有的邮件全部下载,可以通过客户端直接对服务器上的邮件进行操作。) --without-mail_smtp_module:禁用smtp协议(SMTP即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP协议族,它帮助每台计算机在发送或中转信件时找到下一个目的地。) --with-google_perftools_module:启用ngx_google_perftools_module支持(调试用,剖析程序性能瓶颈) --with-cpp_test_module:启用ngx_cpp_test_module支持 --add-module=:启用外部模块支持 --with-cc=:指向C编译器路径 --with-cpp=:指向C预处理路径 --with-cc-opt=:设置C编译器参数(PCRE库,需要指定–with-cc-opt=”-I /usr/local/include”,如果使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc-opt=”-D FD_SETSIZE=2048”指定。) --with-ld-opt=:设置连接文件参数。(PCRE库,需要指定–with-ld-opt=”-L /usr/local/lib”。) --with-cpu-opt=:指定编译的CPU,可用的值为pentium、pentiumpro、pentium3、pentium4、athlon、opteron、amd64、sparc32、sparc64、ppc64 --without-pcre:禁用pcre库 --with-pcre:启用pcre库 --with-pcre=:指向pcre库文件目录 --with-pcre-opt=:在编译时为pcre库设置附加参数 --with-md5=:指向md5库文件目录(消息摘要算法第五版,用以提供消息的完整性保护) --with-md5-opt=:在编译时为md5库设置附加参数 --with-md5-asm:使用md5汇编源 --with-sha1=:指向sha1库目录(数字签名算法,主要用于数字签名) --with-sha1-opt=:在编译时为sha1库设置附加参数 --with-sha1-asm:使用sha1汇编源 --with-zlib=:指向zlib库目录 --with-zlib-opt=:在编译时为zlib设置附加参数 --with-zlib-asm=:为指定的CPU使用zlib汇编源进行优化,CPU类型为pentium,:pentiumpro --with-libatomic:为原子内存的更新操作的实现提供一个架构 --with-libatomic=:指向libatomic_ops安装目录 --with-openssl=:指向openssl安装目录 --with-openssl-opt:在编译时为openssl设置附加参数 --with-debug:启用debug日志
五、配置Nginx
1、设置Nginx开机自动启动
开机自动启动脚本如下,请根据实际情况修改:
#! /bin/bash # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: This Is Yeboyzq The Nginx Server. # # processname: nginx # config: /etc/nginx/nginx.conf # pidfile: /usr/local/nginx/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/usr/local/nginx/lock/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
把脚本另存为/etc/init.d/nginx,并执行以下步骤
chmod +x /etc/init.d/nginx //添加执行权限 chkconfig --add nginx //添加到开机启动项 mkdir -p /usr/local/nginx/tmp/{client-body,proxy,fastcgi,uwsgi,scgi} //新建临时目录及文件
2、修改配置文件
vim /etc/nginx/nginx.conf (请按字段查找修改) user nginx nginxs;
3、Nginx.conf配置文件详细说明(附备注)
#user nobody; //运行用户 worker_processes 1; //开启的线程数,一般跟逻辑CPU核数一致 #error_log logs/error.log; //定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式,debug输出最多,crir输出最少,根据实际环境而定 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; //指定进程id的存储文件位置 events { worker_connections 1024; //指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制 } http { include mime.types; //核心模块指令,智力默认设置为二进制流,也就是当文件类型未定义时使用这种方式 default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' //日志格式的设定,main为日志格式的名称,可自行设置,后面引用 # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; //引用日志main sendfile on; //开启高效文件传输模式 #tcp_nopush on; //开启防止网络阻塞 #keepalive_timeout 0; //设置客户端连接保存活动的超时时间 keepalive_timeout 65; #gzip on; //开启gzip压缩 server { //Nginx的server虚拟主机配置 listen 80; //监听端口为 80 server_name localhost; //设置主机域名 #charset koi8-r; //设置访问的语言编码 #access_log logs/host.access.log main; //设置虚拟主机访问日志的存放路径及日志的格式为main location / { //设置虚拟主机的基本信息 root html; //设置虚拟主机的网站根目录 index index.html index.htm; //设置虚拟主机默认访问的网页 } #error_page 404 /404.html; //设置指定404页面 # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; //定义错误提示页面 location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 //PHP脚本请求处理 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root //禁止访问 .htxxx 文件 # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
如需要采用多配置文件的方式则按以下方式配置:
首先在 nginx 目录下创建子目录 vhosts
mkdir /etc/nginx/vhosts/
然后在 vhosts 目录中创建对应域名的配置文件。如有域名test.com和test.cn,则分别创建文件test.com.conf和test.cn.conf。
把nginx目录中的nginx.conf配置文件中server的配置分别添加到vhosts目录中的各配置文件。如果使用到了负载均衡的,则要修改相应的upstream配置。
再修改nginx.conf,删除原来的serve 配置项,在 server 配置项位置添加配置文件包含项(http项内):
include /etc/nginx/vhosts/*.conf;
4、运行nginx
firewall-cmd --permanent --zone=public --add-service=http //防火墙永久开放HTTP规则 firewall-cmd --reload //防火墙重新加载配置文件 service nginx start //启动nginx
发表评论