这里是JDK的下载地址,我使用的是rpm包
这里是tomcat的下载地址
部署tomcat
1.使用rz传输JDK的rpm包到本地
2.rz传输tomcat的tar包到本地
1030 rpm -ivh jdk-10.0.1_linux-x64_bin.rpm (安装jdk)
1031 mkdir /usr/local/tomcat (自定义目录)
1032 tar -xf apache-tomcat-8.0.52.tar.gz -C /usr/local/tomcat/ (将包解压到自定义目录)
[root@localhost bin]# vim /etc/profile
export JAVA_HOME=/usr/java/jdk-10.0.1 (添加JDK环境变量,提供给tomcat使用,如果只在命令行运行,子程序会继承)
/usr/local/tomcat/apache-tomcat-8.0.52/bin
[root@localhost bin]# ls
bootstrap.jar commons-daemon.jar daemon.sh setclasspath.sh startup.sh tool-wrapper.sh
catalina.bat commons-daemon-native.tar.gz digest.bat shutdown.bat tomcat-juli.jar version.bat
catalina.sh configtest.bat digest.sh shutdown.sh tomcat-native.tar.gz version.sh
catalina-tasks.xml configtest.sh setclasspath.bat startup.bat tool-wrapper.bat
[root@localhost bin]# ./startup.sh (启动tomcat)
[root@localhost conf]# vim tomcat-users.xml
(自己设置用户,密码)
重启tomcat
*(现在启动是可以的,但是一看进程,都是root在工作?这样会带来很大的安全隐患,解决方法)
基于daemon的启动
[root@localhost bin]# vim daemon.sh
源码编译生成jsvc文件
[root@localhost bin]# tar -xf commons-daemon-native.tar.gz (bin目录下就有)
[root@localhost bin]# ls
bootstrap.jar commons-daemon-1.1.0-native-src configtest.sh setclasspath.bat startup.bat tool-wrapper.bat
catalina.bat commons-daemon.jar daemon.sh setclasspath.sh startup.sh tool-wrapper.sh
catalina.sh commons-daemon-native.tar.gz digest.bat shutdown.bat tomcat-juli.jar version.bat
catalina-tasks.xml configtest.bat digest.sh shutdown.sh tomcat-native.tar.gz version.sh
[root@localhost bin]# cd commons-daemon-1.1.0-native-src/
[root@localhost commons-daemon-1.1.0-native-src]# ls
LICENSE.txt NOTICE.txt README RELEASE-NOTES.txt unix windows
[root@localhost commons-daemon-1.1.0-native-src]# cd unix/
[root@localhost unix]# ls
configure configure.in INSTALL.txt Makedefs.in Makefile.in man native support
[root@localhost unix]# ./configure (编译)
Current host
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking cached host system type... ok
C-Language compilation tools
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for ranlib... ranlib
checking for strip... strip
Host support
checking C flags dependant on host system type... ok
Java compilation tools
checking for JDK os include directory... linux
gcc flags added
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking sys/capability.h usability... no
checking sys/capability.h presence... no
checking for sys/capability.h... no
configure: WARNING: cannot find headers for libcap
Writing output files
configure: creating ./config.status
config.status: creating Makefile
config.status: creating Makedefs
config.status: creating native/Makefile
All done
Now you can issue "make"
[root@localhost unix]# make
(cd native; make all)
make[1]: Entering directory /usr/local/tomcat/apache-tomcat-8.0.52/bin/commons-daemon-1.1.0-native-src/unix/native'
/usr/local/tomcat/apache-tomcat-8.0.52/bin/commons-daemon-1.1.0-native-src/unix/native'
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c jsvc-unix.c -o jsvc-unix.o
jsvc-unix.c: In function ‘run_controller’:
jsvc-unix.c:1304: warning: assignment from incompatible pointer type
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c arguments.c -o arguments.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c debug.c -o debug.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c dso-dlfcn.c -o dso-dlfcn.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c dso-dyld.c -o dso-dyld.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c help.c -o help.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c home.c -o home.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c java.c -o java.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c location.c -o location.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c replace.c -o replace.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c locks.c -o locks.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk-10.0.1/include -I/usr/java/jdk-10.0.1/include/linux -c signals.c -o signals.o
ar cr libservice.a arguments.o debug.o dso-dlfcn.o dso-dyld.o help.o home.o java.o location.o replace.o locks.o signals.o
ranlib libservice.a
gcc jsvc-unix.o libservice.a -ldl -lpthread -o ../jsvc
make[1]: Leaving directory
[root@localhost unix]# pwd (路径不对,移动文件)
/usr/local/tomcat/apache-tomcat-8.0.52/bin/commons-daemon-1.1.0-native-src/unix
[root@localhost unix]# cp jsvc /usr/local/tomcat/apache-tomcat-8.0.52/bin (cp到指定目录)
[root@localhost unix]# cd /usr/local/tomcat/apache-tomcat-8.0.52/bin
[root@localhost bin]# ls
bootstrap.jar commons-daemon.jar digest.bat shutdown.bat tomcat-native.tar.gz
catalina.bat commons-daemon-native.tar.gz digest.sh shutdown.sh tool-wrapper.bat
catalina.sh configtest.bat jsvc startup.bat tool-wrapper.sh
catalina-tasks.xml configtest.sh setclasspath.bat startup.sh version.bat
commons-daemon-1.1.0-native-src daemon.sh setclasspath.sh tomcat-juli.jar version.sh
[root@localhost bin]# ps -ef|grep tomcat
root 4610 2665 0 14:13 pts/0 00:00:00 grep tomcat
[root@localhost bin]# ./daemon.sh start (启动时需要用户)
Invalid user name 'tomcat' specified
[root@localhost bin]# groupadd -g 299 tomcat
[root@localhost bin]# useradd -u 299 -g 299 tomcat (忘记指定不让他登陆了)
[root@localhost bin]# useradd -u 299 -g 299 tomcat -s /sbin/nologin
useradd: user 'tomcat' already exists
[root@localhost bin]# userdel -r tomcat (删除重新添加)
[root@localhost bin]# !g
groupadd -g 299 tomcat
[root@localhost bin]# useradd -u 299 -g 299 tomcat -s /sbin/nologin
[root@localhost bin]# ./daemon.sh start
[root@localhost bin]# !ps
[root@localhost bin]# chown tomcat. -R /usr/local/tomcat/apache-tomcat-8.0.52/ (添加用户的权限)
[root@localhost bin]# ./daemon.sh stop (重启)
[root@localhost bin]# ps -ef|grep tomcat
root 4717 2665 0 14:30 pts/0 00:00:00 grep tomcat
[root@localhost bin]# ./daemon.sh start
测试
./daemon.sh stop(这种启动不舒服)
如何改成service或者systemctl 启动方式?
[root@localhost init.d]# cp /usr/local/tomcat/apache-tomcat-8.0.52/bin/daemon.sh tomcat (先将脚本文件放到/etc/init.d/)
[root@localhost init.d]# vim tomcat (添加以下的环境变量)
export CATALINA_BASE=/usr/local/tomcat/apache-tomcat-8.0.52
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.0.52
export JAVA_HOME=/usr/java/jdk-10.0.1
[root@localhost init.d]# service tomcat start (启动前,先关闭tomcat)
*(基于安全考虑,最好将tomcat放置其家目录下)
tomcat虚拟主机
[root@localhost conf]# vim server.xml
[root@localhost conf]# pwd
/usr/local/tomcat/apache-tomcat-8.0.52/conf
[root@localhost conf]# service tomcat stop
[root@localhost conf]# service tomcat start
[root@localhost apache-tomcat-8.0.52]# ls
abc.com aefg.com bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost apache-tomcat-8.0.52]# cd aefg.com/
[root@localhost aefg.com]# ls
ROOT
[root@localhost aefg.com]# tree .
.
└── ROOT
└── index.jsp
1 directory, 1 file
怎么让访问时不加8080呢?方法很多,nat,iptables,配置文件,但是这些方式都会降低性能
怎么改合适?----nginx
使用nginx的反向代理功能,能很好地解决此问题
server {
listen 80;
server_name www.abc.com;
location / {
proxy_pass http://192.168.1.3:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
tomcat + DB
这里说明几点即可
1.连接数据库要有 mysql-connector-java.
2.配置文件定义连接数据库(一种为全局,一种为局部。)
3.[root@localhost conf]# vim context.xml ^C(全局,会应用到所有虚拟主机数据目录)
[root@localhost conf]# pwd
/usr/local/tomcat/apache-tomcat-8.0.52/conf
4.WEB-INF(局部,一般存放在数据目录里面)
5.数据库授权
6.修改权限
7.测试
tomcat迁移和反向(参考上一章php的迁移,原理是一样的)
tomcat程序复制(和上一章php的复制一样,唯一一点是nginx做了轮询是,section值)
section会出现什么问题呢?
当你第一次输入用户密码时轮询到1机器,1机器数据库记录下你的section和状态
当你点击登陆时轮询到2机器,2机器发现你没有登陆,要求你重新登陆
但你再次输入用户密码时2机器记录下你的section和状态,登陆轮训到1机器,1机器发现你没登陆(恶性循环)
解决方法:(nginx-proxy+iphash)
tomcat多实例
应为当你维护一个实例时,关闭tomcat会影响所有的实例(这样并不好,解决方法)
[root@localhost tomcat1]# ls (这是1)
bin conf logs temp tomcat1.com webapps work
[root@localhost tomcat2]# ls(这是2,端口改成和1不同的就行,server.xml里改)
bin conf logs temp tomcat2.com webapps work
[root@localhost init.d]# vim tomcat1 (1启动1的实例)
[root@localhost init.d]# pwd
/etc/init.d
export CATALINA_BASE=/usr/local/tomcat/apache-tomcat-8.0.52/tomcat1
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.0.52
export JAVA_HOME=/usr/java/jdk-10.0.1
[root@localhost init.d]# vim tomcat2 (2启动2的实例)
[root@localhost init.d]# pwd
/etc/init.d
export CATALINA_BASE=/usr/local/tomcat/apache-tomcat-8.0.52/tomcat2
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.0.52
export JAVA_HOME=/usr/java/jdk-10.0.1