上一篇文章我们已经谈过centos下的php环境lamp的安装,现在我们就来大家jsp的允许环境Nginx+jdk+mysql+tomcat,在centos上有了yum,安装都变得异常的easy。

一、首先使用yum命令安装或者升级所需的程序库

copy以下命令到命令行回车即可。

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

二、安装mysql

这个上一篇文章已经安装过了,请参考:

三、安装Nginx

根据centos版本安装版本可能有所不同,详情请看http://nginx.org/en/download.html

1、Nginx最新版yum源在EPEL RPM包中,wget下载安装即可cd /root[root@AY12122501352213a7156 /]# wget http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm[root@AY12122501352213a7156 /]# rpm -ivh epel-release-5-4.noarch.rpm

2、安装nginx
[root@AY12122501352213a7156 /]# yum install nginx

中途一直输入y即可完成。

3、启动测试nginx

[root@AY12122501352213a7156 /]# service nginx restart
Stopping nginx:                                            [FAILED]
Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: still could not bind()[root@AY12122501352213a7156 /]# ps -ef | grep httpd
apache   25797     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
apache   25798     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
apache   25799     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
apache   25800     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
apache   25801     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
apache   25802     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
apache   25803     1  0 Jan11 ?        00:00:00 /usr/sbin/httpd
root     31068 30852  0 09:57 pts/0    00:00:00 grep httpd[root@AY12122501352213a7156 /]# killall httpd
Stopping nginx:                                            [FAILED]
Starting nginx:                                            [  OK  ]

80端口被占用,我这里是apache服务占用的,将其结束即可。Nginx启动后有两个进程,master为主进程,worker为工作进程.

更多关于80端口问题,可以参考我前面的文章

更多关于结束进程问题,请参考我前面的文章

在启动完NGINX后,我们可以在浏览器中输入http://serverip查看welcome to nginx即算成功。

 

 

四、安装jdk

1、首先下载jdk

到官网找到你要下载的jdk地址,然后使用wget下载到本地。

[root@AY12122501352213a7156 /]# wget --no-cookie --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" http://download.oracle.com/otn-pub/java/jdk/7u10-b18/jdk-7u10-linux-i586.rpm
wget下载Jdk有个小问题,就是wget支持重定向,直接:wget  http://download.oracle.com/otn-pub/java/jdk/7u10-b18/jdk-7u10-linux-i586.rpm 这样会出问题,具体参考我前面的文章

2、安装jdk

[root@AY12122501352213a7156 softdir]# rpm -ivh jdk-7u10-linux-i586.rpm

3、配置环境变量

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

[root@AY12122501352213a7156]#vi /etc/profile
在文件的末尾增加如下内容#JDK  START==================
JAVA_HOME="/usr/java/jdk1.7.0_10" #jdk安装路径,可以使用where is java查看
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib" 
PATH=".:$PATH:%JAVA_HOME/bin" 
CATALINA_HOME="/usr/local/tomcat" 
export JAVA_HOME CATALINA_HOME
#JDK END============
保存并退出vi,执行以下命令使配置生效。
#source /etc/profile

关于vi编辑器的使用,请参考
4、测试jdk

[root@AY12122501352213a7156]# java -version
java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) Client VM (build 23.6-b04, mixed mode, sharing)

五、安装tomcat

#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
#vi /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.s

h

3、测试

在流量其中输入http://serverip:8080/ 显示tomcat的主界面说明成功。

六、nginx与tomcat整合

1、Nginx与tomcat的整合其实就是只要配置好nginx.conf文件就可以了。最好先备份一个。

#vi /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_pass http://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;
}

 2、测试

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

至此,jsp环境搭建大功告成。