LINUX——Nginx反代至双Tomcat(LNMTLAMT),负载均衡中动静分离的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINUX——Nginx反代至双Tomcat(LNMTLAMT),负载均衡中动静分离的使用相关的知识,希望对你有一定的参考价值。
环境布置:
配置安装163的源
[[email protected] ~]# cd /etc/yum.repos.d/
//创建备份目录
[[email protected] yum.repos.d]# mkdir /etc/repo-bf
//将原yum仓库的文件备份到repo-bf
[[email protected] yum.repos.d]# mv * /etc/repo-bf
//下载163的源到yum仓库
[[email protected] yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
//将改为版本的7
[[email protected] yum.repos.d]# sed -i ‘s/$releasever/7/g‘ /etc/yum.repos.d/163.repo
[[email protected] yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo
//自动配置yum的软件仓库,也可以自己配置
[[email protected] yum.repos.d]# yum -y install epel-release
[[email protected] yum.repos.d]# yum clean all
//安装编译环境
[[email protected] yum.repos.d]# yum -y install gcc gcc-c++
[[email protected] yum.repos.d]# yum -y install wget
tomcat项目部署
java环境安装
[[email protected] yum.repos.d]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
//查看安装的版本
[[email protected] 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,在官网下载
这里是下载核心的
[[email protected] yum.repos.d]# cd /usr/src/
[[email protected] src]# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.11/bin/apach
mcat-9.0.11.tar.gz
//解压部署,只需要解压无需安装
//解压部署,只需要解压无需安装
[[email protected] src]# ls
apache-tomcat-9.0.11.tar.gz debug kernels
解压到/usr/local/
[[email protected] src]# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/
将解压后的tomcat包的软件接,命名tomcat
[[email protected] src]# cd /usr/local
[[email protected] local]# ln -s apache-tomcat-9.0.11/ tomcat
[[email protected] local]# ls
apache-tomcat-9.0.11 bin etc games include lib lib64 libexec sbin share src tomcat
[[email protected] local]# ll //软连接
lrwxrwxrwx. 1 root root 20 9月 5 12:03 tomcat -> apache-tomcat-9.0.8/
因为是java程序,一般服务器内存至少8-16g ,并且tomcat在一台服务器上不可能只跑一个
进入tomcat包配置,以及介绍
[[email protected] local]# cd tomcat/
[[email protected] 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界面
[[email protected] local]# cd webapps/
[[email protected] webapps]# mkdir test
[[email protected] webapps]# cd test
[[email protected] test]# vim index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hellow World");
%>
</body>
</html>
启动
//检查
[[email protected] webapps]# /usr/local/tomcat/bin/configtest.sh
//启动
[[email protected] 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.
[[email protected] 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包的目录
[[email protected] local]# cd /usr/src/
解压并将解压后的文件移动到/usr/local/,重命名为apache-tomcat-9.0.11-tom2
[[email protected] src]# tar xf apache-tomcat-9.0.11.tar.gz
[[email protected] src]# mv apache-tomcat-9.0.11 /usr/local/apache-tomcat-9.0.11-tom2
将解压后的apache-tomcat-9.0.11-tom2包创建软件接,命名tomcat2
[[email protected] src]# cd /usr/local
[[email protected] local]# ln -s apache-tomcat-9.0.11-tom2/ tomcat2
[[email protected] 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
[[email protected] local]# ll //软连接
进入webapps,并配置网页,在启动tomcat2
先修改端口配置
[[email protected] local]# cd tomcat2/conf
[[email protected] 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
[[email protected] 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" />
[[email protected] conf]# cd /usr/local/tomcat2/webapps/
[[email protected] webapps]# ls
docs examples host-manager manager ROOT
[[email protected] webapps]# mkdir test
[[email protected] webapps]# cd test
[[email protected] test]# vim index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("123456");
%>
</body>
</html>
启动
//检查
[[email protected] webapps]# /usr/local/tomcat2/bin/configtest.sh
//启动
[[email protected] webapps]# /usr/local/tomcat2/bin/catalina.sh start
[[email protected] 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 :::*
[[email protected] webapps]# cd
不同端口
第二个tomcat同ip不同端口的网页也搭建成功
下面开始搭建nging和数据库
可以在其他服务器搭建也可以就在本服务器搭建,
使用nginx,实现负载均衡
可在本服务器设置,也可以在其他服务器设置反向代理tomcat网页的ip和端口
安装依赖包
//创建用户
[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx
//安装编译环境
[[email protected] ~]# yum -y groups list
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
//创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx,编译安装
[[email protected] ~]# cd /usr/src/
[[email protected] src]#
[[email protected] src]# yum -y install wget
[[email protected] src]# wget http://64.123.28.133/files/21490000000827F6/nginx.org/download/nginx-1.14.0.tar.gz
[[email protected] src]# tar xf nginx-1.14.0.tar.gz
[[email protected] src]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# yum -y install gcc gcc-c++[[email protected] 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
[[email protected] nginx-1.14.0]# make -j 4 && make install
//设置环境变量
[[email protected] nginx-1.14.0]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
[[email protected] nginx-1.14.0]# . /etc/profile.d/nginx.sh
//启动
[[email protected] nginx-1.14.0]# nginx
nginx的配置文件
主配置文件 /usr/local/nginx/conf/nginx.conf
2.安装mysql
//安装依赖包
[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
下载二进制格式的mysql软件包
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
[[email protected] ~]# cd /usr/src/
[[email protected] 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
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 nginx sbin share src
[[email protected] src]# cd /usr/local/
[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
//修改属主
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] 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/
//设置环境变量
[[email protected] local]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] local]# . /etc/profile.d/mysql.sh
[[email protected] local]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[[email protected] local]# cd /usr/local/mysql
[[email protected] mysql]# mkdir /opt/data
[[email protected] mysql]# chown -R mysql.mysql /opt/data/
//初始化数据库,最后生成的是一个临时密码
[[email protected] 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 [email protected]: ?3/(1tqF=gwu
配置mysql
[[email protected] mysql]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[[email protected] mysql]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[[email protected] mysql]# ldconfig -v
//生成配置文件
[[email protected] ~]# 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
配置启动脚本
[email protected] mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# sed -ri ‘s#^(basedir=).*#1/usr/local/mysql#g‘ /etc/init.d/mysqld
[[email protected] mysql]# sed -ri ‘s#^(datadir=).*#1/opt/data#g‘ /etc/init.d/mysqld
启动mysql
[[email protected] mysql]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/localhost.localdomain.err‘.
SUCCESS!
[[email protected] 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
[[email protected] 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 :::*
修改临时密码
[[email protected] mysql]# mysql -uroot -p
Enter password:
mysql> set password = password(‘guohui123‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
[[email protected] nginx-1.14.0]# cd /usr/local/nginx/
配置nginx实现动静分离
[[email protected] 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
}
//测试
[[email protected] 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
[[email protected] nginx]# nginx -s reload
浏览器测试,负载均衡达成,如需要每个ip访问的网页不变,则可以在 upstream web中加入ip_hash
刷新
以上是关于LINUX——Nginx反代至双Tomcat(LNMTLAMT),负载均衡中动静分离的使用的主要内容,如果未能解决你的问题,请参考以下文章
Nginx反代至Tomcat基于memcached的session保持