yum -y install java-1.7.0-openjdk
wget http://download.eclipse.org/jetty/stable-9/dist/jetty-distribution-9.2.5.v20141112.tar.gz
tar zxvf jetty-distribution-9.2.5.v20141112.tar.gz -C /opt/
mv /opt/jetty-distribution-9.2.5.v20141112/ /opt/jetty
cd /opt/jetty/bin
sh ./jetty.sh start
浏览器http://IP:8080/即可
ln -s /opt/jetty/bin/jetty.sh /etc/init.d/jetty
chkconfig --add jetty
chkconfig --level 345 jetty on
vi /etc/default/jetty
JETTY_HOME=/opt/jetty
JETTY_USER=jetty 重启失败可以更改root试试
JETTY_PORT=8080
JETTY_HOST=50.116.24.78
JETTY_LOGS=/opt/jetty/logs/
TMPDIR=/usr/local/jetty9.1/tmp(这个需要修改)
service jetty start
关于一台服务器部署多个jetty9实例如下(环境基于上面安装状态):
sh ./jetty.sh start
netstat -tunlp查看到两个java端口开启,一个是8888,一个是8443(这种是一个实例,默认状态)
cd /opt/jetty/
mkdir run
cp /var/run/jetty.pid /opt/jetty/run
cp -R /opt/jetty/ /opt/jetty1/
cd /opt/jetty/bin
vi jetty.sh(增加以下几行)
JETTY_HOME=/opt/jetty
JETTY_USER=root
JETTY_PORT=8888
JETTY_RUN=$JETTY_HOME/run
JETTY_LOGS=/opt/jetty/logs/
cd /opt/jetty
vi start.ini(查找关于8443的ssl和https的端口,发现8443端口都注释掉了)
#jetty.port=8080(注释掉,因为本人需要改端口)
cd start.d/
vi 900-demo.ini
etc/jetty-ssl.xml
#etc/jetty-https.xml(最后两个逐个注释测试,注释https就ok,再启动jetty只出现一个端口)
jar -xvf file.war(解压war包)
我的云服务器,先启动了8888和8881端口,然后启动8882端口时发现我的8881端口掉了,机器只能启动两个端口,突然想起jetty配置内存问题,使用top命令启了两个jetty我的内存用完了(默认配置)
报错1:(复制过来的更改端口之后不能启动,启动失败但是端口已开启的解决办法)
cd /opt
cp -R jetty/ jetty4/
cd jetty4/run/
rm jetty.pid
touch jetty.pid
报错2:(更改jetty默认的临时解压文件位置)
cd /jetty4
mkdir work
拓展1:与vsftpd结合使用
cd /etc/vsftpd/user
vi guowang-tm
local_root=/opt/jetty/work/jetty-0.0.0.0-8888-包名字.war-_包名字-any-
write_enable=YES
anon_umask=022
file_open_mode=0777
anon_world_readable_only=NO
anon_mkdir_write_enable=YES
anon_upload_enable=YES
anon_other_write_enable=YES
vi vsftpuser.txt
guowang-tm (账号)
guowang-tm-passwd (密码)
db_load -T -t hash -f /etc/vsftpd/user/vsftpuser.txt /etc/vsftpd/user/vsftpuser.db(不需要重启,直接生效)
cd /opt/jetty/work/jetty-0.0.0.0-8888-包名字.war-_包名字-any-
chown -R vsftp.vsftp jsp/ webapp/ ent/
cd /opt/jetty/work
chmod -R 755 jetty-0.0.0.0-8888-包名字.war-_包名字-any-/
chmod 777 jetty-0.0.0.0-8888-包名字.war-_包名字-any-/ (链接FPT测试成功)
拓展2:jetty多实例开发边测试边调试代码,需要jetty的开启关闭和看代码的权限
vi jetty-test.sh(注意修改/opt/jetty-3/bin/jetty.sh里的家目录与脚本一致)
#!/bin/bash
ME=`basename $0`
ArgCount=$#
CMD=$1
KEY=$2
NOWDAY=`date +%Y`_`date +%m`_`date +%d`
WHO=`whoami`
if [ ! $ArgCount -eq 2 ]; then
echo " Arg count must be 2"
echo " {stop | start | log | logn | logf} {key}"
echo " log is cat log file"
echo " logn is tail log file last 1000 line"
echo " logf is tail -f log file"
exit
fi
if [ $CMD != "start" -a $CMD != "stop" -a $CMD != "log" -a $CMD != "logf" -a $CMD != "logn" ]; then
echo "arg 1 must be start or stop or log or logf or logn"
exit
fi
#XMLFILE="/opt/jetty-$KEY/etc/jetty.xml"
#echo -n "XML file $XMLFILE"
#if [ -f "$XMLFILE" ]; then
# echo " is exist."
#else
# echo " is NOT exist!"
# exit
#fi
#JAVA_HOME=/data/app/java
#CLASSPATH=/data/app/java/lib/dt.jar:/data/app/java/lib/tools.jar
#export JAVA_HOME CLASSPATH
#export LANG=zh_CN.UTF-8
export JETTY_HOME=/opt/jetty-$KEY
export JETTY_RUN=/opt/jetty-$KEY/run
export JETTY_LOGS=/opt/jetty-$KEY/logs
#export JETTY_ARGS=/opt/jetty-$KEY/etc/jetty-logging.xml
export JAVA_OPTIONS='-XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled -XX:PermSize=64M -XX:MaxPermSize=128M -Xmx512m -DOPTIONS=server'
PIDCOUNT=`ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME | awk '{print $2}' | wc -l`
LOGFILE=`ls -t $JETTY_HOME/logs/*.stderrout.log | awk 'NR==1'`
case $CMD in
logf)
tail -f $LOGFILE
;;
logn)
tail -n1000 $LOGFILE
;;
log)
more $LOGFILE
;;
start)
if [ "$PIDCOUNT" -gt 1 -o "$PIDCOUNT" -eq 1 ]; then
echo "There are too many process :"
ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME
exit 1
fi
if [ "$PIDCOUNT" -eq 0 ]; then
echo "Let's start it......"
/opt/jetty-$KEY/bin/jetty.sh start
sleep 1
echo "It's stared"
exit 0
fi
;;
stop)
if [ "$PIDCOUNT" -gt 1 ]; then
echo "There are too many process :"
ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME
exit 1
fi
if [ "$PIDCOUNT" -eq 0 ]; then
echo "It had been down!"
exit 1
fi
if [ "$PIDCOUNT" -eq 1 ]; then
echo "Let's stop it......"
PID=`ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME | awk '{print $2}'`
kill $PID
echo "Sending STOP ... Waiting ..."
for i in {60..0}; do
PIDCOUNT=`ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME | awk '{print $2}' | wc -l`
if [ "$PIDCOUNT" -eq 1 ]; then
echo "It's running yet...$i"
sleep 1
else
echo "$JettyHome has been down"
exit
fi
done
PIDCOUNT=`ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME | awk '{print $2}' | wc -l`
if [ "$PIDCOUNT" -eq 1 ]; then
PID=`ps -ef | grep "jetty-$KEY" | grep -v 'grep' | grep -v $ME | awk '{print $2}'`
kill -9 $PID
echo "Force to kill -9 pid:$PID"
echo "sleep 5 second"
sleep 5
echo "$PID has been down"
exit
fi
fi
;;
esac
使用方法:(关联的vsftp里面的目录地址注意修改)
ln -s /opt/shell/jetty-test.sh /usr/bin/jj
开启:jj start 3
关闭:jj stop 3
日志:jj logn 3
照搬的脚本,有些不要的自己注释掉了http://blog.csdn.net/chris_playnow/article/details/7987584
拓展3:(解决jetty占用内存过大)
ps -ef|grep jetty
ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid
' | grep jetty | sort -nrk5
(其中rsz是实际内存,vsz是虚拟内存)
(上面脚本占用内存容易一行被我注释掉了,建议使用)
cd /opt/jetty-3
mv start.d/900-demo.ini start.d/900-demo.ini.backup(注释掉不用,需要功能再加,这是解决日志关于java:....../root/etc/webdefault.xml找不到的错误)
vi start.ini(把前面的注释去掉)(不建议使用)
--exec
-Dsun.rmi.dgc.client.gcInterval=3600000
-Dsun.rmi.dgc.server.gcInterval=3600000 设置客户端和服务端GC启动的时间
-Xmx2000m 最大缓存空间
-Xmn512m 新生代大小
-XX:PermSize=512m 内存永久区初始大小
-XX:MaxPermSize=1024m 最大永久区大小
-XX:+UseConcMarkSweepGC
-XX:+CMSClassUnloadingEnabled 允许CMS清楚持久代
# ps -o sz,rss,vsz 端口号
sz:进程映像所占用的物理页面数量,也就是以物理页面为单位表示的虚拟内存大小
rss:进程当前所占用的物理内存大小,单位为kB
vsz:进程的虚拟内存大小,单位为kB,它等于sz乘于物理页面大小(x86平台通常为4kB)
拓展4:jetty优化
http://www.360doc.com/content/15/0626/10/281812_480764429.shtml
http://os.51cto.com/art/201502/464740.htm
监控jetty运行状态脚本:
monitor.sh
#!/usr/bin/env bash
#当前用户根目录
host_dir=`echo ~`
proc_name="start.jar"
# 日志文件
file_name="/opt/cron.log"
pid=0
# 计算进程数
proc_num()
{
num=`ps -ef | grep $proc_name | grep -v grep | wc -l`
return $num
}
# 进程号
proc_id()
{
pid=`ps -ef | grep $proc_name | grep -v grep | awk '{print $2}'`
}
proc_num
number=$?
# 判断进程是否存在
if [ $number -eq 0 ]
then
/opt/jetty9/bin/jetty.sh restart # 重启进程的命令,请相应修改
proc_id # 获取新进程号
echo ${pid}, `date` >> $file_name # 将新进程号和重启时间记录
fi
最近最简安装如下:
yum install java-1.8.0
cp jetty-1.9.0 jetty
cd jetty/bin
vi jetty.sh
JETTY_HOME=/opt/jetty
JETTY_USER=root
JETTY_PORT=8888
JETTY_RUN=$JETTY_HOME/run
JETTY_LOGS=/opt/jetty/logs/
./jetty.sh start(报错)
mkdir run
cp /run/jetty.pid run/
cp /root/jetty.state .
./jetty.sh start(好了)
netstat -tunlp(查看java启动的任然是8080默认端口)
vi start.ini
# Module: http
--module=http
## Connector port to listen on
jetty.http.port=80(把这个8080端口改成80端口即可)
报错1:
[启动报错 bin]# ./jetty.sh start
Starting Jetty: Exception in thread "main" java.lang.UnsupportedClassVersionError: org/eclipse/jetty/start/Main : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
FAILED Fri Apr 15 19:28:03 CST 2016
解决1:
yum install java-1.8.0即可解决
转载于:https://blog.51cto.com/guowang327/1709673