nginx从其诞生不久就迅速占领市场很大份额,而且这种趋势还在增长,这说话nginx肯定有很多优秀的表现,本文就算是笔者自己一个总结文章,供以后自己参阅,也希望各位感兴趣的网友一起添加新的应用。
编译安装nginx很简单,我就从nginx编译开始
一:安装前准备
nginx编译需要在yum环境和编译环境准备好进行,yum源可以是本地源(把镜像一挂,路径一指就能用,很适合不能上网的情况)和网络源(在这我推荐使用163的源)
编译最基本环境yum groupinstall "Development Libraries" "Development Tools"
同时nginx编译依赖pcre-devel开发包所有野需要装一下
yum install pcre-devel -y
下载源码包
wget http://www.nginx.org/download/nginx-1.0.14.tar.gz
(ps:如果想往xshell里直接拉东西的话需要安装这个包lrzsz,注意不是想虚拟机中拖拉东西哟)
二,编译安装
像apache一样,nginx也需要一个指定的系统用户来使用,这个系统用户如果是编译安装的时候可以自己添加指定。像我下面用的就是nginx用户和组(你完全可以改成其他你定义的系统用户)
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx
(ps,如果不添加nginx这个系统用户的话会在/usr/sbin/nginx启动的时候报错:nginx: [emerg] getpwnam("nginx") failed)
# tar xf nginx-1.0.14.tar.gz
# cd nginx-1.0.14
#./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/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/ \
--with-pcre
#make && make install
关于配置选项的简单说明: 大部分选项对于大家来说都是一看就知道什么意思,像--with-http_gzip_static_module 这样有特殊作用的我在分析配置文件的时候给出具体解释。
刚刚编译前忘记了一件很重要的事情,就是伪装nginx的版本号,
伪装版本号需要在nginx没有被编译前修改nginx文件下的nginx-0.5.35/src/core/nginx.h这个头文件如:
/*
* Copyright (C) Igor Sysoev
*/
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define NGINX_VERSION "1.0"
#define NGINX_VER "apache/" NGINX_VERSION
#define NGINX_VAR "apache"
#define NGX_OLDPID_EXT ".oldbin"
#endif /* _NGINX_H_INCLUDED_ */
(如果不在编译前修改,编译后就无法修改,最多只能隐藏nginx的版本号)
make install后一个最简答的没有经过优化的nginxweb服务器已经安装成功,可以启动服务验证下,nginx服务启动可以使用自己带的启动(ps:一定不要启动apache,不然会争用80端口)
# /usr/sbin/nginx
# netstat -tnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27366/nginx
从上面可以看出nginx已经正常启动,打开网页http://192.168.1.103/出现了welcome to nginx!
但是这样对于习惯sv风格的管理服务的朋友来说,感觉会很不爽,还好我们有脚本,自己写个sv风格bash脚本:
# vim /etc/init.d/nginx
#!/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/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
esac
#chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# service nginx start
验证能够正常启动关闭nginx服务,如果你编译的路径不一样的话,按照你编译的参数相对应的修改。
