LNMT

2019-09-27  本文已影响0人  UncleZ_strive

1.JVM基本介绍

2.Tomcat与Nginx区别

tomcat
    基于java开发一个web服务器软件, 和nginx类似.
    tomcat能直接解析jsp代码,以及静态资源.
    nginx + tomcat 

3.Tomcat安装配置启动

[root@web01 ~]# yum install java -y
二进制安装tomcat
[root@web01 ~]# mkdir /soft && cd /soft
[root@web01 soft]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.26/bin/apache-tomcat-9.0.26.tar.gz
[root@web01 soft]# tar xf apache-tomcat-9.0.26.tar.gz 
[root@web01 soft]# ln -s /soft/apache-tomcat-9.0.26 /soft/tomcat
[root@web01 soft]# /soft/tomcat/bin/startup.sh
[root@web01 soft]# netstat -lntp|grep java
tcp6       0      0 :::8009            :::*        LISTEN      8500/java           
tcp6       0      0 :::8080            :::*        LISTEN      8500/java           
tcp6       0      0 127.0.0.1:8005     :::*        LISTEN      8500/java    

无法启动:
1.替换配置文件: pkill java 然后在启动
2.配置文件写错
3.虚拟主机是添加,不要修改
4.context如果写了,一定要有对应的目录,不然整体就报错
/soft/tomcat/logs/catalina.out

4.Tomcat配置文件详解

https://www.cnblogs.com/yinzhengjie/p/9508426.html

image.png

5.Tomcat部署博客项目zrlog

域名: zrlog.hai.com:8080
站点目录: /code/zrlog

[root@web01 ~]# vim /soft/tomcat/conf/server.xml
     <!--zrlog站点-->
    <Host name="zrlog.hai.com"  appBase="/code/zrlog"
          unpackWARs="true" autoDeploy="true">

      <Va0lve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
             prefix="zrlog_access_log" suffix=".txt"
             pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
[root@web01 ~]# mkdir /code/zrlog
[root@web01 ~]# cd /code/zrlog/
[root@web01 zrlog]# rz ROOT.war  
[root@web01 zrlog]# /soft/tomcat/bin/shutdown.sh && /soft/tomcat/bin/startup.sh && tail -f /soft/tomcat/logs/catalina.out
[root@db01 ~]# mysql -uroot -poldxu.com
MariaDB [(none)]> create database zrlog charset utf8;
    <---此前配置过all用户,可以复用
MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'oldboy.com';

6.源码包-->jar包--war包的关系

源码包 --> 由开发人员编写的
maven

7.如何开启 Server Status Host Manager页面

1.配置conf/tomcat-users.xml

<role rolename="manager-gui"/>
<user username="tomcat" password="123456" roles="manager-gui"/>

2.如果访问还是403,是因为tomcat默认仅运行本地访问该管理页面,需要允许同网段主机访问

[root@web01 ~]# ll /soft/tomcat/webapps/manager/
[root@web01 ~]# ll /soft/tomcat/webapps/host-manager/

[root@es-node1 tomcat]# vim 项目目录下/META-INF/context.xml
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
#修改为
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10\.0\.0\.\d+" />
[root@es-node1 tomcat]# vim /code/zrlog/ROOT/WEB-INF/web.xml
<web-app>
...
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>zrlog</web-resource-name>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
    
        <auth-constraint>
            <role-name>zrlog_role</role-name>
        </auth-constraint>
    </security-constraint>
    
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Default</realm-name>
    </login-config>
...
</web-app>
[root@es-node1 tomcat]# vim /soft/tomcat/conf/tomcat-users.xml
<role rolename="zrlog_role"/>
<user username="tomcat" password="123456" roles="zrlog_role"/>
[root@es-node1 ~]# /soft/tomcat/bin/shutdown.sh && /soft/tomcat/bin/startup.sh

8.部署多节点Tomcat-->mysql

10.0.0.7 ---> 10.0.0.51
10.0.0.8 ---> 10.0.0.51
1.安装NFS

[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# yum install nfs-utils -y
[root@nfs ~]# cat /etc/exports
/data/zrlog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs ~]# mkdir /data/zrlog
[root@nfs ~]# systemctl restart nfs

2.找到图片资源 推送图片资源至NFS

[root@web01 ~]# scp -rp /code/zrlog/ROOT/attached/* root@172.16.1.31:/data/zrlog/
[root@nfs ~]# chown -R www.www /data/zrlog/         #重新授权

3.多节点挂载
mount -t nfs 172.16.1.31:/data/zrlog/ /code/zrlog/ROOT/attached/

9.接入负载均衡

[root@lb01 conf.d]# cat proxy_zrlog.oldhai.com.conf 
upstream  zrlog {
    server 172.16.1.7:8080;
    server 172.16.1.8:8080;
}

server {
    listen 80;
    server_name zrlog.oldxu.com;

    location / {
        proxy_pass http://zrlog;
        include proxy_params;
    }
}

10.session测试代码用例,配置redis会话保持

[root@web01 conf]# vim /soft/tomcat/conf/server.xml 
<!--session站点-->
  <Host name="session.oldxu.com"  appBase="/code/session"
        unpackWARs="true" autoDeploy="true">
  </Host>
[root@web01 conf]# cat /code/session/ROOT/index.jsp 
<body>
<%
//HttpSession session = request.getSession(true);
System.out.println(session.getCreationTime());
out.println("<br> web01 SESSION ID:" + session.getId() + "<br>");
out.println("Session created time is :" + session.getCreationTime()
+ "<br>");
%>
</body>
[root@tomcat ~]# wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/3.0.3/tomcat-cluster-redis-session-manager.zip
[root@tomcat ~]# unzip tomcat-cluster-redis-session-manager.zip
[root@web01 ~]# cd tomcat-cluster-redis-session-manager

拷贝jar包

[root@web01 tomcat-cluster-redis-session-manager]# cp lib/* /soft/tomcat/lib/

拷贝tomcat连接redis配置文件

[root@web01 tomcat-cluster-redis-session-manager]# cp conf/redis-data-cache.properties /soft/tomcat/conf/

修改redis-data-cache.properties

[root@web01 ~]# vim /soft/tomcat/conf/redis-data-cache.properties
...
redis.hosts=172.16.1.51:6379
redis.password=123456           #有密码就写密码,没有不要写
...
[root@web01 ~]# vim /soft/tomcat/conf/context.xml
<Context>
    .....
    <Valve className="tomcat.request.session.redis.SessionHandlerValve" />
    <Manager className="tomcat.request.session.redis.SessionManager" />
    ....
</Context>
    <session-config>
            <session-timeout>60</session-timeout>       #根据情况调整
    </session-config>
[root@redis ~]# yum install redis -y
[root@redis ~]# cat /etc/redis.conf
...
bind 172.16.1.51 127.0.0.1
requirepass 123456              #如果不需要密码,则不要配置
...
[root@redis ~]# systemctl start redis
[root@redis ~]# systemctl enable redis

Nginx+Tomcat集群全站Https+SSL

[root@lb ~]# cat /etc/nginx/conf.d/proxy_zrlog.com.conf 
upstream zrlog {
    server 10.0.0.7:8080;
    server 10.0.0.8:8080;
}
server {
    listen 443 ssl;
    ssl_certificate ssl_key/server.crt;
    ssl_certificate_key ssl_key/server.key;
    server_name zrlog.oldxu.com;

    location / {
        proxy_pass http://zrlog;
        include proxy_params;
}
}
server {
    listen 80;
    server_name zrlog.oldxu.com;
    return 302 https://$http_host$request_uri;
}

上一篇 下一篇

猜你喜欢

热点阅读