当前位置: > Linux服务器 > 环境配置 >

LNMP(linux+nginx+php+mysql)环境安装tomcat以实现php和jsp双解析

时间:2016-05-26 14:15来源:linux.it.net.cn 作者:IT

安装lnmp环境

我这里使用的是LNMP一键安装包:http://lnmp.org/

 
1
wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar zxf lnmp1.2-full.tar.gz && cd lnmp1.2-full && ./install.sh lnmp

 

安装JDK和Tomcat

1.下载并安装JDK 1.8

Orcale不允许外链下载JDK,可以使用以下命令下载

 
1
2
curl -L -O -H "Cookie: oraclelicense=accept-securebackup-cookie" -k "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz"
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz

 

 
1
tar zxf jdk-8u60-linux-x64.tar.gz && mkdir /usr/local/java && mv jdk1.8.0_60 /usr/local/java

 

 
1
vi /etc/profile

在文件的最后面加上:

 
1
2
3
4
5
JAVA_HOME=/usr/local/java/jdk1.8.0_60
JRE_HOME=/usr/local/java/jdk1.8.0_60/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH

 

 
1
2
3
4
5
# source /etc/profile
# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

2.安装Tomcat

 
1
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.0.26/bin/apache-tomcat-8.0.26.tar.gz && tar zxf apache-tomcat-8.0.26.tar.gz && mv apache-tomcat-8.0.26 /usr/local/tomcat

添加tomcat service,内容来自https://gist.github.com/lesstif/f96b2ea2b975b8b16d0e&&https://www.liferay.com/web/brett.swaim/blog/-/blogs/sample-tomcat-startup-scripts:

 
1
vi /etc/init.d/tomcat

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#
# tomcat     This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO
 
TOMCAT_USER=root
TOMCAT_HOME="/usr/local/tomcat"
SHUTDOWN_WAIT=45
 
tomcat_pid() {
    echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
 
start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Tomcat is already running (pid: $pid)"
    else
        # Start tomcat
        echo "Starting tomcat"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh" $TOMCAT_USER
    fi
    return 0
}
 
stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Stoping Tomcat"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh" $TOMCAT_USER
 
    let kwait=$SHUTDOWN_WAIT
    count=0
    count_by=5
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
        echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
        sleep $count_by
        let count=$count+$count_by;
    done
 
    if [ $count -gt $kwait ]; then
        echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
        kill -9 $pid
    fi
    else
        echo "Tomcat is not running"
    fi
    return 0
}
 
status() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Tomcat is running with pid: $pid"
    else
        echo "Tomcat is not running"
    fi
}
 
version() {
    if [ -f $TOMCAT_HOME/bin/version.sh ];
    then
        echo "Display Tomcat Version"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/version.sh" $TOMCAT_USER
    fi
}
 
case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    version)
        version
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status|version}"
        exit 1
        ;;
esac
 
exit 0

将tomcat添加到自启动:

 
1
2
chmod +x /etc/init.d/tomcat
chkconfig tomcat on

3.修改nginx配置文件,在http{}中添加内容

 
1
vi /usr/local/nginx/conf/nginx.conf

 

 
1
2
3
4
5
6
7
8
9
10
11
12
......
        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;
 
        ####### 添加以下内容 #######
        upstream tomcat_server {
              server 127.0.0.1:8080;
              # 这里可以配置多个server形成负载均衡
        }
......

4.添加一个虚拟主机

 
1
lnmp vhost add

5.新建一个nginx配置文件,将请求交给Tomcat处理

 
1
vi /usr/local/nginx/conf/proxy-tomcat.conf

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#### location ~ \.(jsp|jsp|do|action)?$ ####
#### 使用下面的配置是将请求全部交给tomcat,上一行配置了交给tomcat处理的后缀
location / {
    proxy_redirect off;
 
    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://tomcat_server;
 
    client_max_body_size 100m; #客户端允许上传的最大文件大小
    client_body_buffer_size 128k; #允许客户端请求的最大单文件字节数
 
    proxy_connect_timeout 5; #nginx跟后端服务器连接超时时间
    proxy_read_timeout 60; #连接成功后,后端服务器响应时间
    proxy_send_timeout 5; #后端服务器发送时间
 
    proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
    proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
    proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
    #proxy_ignore_client_abort on; #不允许代理端主动关闭连接
}

6.修改虚拟主机配置文件,包含刚才那个配置文件即可

 
1
vi /usr/local/nginx/conf/vhost/blog.it.net.cn.conf

 

 
1
2
3
4
5
6
server_name blog.it.net.cn;
index index.html index.htm index.jsp index.do index.php;
root  /home/wwwroot/blog.it.net.cn;
 
include proxy-tomcat.conf; # 包含配置文件
#error_page   404   /404.html;

7.修改Tomcat配置文件

 
1
vi /usr/local/tomcat/conf/server.xml

添加一个网站:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Host name="blog.it.net.cn" appBase="webapps"
     unpackWARs="true" autoDeploy="true"
     xmlValidation="false" xmlNamespaceAware="false">
   <!-- 绑定其他域名 -->
   <!-- <Alias>www.it.net.cn</Alias> -->
   <!-- 获取真实IP -->
   <Valve className="org.apache.catalina.valves.RemoteIpValve"
           remoteIpHeader="X-Forwarded-For"
           requestAttributesEnabled="true"
           internalProxies="127.0.0.1" />
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="aqjlb_access_log" suffix=".txt"
           pattern="%{X-Forwarded-For}i %l %u %t &quot;%r&quot; %s %b" />
    <Context path="" docBase="/home/wwwroot/blog.it.net.cn" debug="0" reloadable="true" />
</Host>

8.启动服务器

 
1
2
# service tomcat start
# lnmp start

9.访问测试一下是否访问正常





(责任编辑:IT)
------分隔线----------------------------