一、安装Tomcat、JDK
1、上传tar包到系统的/usr/local目录下
2、解压缩apache-tomcat-7.0.27.tar.gz,并更名为tomcat7(方便记)
#tar zxvf apache-tomcat-7.0.27.tar.gz
#mv apache-tomcat-7.0.27 tomcat7
3、解压缩JDK并安装(如果你安装系统时已经安装了java环境, 则3、4步骤可省略)
#gunzip jdk-6u11-linux-i586-rpm.bin.gz
#./jdk-6u11-linux-i586-rpm.bin
4、配置环境变量
编辑/etc/profile文件(注:养成编辑前备份的好习惯)
#cd /etc/
#cp profile profile.bak
追加如下内容:
JAVA_HOME="/usr/java/jdk1.6.0_11"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin "
CATALINA_HOME="/usr/local/tomcat7"
export JAVA_HOME CATALINA_HOME
5、启动Tomcat并检测安装是否成功
#cd /usr/local/tomcat7/bin/
#./startup.sh
浏览器中输入:http://ip:8080/,看看是否会出现一个猫。前提是你的firewall必须放开对8080的访问。
6、设置Tomcat的服务目录
#cd /usr/local/tomcat7/conf/
编辑server.xml文件:找到
<Host name="localhost"
appBase="webapps" unpackWARs="true" autoDeploy="true">
在其后追加:
<Context path="" docBase="/var/www/html" debug="0"
reloadable="true"/>
将“/var/www/html”换成你自己的放置网站的目录。
再次打开http://ip:8080/,看是否是你想要访问的页面。
二、安装Nginx
1、获取Nginx-1.3.0,并解压
#cd /usr/local/
#wget http://nginx.org/download/nginx-1.3.0.tar.gz
#tar zxvf nginx-1.3.0.tar.gz
2、编译安装
#cd nginx-1.3.0
#./configure --with-http_stub_status_module --with-http_ssl_module #启动server状态页和https模块
#make
可能出现的错误:
错误1、
[[email protected] nginx-1.3.0]# ./configure --with-http_stub_status_module --with-http_ssl_module
checking for OS
+ Linux 2.6.32-220.el6.i686 i686
checking for C compiler ... not found
./configure: error: C compiler gcc is not found
原因:缺少gcc包
解决方式:#yum -y install gcc
错误2、
libtool: compile: unrecognized option `-DHAVE_CONFIG_H'
libtool: compile: Try `libtool --help' for more information.
make[1]: *** [pcrecpp.lo] 错误 1
make[1]: Leaving directory `/usr/local/pcre-8.30'
make: *** [all] 错误 2
原因:缺少包
解决方式:#yum -y install gcc-c++
重新执行
#./configure --with-http_stub_status_module
--with-http_ssl_module #启动server状态页和https模块
#make
错误3、缺少pcre library,这个是http rewrite的模块。
解决方式:
#cd /usr/local/
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
#cd pcre-8.30
#./configure
#make
#make install
继续安装Nginx:
#./configure --with-http_stub_status_module --with-http_ssl_module #make
#make install
4、简要配置Nginx。
安装成功后的nginx在/usr/local/nginx.
在/usr/local/nginx/conf/下新建proxy.conf,用于配置一些代理参数,内容如下:
#!nginx (-)
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
编辑conf/nginx.conf文件,内容如下:
#运行nginx所在的用户
#user nobody;
#启动进程数
worker_processes 8;
#全局错误日志及PID文件
error_log /usr/local/nginx/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服务器,利用它的反向代理功能提供负载均衡支持
http
{
#设定mime类型
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx/conf/proxy.conf;
#charset gb2312;
#设定请求缓冲
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m; (在1.3中加入这个参数验证配置时报错,就去掉了)
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
# fastcgi_buffer_size 64k;
# fastcgi_buffers 4 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;
# 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;
###禁止通过ip访问站点
server{
server_name _;
return 404;
}
server
{
listen 80;
server_name www.xxx.com;
index index.html index.htm index.jsp;#设定访问的默认首页地址
root /var/www/html;#设定网站的资源存放路径
#limit_conn crawler 20;
location / {
root /var/www/html;
index index.html index.htm;
}
location ~ .(jsp|jspxdo)?$ {
index index.jsp;
proxy_pass http://localhost:8080;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
#定义访问日志的写入格式
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 /usr/local/nginx/logs/localhost.log access;#设定访问日志的存放路径
}
}
5、验证配置
#/usr/local/nginx/sbin/nginx -t
错误:
[[email protected] conf]# /usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:在/lib中创建一个symbol link到/usr/local/lib/libpcre.so.1
[[email protected] conf]# ln -s /usr/local/lib/libpcre.so.1
/lib
若出现如下信息,则表示配置正确,否则需按错误提示进行配置文件更正
nginx: the configuration file /usr/local/nginx/conf/nginx.conf
syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is
successful
6、启动Nginx服务
#/usr/local/nginx/sbin/nginx
出现告警信息:
nginx: [warn] the "log_format" directive may be used only on
"http" level in /usr/local/nginx/conf/nginx.conf:
这是新版本的问题,需要我们将log_format的定义放到server的前面。如果你是直接更改的nginx.conf文件,你会发现这一句原本就是在server的前面的。
查看nginx主进程:
#ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F
' ' '{print $2}'
7、停止Nginx服务
#/usr/local/nginx/sbin/nginx -s stop
三、联合测试
在/var/www/html/下新建两个文件index.html,index.jsp,内容如下:
[[email protected] bin]# cat /var/www/html/index.html
hello world
[[email protected] bin]# cat /var/www/html/index.jsp
<html>
<head>
<title>Nginx Test</title>
</head>
<body>
<%out.println("<h1>Hello World!</h1>");%>
</body>
</html>
用浏览器分别访问 http://ip/index.html
http://ip/index.jsp,
http://ip:8080/index.jsp
到此配置完成!