码迷,mamicode.com
首页 > 系统相关 > 详细

LINUX——Nginx反代至双Tomcat(LNMT、LAMT),负载均衡中动静分离的使用

时间:2018-09-06 11:01:37      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:tle   mod   exec   webapps   evel   编译   temp   environ   pst   

环境布置:
配置安装163的源

[root@localhost ~]# cd /etc/yum.repos.d/
//创建备份目录
[root@localhost yum.repos.d]# mkdir /etc/repo-bf
//将原yum仓库的文件备份到repo-bf
[root@localhost yum.repos.d]# mv * /etc/repo-bf
//下载163的源到yum仓库
[root@localhost yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
//将改为版本的7
[root@localhost yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/163.repo
[root@localhost yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo
//自动配置yum的软件仓库,也可以自己配置
[root@localhost yum.repos.d]# yum -y install epel-release
[root@localhost yum.repos.d]# yum clean all
//安装编译环境
[root@localhost yum.repos.d]# yum -y install gcc gcc-c++
[root@localhost yum.repos.d]# yum -y install wget

tomcat项目部署
java环境安装

[root@localhost yum.repos.d]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

//查看安装的版本

[root@localhost yum.repos.d]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

tomcat部署
下载tomcat,在官网下载
这里是下载核心的

[root@localhost yum.repos.d]# cd /usr/src/
[root@localhost src]# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.11/bin/apach
mcat-9.0.11.tar.gz

//解压部署,只需要解压无需安装


//解压部署,只需要解压无需安装

[root@localhost src]# ls
apache-tomcat-9.0.11.tar.gz debug  kernels
解压到/usr/local/
[root@localhost src]# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/

将解压后的tomcat包的软件接,命名tomcat
[root@localhost src]# cd /usr/local
[root@localhost local]# ln -s apache-tomcat-9.0.11/ tomcat
[root@localhost local]# ls
apache-tomcat-9.0.11  bin  etc  games  include  lib  lib64  libexec  sbin  share  src  tomcat
[root@localhost local]# ll           //软连接
lrwxrwxrwx. 1 root root  20 9月   5 12:03 tomcat -> apache-tomcat-9.0.8/      
因为是java程序,一般服务器内存至少8-16g ,并且tomcat在一台服务器上不可能只跑一个               
进入tomcat包配置,以及介绍

[root@localhost local]# cd tomcat/
[root@localhost tomcat]# ls
bin     //二进制程序位置
startup.sh-启动的脚本
startdown.sh-停止的脚本
catalina.sh -控制启动和停止
configtest.sh -检查配置文件有无错误
----
conf       //配置文件
server.xml  //运行了两个则需要修改端口号
-----
lib   
-----
logs    //日志目录

README.md    

RUNNING.txt  
-----
webapps    //程序与网页都放这java的,在发版时,就是把开发的包放在这里,解压出来
开发写的war包里哪里需要配置需要问开发,例:classes/config.properties配置程序参数
BUILDING.txt  
CONTRIBUTING.md  
LICENSE  
NOTICE  
RELEASE-NOTES 
temp         
work

//在webapps里创建一个目录并且在目录里编辑网页,写一个 hello worid 的java界面

[root@localhost local]# cd webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test
[root@localhost test]# vim index.jsp
[root@localhost test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hellow World");
%>
</body>
</html>

启动

//检查
[root@localhost webapps]# /usr/local/tomcat/bin/configtest.sh
//启动
[root@localhost webapps]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost webapps]# ss -anlt
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN     0      100                    :::8080                               :::*                              
LISTEN     0      1        ::ffff:127.0.0.1:8005                               :::*                  
LISTEN     0      100                    :::8009                               :::*       

技术分享图片
技术分享图片
现在再来一个汤姆猫
技术分享图片

再次回到tomcat包的目录
[root@localhost local]# cd /usr/src/
解压并将解压后的文件移动到/usr/local/,重命名为apache-tomcat-9.0.11-tom2
[root@localhost src]# tar xf apache-tomcat-9.0.11.tar.gz
[root@localhost src]# mv apache-tomcat-9.0.11 /usr/local/apache-tomcat-9.0.11-tom2

将解压后的apache-tomcat-9.0.11-tom2包创建软件接,命名tomcat2
[root@localhost src]# cd /usr/local
[root@localhost local]# ln -s apache-tomcat-9.0.11-tom2/ tomcat2
[root@localhost local]# ls
apache-tomcat-9.0.11       bin  games    lib    libexec  share  tomcat
apache-tomcat-9.0.11-tom2  etc  include  lib64  sbin     src    tomcat2
[root@localhost local]# ll           //软连接

进入webapps,并配置网页,在启动tomcat2

先修改端口配置
[root@localhost local]# cd tomcat2/conf
[root@localhost conf]# ls
catalina.policy      context.xml           jaspic-providers.xsd  server.xml        tomcat-users.xsd
catalina.properties  jaspic-providers.xml  logging.properties    tomcat-users.xml  web.xml
[root@localhost conf]# vim server.xml        //在原有的端口稍作改变即可
<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
<Connector port="8019" protocol="AJP/1.3" redirectPort="8453" />

[root@localhost conf]# cd /usr/local/tomcat2/webapps/
[root@localhost webapps]# ls
docs  examples  host-manager  manager  ROOT

[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test
[root@localhost test]# vim index.jsp
[root@localhost test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("123456");
%>
</body>
</html>

启动

//检查
[root@localhost webapps]# /usr/local/tomcat2/bin/configtest.sh
//启动
[root@localhost webapps]# /usr/local/tomcat2/bin/catalina.sh start
[root@localhost webapps]# ss -anlt
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN     0      128                     *:111                                 *:*                  
LISTEN     0      5           192.168.122.1:53                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      128             127.0.0.1:631                                 *:*                  
LISTEN     0      100             127.0.0.1:25                                  *:*                  
LISTEN     0      1        ::ffff:127.0.0.1:8015                               :::*                  
LISTEN     0      128                    :::111                                :::*                  
LISTEN     0      100                    :::8080                               :::*                  
LISTEN     0      100                    :::8081                               :::*                  
LISTEN     0      100                    :::8019                               :::*                  
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      128                   ::1:631                                :::*                  
LISTEN     0      100                   ::1:25                                 :::*                  
LISTEN     0      1        ::ffff:127.0.0.1:8005                               :::*                  
LISTEN     0      100                    :::8009                               :::*             
[root@localhost webapps]# cd

不同端口
技术分享图片
技术分享图片
第二个tomcat同ip不同端口的网页也搭建成功
技术分享图片


下面开始搭建nging和数据库
可以在其他服务器搭建也可以就在本服务器搭建,


使用nginx,实现负载均衡
可在本服务器设置,也可以在其他服务器设置反向代理tomcat网页的ip和端口
安装依赖包

//创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx 

//安装编译环境
[root@localhost ~]# yum -y groups list
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel

//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx 
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx 
//下载nginx,编译安装
[root@localhost ~]# cd /usr/src/
[root@localhost src]# 
[root@localhost src]# yum -y install wget
[root@localhost src]# wget http://64.123.28.133/files/21490000000827F6/nginx.org/download/nginx-1.14.0.tar.gz
[root@localhost src]# tar xf nginx-1.14.0.tar.gz 
[root@localhost src]# cd nginx-1.14.0
[root@localhost nginx-1.14.0]# yum -y install gcc gcc-c++[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module \--with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.14.0]# make -j 4 && make install

//设置环境变量

[root@localhost nginx-1.14.0]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh

[root@localhost nginx-1.14.0]# . /etc/profile.d/nginx.sh

//启动
[root@localhost nginx-1.14.0]# nginx

nginx的配置文件
主配置文件 /usr/local/nginx/conf/nginx.conf
2.安装mysql

//安装依赖包
[root@guohui ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[root@guohui ~]# groupadd -r -g 306 mysql
[root@guohui ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

下载二进制格式的mysql软件包

[root@localhost ~]# cd /usr/src/
[root@guohui src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  nginx-1.14.0  nginx-1.14.0.tar.gz

[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# ls  /usr/local/
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.22-linux-glibc2.12-x86_64  nginx  sbin  share  src

[root@localhost src]# cd  /usr/local/
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"

//修改属主
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月  24 16:13 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

//设置环境变量
[root@localhost local]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@localhost local]# . /etc/profile.d/mysql.sh
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立数据存放目录
[root@localhost local]# cd /usr/local/mysql
[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/

//初始化数据库,最后生成的是一个临时密码
[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-05T09:17:21.026072Z 1 [Note] A temporary password is generated for root@localhost: ?3/(1tqF=gwu

配置mysql

[root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@localhost mysql]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# ldconfig -v

//生成配置文件
[root@guohui ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

配置启动脚本

root@localhost mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld
[root@localhost mysql]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld

启动mysql

[root@localhost mysql]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/localhost.localdomain.err‘.
SUCCESS!
[root@localhost mysql]# ps -ef|grep mysql
root      24348      1  0 16:25 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     24526  24348  2 16:25 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      24556   2316  0 16:25 pts/0    00:00:00 grep --color=auto mysql
[root@localhost mysql]# ss -antl
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN     0      128                     *:111                                 *:*                  
LISTEN     0      128                     *:80                                  *:*                  
LISTEN     0      5           192.168.122.1:53                                  *:*                  
LISTEN     0      128                     *:22                                  *:*                  
LISTEN     0      128             127.0.0.1:631                                 *:*                  
LISTEN     0      100             127.0.0.1:25                                  *:*                  
LISTEN     0      1        ::ffff:127.0.0.1:8015                               :::*                  
LISTEN     0      128                    :::111                                :::*                  
LISTEN     0      100                    :::8080                               :::*                  
LISTEN     0      100                    :::8081                               :::*                  
LISTEN     0      100                    :::8019                               :::*                  
LISTEN     0      128                    :::22                                 :::*                  
LISTEN     0      128                   ::1:631                                :::*                  
LISTEN     0      100                   ::1:25                                 :::*                  
LISTEN     0      1        ::ffff:127.0.0.1:8005                               :::*                  
LISTEN     0      100                    :::8009                               :::*                  
LISTEN     0      80                     :::3306                               :::*       

修改临时密码

[root@localhost mysql]# mysql -uroot -p
Enter password:
mysql> set password = password(‘guohui123‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
[root@localhost nginx-1.14.0]# cd /usr/local/nginx/
配置nginx实现动静分离
[root@localhost nginx]# vim conf/nginx.conf
  keepalive_timeout  65;
    upstream web {
    server 192.168.56.11:8080;              //设置web设置集群,实行负载均衡,tomcat网站192.168.56.11:8080和tomcat网站192.168.56.11:8081
    server 192.168.56.11:8081;
}
    server {
        listen       80;
        server_name  localhost;
        access_log  logs/host.access.log  main;
   location / {
            root   html;
            index  index.html index.htm;
}
        location ^\.(do | jsp)$ {
        proxy_pass http://web;          //proxy_pass
        }
//测试            
[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx -s reload

浏览器测试,负载均衡达成,如需要每个ip访问的网页不变,则可以在 upstream web中加入ip_hash
技术分享图片
技术分享图片
刷新
技术分享图片

LINUX——Nginx反代至双Tomcat(LNMT、LAMT),负载均衡中动静分离的使用

标签:tle   mod   exec   webapps   evel   编译   temp   environ   pst   

原文地址:http://blog.51cto.com/13859004/2170882

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!