【CentOS】 Nginx+jdk+tomcat 环境搭建

CentOS  Nginx+jdk+tomcat 环境搭建  

一、jdk安装

jdk下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html

从上面网页中选择适合的jdk下载到/root/目录下。我选择的是jdk-6u20-linux-i586.bin。

下载完后,修改jdk-6u23-linux-i586.bin的文件属性为可执行,然后执行该程序安装JDK:

chmod +x jdk-6u20-linux-i586.bin
./ jdk-6u20-linux-i586.bin

按空格键看完协议,当出现提示“Do you agree to the above license terms?[yes or no] ” 时,输入“yes”。安装完成后,执行以下语句:

mv jdk1.6.0_23 /usr/local/jdk

编辑profile文件 设置开机文件java开机执行环境文件:

vi /etc/profile

在文件的末尾增加如下内容:

JAVA_HOME="/usr/local/jdk"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH="$JAVA_HOME/bin:.:$PATH"
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME

保存并退出vi,执行以下命令使配置生效:

source /etc/profile

二、tomcat安装

将 apache-tomcat-6.0.35 放到/root/目录中,拷贝到/user/local/目录下,并重命名为tomcat:

mv apache-tomcat-6.0.23 /usr/local/tomcat

建立网页根目录:

mkdir /www 
cp -rf /usr/local/tomcat/webapps/* /www/

配置tomcat的server.xml文件:

vim /usr/local/tomcat/conf/server.xml

查找appBase=”webapps”,修改为appBase=”/www”,其中/www 即为网页的根目录。

修改完成后,启动tomcat,默认监听端口为8080:

/usr/local/tomcat/bin/startup.sh

停止tomcat可以使用以下命令:

/usr/local/tomcat/bin/shutdown.sh

注:在执行./startup.sh,或者./shutdown.sh的时候,爆出了Permission denied,

其实很简单,就是在执行tomcat的时候,用户没有权限,而导致无法执行,

用命令chmod 修改一下bin目录下的.sh权限就可以了:

chmod u+x *.sh

再次执行 /usr/local/tomcat/bin/startup.sh (或/usr/local/tomcat/bin/shutdown.sh)就可以了。

 三、nginx与tomcat整合 

Nginx与tomcat的整合其实就是只要配置好nginx.conf文件就可以了。

查看并修改nginx.conf文件:

vim /usr/local/nginx/conf/nginx.conf 

修改后的nginx.conf文件,红色部分为添加的关于tomcat的代码:

user  www www;

worker_processes 1;

error_log /home/wwwlogs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;


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 256k;

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;

upstream tomcat_server {
server 127.0.0.1:8080;
}


#limit_zone crawler $binary_remote_addr 10m;

server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.jsp index.do default.jsp default.do index.php;
root /home/wwwroot;

if (-d $request_filename)
{
rewrite ^/(.*)([^/])$http://$host/$1$2/ permanent;
}

location ~ \.(jsp|jspx|do|wsdl)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_server;
}



location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}

location /status {
stub_status on;
access_log off;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

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 /home/wwwlogs/access.log access;
}
include vhost/*.conf;
}

在配置文件中,静态HTML网页、图片、js、css、Flash等使用Nginx来处理,以便得到更快的速度,文件扩展名为.JSP、.do、wsdl的请求,由Nginx反向代理Tomcat HTTP服务器来处理。

修改/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

重新启动nginx:

/usr/local/nginx/sbin/nginx -s reload

nginx启动后,可以访问以下URL中的jsp实例程序,检查jsp程序能否运行。
http://localhost/examples/jsp/
注意:nginx与tomcat的工作原理是由nginx代理tomcat输出网页,因此如果开启了防火墙,防火墙不用打开8080端口,也一样可以访问jsp页面。

 

 

 

 

本文内容参考如下网址:

http://www.cnblogs.com/huangjingzhou/articles/2153405.html

http://blog.163.com/leijie131421@126/blog/static/424111452011228103243106/

http://blog.slogra.com/?post=28

http://hi.baidu.com/wptad/blog/item/d3d33b019b0741147bec2cea.html

posted @ 2012-02-24 18:09  一筐  阅读(4906)  评论(0编辑  收藏  举报