在CentOS 5.5中安装Nginx+jdk+mysql+tomcat是非常容易的。只需yum安装环境包和nginx、解压安装jdk和tomcat、配置profile文件、server.xml和nginx.conf文件即可。具体步骤如下
一、安装环境包
1、首先使用yum命令安装、升级所需的程序库。
yum -y install gcc gcc-c++
autoconf libjpeg libjpeg-devel libpng libpng-devel freetype
freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel
glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-server krb5-devel libidn libidn-devel openldap
openldap-devel nss_ldap openldap-clients openldap-servers
2、yum安装mysql
先检查mysql是否安装:
rpm -qa | grep mysql
如果没有安装齐全执行下列yum
yum install mysql-bench mysql-test mysql-devel mysql-server mysql
3、yum安装prce
先检查prce是否安装:
rpm -qa | grep prce
如果没有安装执行下列yum
Yum install prce
二、yum安装nginx
1、安装nginx的最新稳定版yum源
Nginx最新版yum源在EPEL RPM包中,wget下载安装即可
cd /root
wget http://download.Fedora.RedHat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm –ivh epel-release-5-4.noarch.rpm
2、安装nginx
Yum install nginx
3、启动测试nginx
Service nginx restart
Nginx启动后有两个进程,master为主进程,worker为工作进程
在启动完NGINX后,我们可以在浏览器中输入http://localhost查看welcome to nginx即算成功。
三.安装jdk
1、首先下载jdk,并安装jdk。
#cd /root
#wget
http://cds-esd.sun.com/ESD6/JSCDL/jdk/6u23-b05/jdk-6u23-linux-i586.bin?AuthParam=1292548492_5c40501ebcb0ead52c24198a34e7e6ee&TicketId=B%
2Fw3nB2ESl1MSxRGMlFYlQHl&GroupName=CDS&FilePath=/ESD6/JSCDL/jdk/6u23-b05/jdk-6u23-linux-i586.bin&File=jdk-6u23-linux-i586.bin
下载完后,修改jdk-6u23-linux-i586.bin的文件属性为可执行,www.linuxidc.com然后执行该程序安装
#chmod +x jdk-6u23-linux-i586.bin
#cd ./ jdk-6u23-linux-i586.bin
#mv jdk1.6.0_23 /usr/local/jdk
2、编辑profile文件 设置开机文件java开机执行环境文件
#vi /etc/profile
在文件的末尾增加如下内容
JAVA_HOME="/usr/local/jdk"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:%JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME
保存并退出vi,执行以下命令使配置生效,且重启电脑
#source /etc/profile
#reboot
四、安装apache tomcat
1、下载apache tomcat并安装tomcat
#cd /root
#mkdir /www //建立网页根目录
#cp /usr/share/nginx/html/* /www/
#wget http://apache.etoak.com/tomcat/tomcat-7/v7.0.5-beta/bin/apache-tomcat-7.0.5.tar.gz
#tar zxvf apache-tomcat-7.0.5.tar.gz
#mv apache-tomcat-7.0.5 /usr/local/tomcat
#cp –rf /usr/local/tomcat/webapps/* /www/
2、配置tomcat的server.xml文件,并启动或停止tomcat
#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
五、nginx与tomcat整合
Nginx与tomcat的整合其实就是只要配置好nginx.conf文件就可以了。
#vim /etc/nginx/nginx.conf //配置好的nginx.conf文件如下(注意红色部分)
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32K;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#tomcat add start<<
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;
#tomcat add end>>
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#tomcat add start<<
upstream tomcat_server {
server 127.0.0.1:8080;
}
#tomcat add end>>
server {
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /www;
index index.html index.htm index.jsp default.jsp index.do default.do;
}
#tomcat add start<<
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$http://$host/$1$2/ permanent;
}
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_passhttp://tomcat_server;
}
#tomcat add end>>
error_page 404 /404.html;
location = /404.html {
root /www;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#ocation ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
}
六、测试
启动nginx
#service nginx restart
Nginx启动后,可以访问以下URL中的jsp实例程序,检查jsp程序能否运行。
http://localhost/examples/jsp/
注意:nginx与tomcat的工作原理是由nginx代理tomcat输出网页,因此如果开启了防火墙,防火墙不用打开8080端口,也一样可以访问jsp页面。