共计 9508 个字符,预计需要花费 24 分钟才能阅读完成。
前言:从今天开始,我们将开始了解、学习、深入 Nginx,如果不知道 Nginx 是什么东西的童鞋,自行查找资料。其实我们每天接触的互联网中,每两个网站中几乎就有它的支持,截止到写本文,另一个市场占有率,也是排第一的 WEB 服务器是 Apache,为什么它排第一位?这是有历史原因的。为什么我们不学习市场占用率第一的 WEB 服务器?这也是有历史原因的,正是因为这样,它背负了太多的历史,导致它现在越来越臃肿,当然不可否认它的伟大性,任何一个东西存在即是有它的原因的。讲一个小的笑话,从前有一个软件,从它出生起就开始打 Patch,啊,又一个 Patch,a,又一个 Patch,然后我们就把它命名为 Apache。好了今天我们要学习的 Nginx,它是一个快速的,高扩展性,高可靠性,低消耗,自由的 WEB 服务器。在后面的学习中,我们将慢慢认识并热恋它。
1、环境准备
系统:CentOS 6.6 当前企业使用最多的 Linux 发行版本之一
Linux 内核:2.6+ 我们使用要系统已经满足条件了,为什么使用 2.6+ 内核版本?因为 epoll 异步编程模型只有在 Linux 2.6+ 内核上才支持,在 2.6 以前的内核中,普遍使用的是 select 或 poll 模型,它们无法解决高并发压力的问题。
2、必备软件
1) gcc 编译器
这个就不用解释了吧。
2) pcre 库
Perl Compatible Regular Expressions,意为 Perl 兼容的正则表达式,这个在我们后面配置的 nginx.con 配置文件中将会用到正则表达式,所以得用到这个库
3) zlib 库
对 HTTP 包的内容做 gzip 格式的压缩,这个在 nginx.conf 中有一个 gzip on 的选项,开启此选项后,可以降低文件或数据传输时的大小,节省带宽,但会带来额外的 CPU 开销,这个可根据服务器配置做适当调整,一般现代服务器 CPU 都很强大了,所以该选项也一般是开启的。
4) openssl 库
常见的 https 网站离不开它的功能,可以说整个互联网的安全都离不开它,建议安装 nginx 时,配置上该选项。
那么在 CentOS 中如何安装以上列出的这些库呢?很简单,只需要在联网的情况下,执行以下命令即可:
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
CentOS 中比较有意思的是,当写上一个包名,后面接上 -devel 意思就是说这是一个开发才需要的库。
3、获取 Nginx 源码
截止到写作本文,Nginx 的版本已经是 1.9.9,根据官网:http://www.nginx.org 的介绍,其中增加了好多新功能,我当前使用的时候是 1.0.8 的版本,新增了一些比如 tcp 代理、线程池的支持,这些东西我们后面再说。在实际产品开发过程中,一般会选取 stable release 版本,即稳定版本,这里我们选择 1.8.0 稳定版本,源码地址:
wget http://nginx.org/download/nginx-1.8.0.tar.gz
4、编译安装
我们把源码下载到 opt 目录下,解压,可以使用以下命令进行配置选项的查看:
./configure --help
以下是其输出:
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname--user=USER set non-privileged user for
worker processes
--group=GROUP set non-privileged group for
worker processes--build=NAME set build name
--builddir=DIR set build directory--with-select_module enable select module
--without-select_module disable select module
--with-poll_module enable poll module
--without-poll_module disable poll module--with-threads enable thread pool support
--with-file-aio enable file AIO support
--with-ipv6 enable IPv6 support--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_slice_module enable ngx_http_slice_module
--with-http_stub_status_module enable ngx_http_stub_status_module--without-http_charset_module disable ngx_http_charset_module
--without-http_gzip_module disable ngx_http_gzip_module
--without-http_ssi_module disable ngx_http_ssi_module
--without-http_userid_module disable ngx_http_userid_module
--without-http_access_module disable ngx_http_access_module
--without-http_auth_basic_module disable ngx_http_auth_basic_module
--without-http_autoindex_module disable ngx_http_autoindex_module
--without-http_geo_module disable ngx_http_geo_module
--without-http_map_module disable ngx_http_map_module
--without-http_split_clients_module disable ngx_http_split_clients_module
--without-http_referer_module disable ngx_http_referer_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--without-http_proxy_module disable ngx_http_proxy_module
--without-http_fastcgi_module disable ngx_http_fastcgi_module
--without-http_uwsgi_module disable ngx_http_uwsgi_module
--without-http_scgi_module disable ngx_http_scgi_module
--without-http_memcached_module disable ngx_http_memcached_module
--without-http_limit_conn_module disable ngx_http_limit_conn_module
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
--without-http_upstream_hash_module
disable ngx_http_upstream_hash_module
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
disable ngx_http_upstream_least_conn_module
--without-http_upstream_keepalive_module
disable ngx_http_upstream_keepalive_module
--without-http_upstream_zone_module
disable ngx_http_upstream_zone_module--with-http_perl_module enable ngx_http_perl_module
--with-perl_modules_path=PATH set Perl modules path
--with-perl=PATH set perl binary pathname--http-log-path=PATH set http access log pathname
--http-client-body-temp-path=PATH set path to store
http client request body temporary files
--http-proxy-temp-path=PATH set path to store
http proxy temporary files
--http-fastcgi-temp-path=PATH set path to store
http fastcgi temporary files
--http-uwsgi-temp-path=PATH set path to store
http uwsgi temporary files
--http-scgi-temp-path=PATH set path to store
http scgi temporary files--without-http disable HTTP server
--without-http-cache disable HTTP cache--with-mail enable POP3/IMAP4/SMTP proxy module
--with-mail_ssl_module enable ngx_mail_ssl_module
--without-mail_pop3_module disable ngx_mail_pop3_module
--without-mail_imap_module disable ngx_mail_imap_module
--without-mail_smtp_module disable ngx_mail_smtp_module--with-stream enable TCP proxy module
--with-stream_ssl_module enable ngx_stream_ssl_module
--without-stream_limit_conn_module disable ngx_stream_limit_conn_module
--without-stream_access_module disable ngx_stream_access_module
--without-stream_upstream_hash_module
disable ngx_stream_upstream_hash_module
--without-stream_upstream_least_conn_module
disable ngx_stream_upstream_least_conn_module
--without-stream_upstream_zone_module
disable ngx_stream_upstream_zone_module--with-google_perftools_module enable ngx_google_perftools_module
--with-cpp_test_module enable ngx_cpp_test_module--add-module=PATH enable an external module
--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid values:
pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, sparc32, sparc64, ppc64--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation support--with-md5=DIR set path to md5 library sources
--with-md5-opt=OPTIONS set additional build options for md5
--with-md5-asm use md5 assembler sources--with-sha1=DIR set path to sha1 library sources
--with-sha1-opt=OPTIONS set additional build options for sha1
--with-sha1-asm use sha1 assembler sources--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sources--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL--with-debug enable debug logging
在配置之前,我们先增加一个拥有 WEB 目录权限的用户 www,组 www(默认会添加一个组):
useradd -s /sbin/nologin www
开始配置,在这里我们为了避免选项太多产生混乱,我们只加入一个 --with-pcre 选项:
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/usr/local/nginx/error.log \
--pid-path=/usr/local/nginx/nginx.pid \
--user=www \
--group=www \
--with-pcre
编译并安装:
make
make install
此时,nginx 二进制文件已经安装到 /usr/local/nginx/sbin 中,我们进去看看:
cd /usr/local/nginx/sbin
./nginx -h
nginx version: nginx/1.9.9
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: /usr/local/nginx/nginx.conf)
-g directives : set global directives out of configuration file
直接运行./nginx 是没有问题的,默认的配置文件就是 /usr/local/nginx/nginx.conf,启动脚本我们后面再慢慢来写。
5、测试效果
如果没有出现以上效果,可能是你的防火墙没有放开 80 端口,加入以下命令即可:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
