说明:我本机的nginx安装的路径为:/usr/local/nginx
所需软件:nginx-1.2.3.tar.gz(负载均衡/反向代理服务器) pcre-8.31.tar.gz(正规表达式库) nginx-upstream-jvm-route-0.1.tar.gz(一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能) (以上软件包版本号以最新为准)
主要问题:每台机器上的环境都不太一样,最主要是已安装的包的情况不一样导致,如果你在按步骤做时有提示缺少哪个包或库时,请自行下载或到安装软件中的server文件夹下找相应的包,用 rpm -ivh --force xxx.rpm 安装,有提示依赖包时,一个一个按提示安装即可,有时依赖还挺多的,但总会完的…………。
步骤:
1:(有网络的情况)在线下线软件包:
1、nginx-1.1.2.tar.gz,负载均衡/反向代理服务器,可通过http://nginx.org/en/download.html获取。
2、pcre-8.10.tar.gz,正规表达式库,可通过http://sourceforge.net/projects/pcre/获取;
3、nginx-upstream-jvm-route-0.1.tar.gz,是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能,可通过http://code.google.com/p/nginx-upstream-jvm-route/downloads/list获取;
2:安装:我把上面的三个包放在/usr/local/下新建的文件夹nginx
进入/usr/local/nginx,用 tar zxvf xx.tar.gz 解以上的三个压缩包
安装nginx之前需要pcre依赖和jvm-remote补丁 ,后面的检查时带的参数路径(./configure ....)
终端:
[root@localhost ~]#cd nginx-1.1.2
[root@localhost nginx-1.2.3]#patch -p0 < /usr/local/nginx/nginx_upstream_jvm_route/jvm_route.patch (patch -p0 < ${nginx-upstream-jvm-route解压目录}/jvm_route.patch)
[root@localhost nginx-1.2.3]#./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/nginx/pcre-8.31 --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/nginx (./configure --prefix=/usr/local/nginx --with-pcre=${pcre解压目录} --with-http_stub_status_module --with-http_ssl_module --add-module=${nginx-upstream-jvm-route解压目录})
当这步执行时,出现的问题最多,因为它要检查系统的所需要的东西,我本机报:
the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl
错误提示要需要OpenSSL,接下来你就要把它装好,不过它依赖一些包,这里就不叙述了
接下来,make(本地编译nginx):
[root@localhost nginx-1.2.3]# make
不幸的是,报错了:
make -f objs/Makefile
make[1]: Entering directory `/usr/local/nginx/nginx-1.2.3'
make[1]: Warning: File `src/core/nginx.h' has modification time 1.4e+07 s in the future
cd /usr/local/nginx/pcre-8.31 \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
./configure --disable-shared
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... configure: error: newly created file is older than distributed files!
Check your system clock
make[1]: *** [/usr/local/nginx/pcre-8.31/Makefile] 错误 1
make[1]: Leaving directory `/usr/local/nginx/nginx-1.2.3'
make: *** [build] 错误 2
----------------------------------------------------------------------
从错误提示中,得知是pcre-xxx有问题,但我用 rpm -qa |grep pcre时显示的是pcre .6.6 ,一看系统已经有了,而不是我要装的pcre-8.31,虽然有了pcre,但却连接不到,我还是重新手动编译我下载的pcre-8.31吧;
[root@localhost nginx-1.2.3]# cd /usr/local/nginx/pcre-8.31/
[root@localhost pcre-8.31]# ./configure
报错:
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... configure: error: newly created file is older than distributed files!
Check your system clock
解决办法:
[root@localhost pcre-8.31]# find|xargs touch
[root@localhost pcre-8.31]# make
[root@localhost pcre-8.31]# make install
到此,再进入到/usr/local/nginx/nginx-1.2.3
[root@localhost nginx-1.2.3]# ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/nginx/pcre-8.30 --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/nginx
[root@localhost nginx-1.2.3]# make
[root@localhost nginx-1.2.3]# make install
自此,nginx就安装好了,至于linux下tomcat请参照别处自行装好。接下来,就是做web集群,负载均衡等功能的配置
关键的是在nginx安装目录下conf/nginx.conf配置,这里假设有一台192.168.77.105作为nginx的服务器(之所以用了一台,因为该机器上还要装分布式内存对象缓存系统memcached),一台192.168.77.200(作为web服务器,装了4台tomcat服务器,端口分别是8090,8091,8092,8093),105机器上放了一台tomcat,端口8090
具体nginx.conf的参考内容为:(颜色块标注的为最为重要,至于其他特殊需求可以参考nginx官方文档里各个参数设置)
worker_processes 8;
error_log logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
upstream backend {
server 192.168.77.200:8090 ;//这边就是nginx随机分配服务器的ip地址(tomcat服务器URL).也可加上srun_id来区分,比如srun_id=a,那么要修改192.168.77.200机器上端口对应的tomcat的conf下的server.xml里
找到<Engine ...改为 <Engine name="Catalina" defaultHost="localhost" jvmRoute="a"> server 192.168.77.200:8093 ;
server 192.168.77.200:8092 ;
server 192.168.77.200:8091 backup;//backup 表示作为备用服务器
#jvm_route $cookie_JSESSIONID|sessionid reverse;
ip_hash;
}
include mime.types;
default_type application/octet-stream;
#charset gb2312;
charset UTF-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4096k;
large_client_header_buffers 32 4096k;
client_max_body_size 20m;
limit_rate 4096k;
sendfile on;
tcp_nopush on;
keepalive_timeout 288000;
tcp_nodelay on;
fastcgi_connect_timeout 288000;
fastcgi_send_timeout 288000;
fastcgi_read_timeout 288000;
fastcgi_buffer_size 4096k;
fastcgi_buffers 32 4096k;
fastcgi_busy_buffers_size 8192k;
fastcgi_temp_file_write_size 8192k;
gzip on;
#gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
# access_log off;
server
{
listen 89;//nginx的监听端口,客户端在浏览器地址栏访问该服务器的该端口后即用nginx代理处理
server_name 192.168.77.105;//客户端在浏览器地址栏访问该服务器ip,也可以用域名如www.xxx.com,即用nginx代理处理
index index.html index.htm index.jsp;
root /usr/local/swsport-clustor;//本地系统存放路径的上一级,比如我的项目文件xxx放在/usr/local/swsport-clustor下
#location ~ .*\.jsp$
location /xxx/;//对应上面root的下一级项目文件,两者组合一个完整的项目路径
{
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_connect_timeout 3600;#延长为400秒,默认60秒
proxy_send_timeout 3600;#延长为300秒,默认60秒
proxy_read_timeout 3600;#延长为400秒,默认60秒
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
location /Nginxstatus {
stub_status on;
access_log off;
}
}
}
修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
#/usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
如果提示unknown host,则可在服务器上执行:ping www.baidu.com如果也是同样提示unknown host则有两种可能:
a、服务器没有设置DNS服务器地址,查看/etc/resolv.conf下是否设置,若无则加上
b、防火墙拦截
///////////error while loading shared libraries: libpcre.so.1 运行第五步时发生这样的错,让哥搞了接近一天
解决方案 //////////
:[root@bogon nginx-1.0.12]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007fff7e9db000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fe4629d0000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fe462799000)
libpcre.so.1 => not found//确实没找到
libz.so.1 => /lib64/libz.so.1 (0x00007fe462582000)
libc.so.6 => /lib64/libc.so.6 (0x00007fe4621e1000)
/lib64/ld-linux-x86-64.so.2 (0x00007fe462bfa000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fe461f7e000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fe461d7a000)
[root@bogon nginx-1.0.12]# cd /lib64/
[root@bogon lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
[root@bogon lib64]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007fff4d7ff000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb06f13e000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fb06ef07000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fb06ecda000)
libz.so.1 => /lib64/libz.so.1 (0x00007fb06eac4000)
libc.so.6 => /lib64/libc.so.6 (0x00007fb06e723000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb06f368000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fb06e4c0000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fb06e2bc000)
6、启动nginx的命令
#/usr/local/nginx/sbin/nginx
这时,输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
7、停止nginx的命令
#/usr/local/nginx/sbin/nginx -s stop
8、在不停止Nginx服务的情况下平滑变更Nginx配置
a、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
b、这时,输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/nginx/nginx.pid`
开机自启动nginx
这里使用的是编写shell脚本的方式来处理
vi /etc/init.d/nginx (输入下面的代码)
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
:wq 保存并退出
设置文件的访问权限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…
同样的修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
设置192.168.77.105上端口8090的tomcat作为linux服务,并设置自启动:
1、将/usr/local/swsport-clustor/tomcat90/bin/catalina.sh复制到/etc/rc.d/init.d/tomcat90
# cp /usr/local/swsport-clustor/tomcat90/bin/catalina.sh /etc/rc.d/init.d/tomcat90
2、修改/etc/rc.d/init.d/tomcat90
# vi /etc/rc.d/init.d/tomcat90
修改内容为在第二行加入以下内容
# chkconfig:2345 63 37
# description:tomcat server init script
在#CATALINA_HOME 这条注释下面加入
CATALINA_HOME=/usr/local/swsport-clustor/tomcat90
3、将tomcat加入Service 服务中
# chkconfig --add tomcat90
4、查看tomcat的service 列表
# chkconfig --list tomcat90
5、启动tomcat 并测试tomcat是否启动
# service tomcat90 start
# ps -e | grep java
如下显示,则表明Tomcat启动
5925 pts/0 00:00:03 java