Nginx(engine-x)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的。
Nginx主要着眼点是其高性能以及对物理计算机资源的高密度利用,nginx采用了模块化、事件驱动、异步、单线程及非阻塞的架构,并大量采用多路复用及事件通知机制。Nginx按需同时运行多个进程:一个主进程(master)和几个工作进程(worker)。
Nginx的主要配置:main, http, server, upstream和location等等。Nginx的代码是由一个核心和一系列的模块组成,核心主要是提供webServer的基本功能等等。
1、编译环境
操作系统: centos 6.5
编译工具:
基于源码的安装方式首先需要确保必要的编译工具已经安装好。
# yum grouplist "Development tools"查看开发编译工具是否安装完成
2、依赖关系
gzip模块需要zlib库
rewrite模块需要使用pcre库
ssl功能需要使用openssl库
使用yum 安装所有依赖开发包
# yum install -y pcre-devel zlib-devel openssl-devel
3、下载nginx源码
下载网址为:http://nginx.org/en/download.html
这里我下载版本为nginx-1.6.3.tar.gz
1、增加服务器用户和所在用户组
# groupadd -r nginx
# useradd -r -g nginx nginx
2、配置
# ./configure --help | less 查看配置项
说明几个配置项:
--prefix=PATH set installation prefix 软件安装目录
--conf-path=PATH set nginx.conf pathname 设置配置文件路径
--error-log-path=PATH set error log pathname 错误日志路径
--http-log-path=PATH set http access log pathname http请求访问日志路径
--pid-path=PATH set nginx.pid pathname 设置进程id路径
--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 工作者进程用户组
--with-http_ssl_module enable ngx_http_ssl_module 启用ssl模块 支持https协议
--with-http_flv_module enable ngx_http_flv_module 启用flv模块 为FlashVideo文件提供服务端伪流媒体支持
--with-http_stub_status_module enable ngx_http_stub_status_module 启用NginxStatus功能,监控Nginx当前状态
--with-http_gzip_static_module enable ngx_gzip_static_module 启用nginx静态压缩能力
--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
--with-pcre force PCRE library usage
配置如下
# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock \
--user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module \
--with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi
3、编译安装
# make
# make install
4、服务启动脚本
1)新建服务启动脚本/etc/rc.d/init.d/nginx
nginx SYS V启动脚本内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/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=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
for opt in $options; do
if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
esac2)增加执行权限
# chomd a+x /etc/rc.d/init.d/nginx
3)启动脚本添加到服务列表
# chkconfig --add nginx
# chkconfig nginx on // 设定开机自启动
1、启动服务
# service nginx start
2、浏览页面
完成测试!
按以上步骤操作可以完成nginx web服务器安装!
本文出自 “佩天门的技术感悟” 博客,请务必保留此出处http://7490142.blog.51cto.com/7480142/1683225
原文地址:http://7490142.blog.51cto.com/7480142/1683225