系统环境:
os:centos6.5x64
hostname:test1.lxm.com
ip:10.0.10.11
nginx:nginx-1.6.1.tar.gz
openssl:openssl-1.0.1i.tar.gz
software path:/root/soft/
software install path: /usr/localphp
1.安装系统所需的编译软件(若是安装过了,则跳过,本人新装系统,因此须要安装)
#yum -y install gcc* compact-gcc* automake make autoconflinux
2.安装nginx依赖软件
#yum -y install pcre pcre-develnginx
注:pcre软件包提供了nginx正则表达式的支持,若是不想使用系统自带的pcre软件包或者想要使用更高版本的pcre软件包,能够自行编译安装,本人采用人rpm包安装
编译安装pcre:
#tar -zxvf pcre-8.35.tar.gz
#cd pcre-8.35
#mkdir /usr/local/pcre (建立pcre安装的目录,本人习惯将编译安装的软件放置于/usr/local/目录下.不过这里要注意的是,若是自行下载pcre包,nginx使用的是源码解压的目录,而不是安装后的目录,这跟apache不同)
#./configure --prefix=/usr/local/pcre
#make && make installweb
3.安装nginx
#cd /root/soft
#tar -zxvf openssl-1.0.1i.tar.gz
#tar -zxvf nginx-1.6.1.tar.gz
#mkdir /usr/local/nginx
#mkdir -pv /usr/local/nginx/{client_body_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
#./configure --prefix=/usr/local/nginx/ --with-pcre --with-http_ssl_module --with-openssl=/root/soft/openssl-1.0.1i --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client_body_temp/ --http-proxy-temp-path=/usr/local/nginx/proxy_temp/ --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp/ --http-scgi-temp-path=/usr/local/nginx/scgi_temp/
#make && make install
#make clean正则表达式
注:此处的nginx编译时加入了ssl的功能,可是不须要事先编译安装openssl,由于这里只须要调用openssl的源码包.此外temp几个目录文件必须手工建立,不然安装完后默认是没有的,这也是高版本和低版本的区别apache
4.启动并测试nginx
建立nginx的启动用户和组:
#groupadd -g 48 nginx
#useradd -g 48 -u 48 -s /sbin/nologin nginx -M
#cd /usr/local/nginx/conf/
#vim nginx.conf
将#user nobody; 修改成user nginx;编程
#/usr/local/nginx/sbin/nginx -t : 测试配置文件是否有语法错误
#/usr/local/nginx/sbin/nginx :启动nginx服务
#ps aux | grep nginx :查看是否有nginx进程
#netstat -nultp | grep 80 :nginx默认监听80端口,查看是否正常vim
若是检查一切正常,则使用浏览器进程访问测试:
http://10.0.10.11
正常状况下会返回nginx的欢迎界面:centos
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.浏览器
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
注:这里的内容为复制,第一次写发现插入不了图片,有待研究,哈哈哈。。。。。
若是发现不能访问,测试可能有两个问题:
1)查看selinux是否关闭
#sestatus
若是selinux开启,则临时容许:
#setenforce 0
若是想永久关闭,则修改配置文件,以后在重启系统便可;
#sed -i '/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
若是不会使用sed命令,则使用vim打开/etc/sysconfig/selinux文件,将其中的SELINUX=enforcing改成SELINUX=disabled便可,以后重启系统生效。
2)查看iptables是否开启
系统默认是开启iptables来保护系统的安全,默认状况下只容许ssh的远程链接和已经创建链接以及相关联的进程进行交互访问,所以默认开启状况下是阻止80端口访问的。此时有两种经常使用的解决办法:
(1):清空规则,由于iptables链的默认规则是ACCEPT
#iptables -F
(2):添加一条容许访问80端口的规则
#iptables -t filter -I INPUT 5 -m state --state NEW -m tcp -p tcp -s 0/0 --dport 80 -j ACCEPT
注意:我这里使用的是-I(插入)选项,该选项默认是在匹配该number规则的前面插入一条规则。
附:
1.nginx 编译选项解释
nginx的编译选项虽然不是不少,可是也很多,所以这里不作一一解释,也没有必要,只对一些常见的选项进行解释
查看编译的选项:
#cd /root/soft/nginx-1.6.1 //即nginx的解压目录下
#./configure --help | less
注:在编译软件的时候,新手一般会问,到底这个软件默认开启了那些功能,哪些功能又是没开启的。这里提供你们一个简单的判断方法,当咱们查看编译选项的时候,--with开头的一般是默认没有开启的功能,若是要开启该功能,则须要编译的时候手动添加该选项开启该功能,若是是已--without开头的,则默认是开启该功能的,不作任何设置,编译安装好的软件就可使用该功能
--with-rtsig_module
--with-select_module
--without-select_module
--with-poll_module
--without-poll_module
以上几个模块都是web站点底层并发链接的信号处理机制,对nginx来讲,默认采用的epoll机制,可以轻松的应付c10K的问题
--with-file-aio
开启对内核文件异步io传输的支持,可以提供nginx的性能,可是在使用前,建议你们对于气底层的原理要熟悉
--with-ipv6
开启对ipv6的支持
--with-http_ssl_module
开启对ssl的支持,支持证书的功能
--with-http_spdy_module
开启对spdy的支持,spdy是google开发的一种tcp应用层协议,能够下降网络延迟,提高网络性能
--with-http_realip_module
开启该模块,能够记录真实的源ip地址
--with-http_p_w_picpath_filter_module
开启该模块可用用来对图片内容进行过来,可是该模块须要gd库的支持
--with-http_dav_module
开启该模块能够用来针对性的设置文件或者目录的权限
--with-http_flv_module
该选项用来开启nginx对flv视频流的支持
--with-http_mp4_module
开启该模块支持mp4视频流
--with-http_gunzip_module
开启该模块支持对gzip压缩文件的解压功能
--with-http_gzip_static_module enable ngx_http_gzip_static_module
开启该模块能够对文件进行检查是否已经被gzip压缩,由于使用nginx时,为了下降带宽的使用,加快网络传输速度,一般会采用gzip压缩必定格式的文件。该模块就能够防止已经被压缩的文件再次压缩
--with-http_auth_request_module
该模块用来设置nginx的基于子请求的认证功能
--with-http_random_index_module
该选项可让nginx支持随机选项一个索引页面返回给客户端
--with-http_secure_link_module
这个模块用于为所需的安全性“令牌”计算和检查请求URL
--with-http_degradation_module
开启该选项容许nginx在内层不足的状况下返回204或者444状态码
--with-http_stub_status_module
使用该选项,能够是nginx支持使用web界面查看nginx的实时链接状态,也就是一些状态信息
--without-http_gzip_module
该选项是用来设置gizp压缩功能的设置
--without-http_userid_module
该选项是用来获取客户端cookie功能的设置
--without-http_access_module
该选项是用来设置nginx对访问权限的设置
--without-http_auth_basic_module
该选项用来设置nginx是否支持给予用户和密码认证的访问权限控制
--without-http_autoindex_module
该选项用来设置nginx对自动因此的支持,也就是web节目访问时,能够返回一个文件列表
--without-http_referer_module
该选项用来设置nginx的防盗链功能
--without-http_rewrite_module
该选项用来设置nginx支持重定向转发规则
--without-http_proxy_module
该选项用来设置nginx支持proxy功能
--without-http_fastcgi_module
该选项用来设置nginx支持fastcgi,一般用来和php进行整合
--without-http_uwsgi_module
该选项用来设置nginx是否支持uwsgi的程序
--without-http_scgi_module
该选项用来设置nginx是否支持scgi的程序
--without-http_memcached_module
该选项用来设置nginx是否支持memcached
--without-http_limit_conn_module
该选项用来设置nginx是否支持并发链接数的限制,一般用来防止攻击
--without-http_limit_req_module
该选项用来设置nginx是否支持请求数的限制
--without-http_browser_module
该选项用来设置nginx是否支持识别客户段的浏览器功能,能够针对不一样的客户端浏览器来返回不一样的内容
--without-http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
--without-http_upstream_keepalive_module
以上几个选项是针对nginx做为反向代理时的设置
--with-http_perl_module
--with-perl_modules_path=PATH
--with-perl=PATH
以上几个选项用来开启对perl正则表达式的支持,一般咱们会使用pcre来开启对perl正则表达式的支持,在编译的时候使用--with-pcre
--http-log-path=PATH
指定nginx的日志存放路径
--http-client-body-temp-path=PATH
--http-proxy-temp-path=PATH
--http-fastcgi-temp-path=PATH
--http-uwsgi-temp-path=PATH
--http-scgi-temp-path=PATH
以上几个选项都是针对nginx运行中的一些临时文件的存放目录的设置,在nginx的新版本中,必须手动指定,不然编译安装后,不存在此类目录
--without-http
nginx默认也是一个web软件,支持大件web站点,若是要关闭此功能,则使用该选项
--without-http-cache
nginx默认支持http的缓存功能,若是要关闭缓存功能,则使用该选项
--with-mail
--with-mail_ssl_module
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
以上几个选项都是跟mail有关,nginx也支持main反向代理
--with-google_perftools_module
该选项用来开启支持google perftools工具,该工具能够完成对nginx实时性能的检测和调优
--add-module=PATH
该选项特别重要,nginx默认自带的模块,并不能完成须要的全部功能,此时若是要使用额外的模块来实现相应的功能,就须要使用该选项来添加额外的模块到nginx中
2.nginx LSB启动脚步
#vim /etc/rc.d/init.d/nginx
复制下面的内容到该文件中:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
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
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
保存该文件
#chmod a+x /etc/rc.d/init.d/nginx
#dos2unix /etc/rc.d/init.d/nginx :将该脚步的格式转换为linux下的格式,主要区别在于行结束符,每每一些复制的脚步看起来内容没有任何错误,但在linux下执行就是报错,一般都是格式没转换的缘由
#service nginx restart :重启一下nginx,若是重启成功,则表示该脚步能够正常使用
#chkconfig --add nginx
#chkconfig --level 3 nginx on
以上两行能够设置nginx开机时在3级别上自动启动
注:使用该脚步时,要确保脚步中可执行文件和配置文件的路径是正确的,不然会报错.其次,所谓LSB脚步是linux/unix的一种脚步编程规范,反正在/etc/rc.d/init.d/目录下的启动脚步,都必须支持LSB的格式
到此为止,nginx的安装部署就说到这里。更多nginx技术文档请看下回分解。。。。。。。
结束!!!