先搭建好LNMP环境
安装jdk和tomcat
下载并安装JDK 1.8
Orcale不允许外链下载JDK,可以使用以下命令下载
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
tar zxf jdk-8u60-linux-x64.tar.gz && mkdir /usr/local/java && mv jdk1.8.0_60 /usr/local/java
vim /etc/profile 在文件的最后面加上:
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 :wq 保存退出 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)
安装tomcat
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.14/bin/apache-tomcat-8.5.14.tar.gz && mkdir -p /usr/local/tomcat/ && tar zxf apache-tomcat-8.5.14.tar.gz && mv apache-tomcat-8.5.14/* /usr/local/tomcat/
添加tomcat service工具
#!/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_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" cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh fi return 0 } stop() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Stoping Tomcat" cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh 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" cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/version.sh 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
修改nginx 把jsp环境反向代理给后端的tomcat
vim /usr/local/nginx/conf/vhost/test.51fuli.info.conf upstream mytomcats { server 127.0.0.1:8080; } server { listen 80; server_name test.51fuli.info; index index.html index.htm index.jsp index.do index.php; root /home/jsp/www; access_log /home/wwwlogs/jspaccess.log; location / { proxy_pass http://mytomcats; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 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; } }
修改tomcat添加一个站点
修改/usr/local/tomcat/conf/server.xml 找到 类似 <Host name=····· </Host> 再后面添加 <Host name="test.51fuli.info" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <!-- 绑定其他域名 --> <!-- <Alias>test.51fuli.info</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 "%r" %s %b" /> <Context path="" docBase="/home/jsp/www" debug="0" reloadable="true" /> </Host>
在/home/jsp/www新建测试jsp页面 vim index.jps <html> <body> <center>Now time is: <%=new java.util.Date()%></center> </body> </html>
重启nginx并启动tomcat 测试访问
测试成功