LINUX——Nginx反代至双Tomcat(LNMT、LAMT),负载均衡中动静分离的使用
lnmt架构http://i2.运维网.com/images/blog/201809/06/1be753faaa1d40d000a058408980bc3a.jpg
环境布置:
配置安装163的源
# cd /etc/yum.repos.d/
//创建备份目录
# mkdir /etc/repo-bf
//将原yum仓库的文件备份到repo-bf
# mv * /etc/repo-bf
//下载163的源到yum仓库
# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
//将改为版本的7
# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/163.repo
# sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/163.repo
//自动配置yum的软件仓库,也可以自己配置
# yum -y install epel-release
# yum clean all
//安装编译环境
# yum -y install gcc gcc-c++
# yum -y install wget
tomcat项目部署
java环境安装
# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
//查看安装的版本
# 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,在官网下载
这里是下载核心的
# cd /usr/src/
# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.11/bin/apach
mcat-9.0.11.tar.gz
//解压部署,只需要解压无需安装
//解压部署,只需要解压无需安装
# ls
apache-tomcat-9.0.11.tar.gz debugkernels
解压到/usr/local/
# tar xf apache-tomcat-9.0.11.tar.gz -C /usr/local/
将解压后的tomcat包的软件接,命名tomcat
# cd /usr/local
# ln -s apache-tomcat-9.0.11/ tomcat
# ls
apache-tomcat-9.0.11binetcgamesincludeliblib64libexecsbinsharesrctomcat
# ll //软连接
lrwxrwxrwx. 1 root root20 9月 5 12:03 tomcat -> apache-tomcat-9.0.8/
因为是java程序,一般服务器内存至少8-16g ,并且tomcat在一台服务器上不可能只跑一个
进入tomcat包配置,以及介绍
# cd 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界面
# cd webapps/
# mkdir test
# cd test
# vim index.jsp
# cat index.jsp
test page
启动
//检查
# /usr/local/tomcat/bin/configtest.sh
//启动
# /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.
# 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 :::*
http://i2.运维网.com/images/blog/201809/05/44df5829903e5783f4bb0d3118378901.png
http://i2.运维网.com/images/blog/201809/05/9610aff0265bead45b153f813a9083d6.png
现在再来一个汤姆猫
http://i2.运维网.com/images/blog/201809/05/1281ebe535ca2c4364114108b0004213.png
再次回到tomcat包的目录
# cd /usr/src/
解压并将解压后的文件移动到/usr/local/,重命名为apache-tomcat-9.0.11-tom2
# tar xf apache-tomcat-9.0.11.tar.gz
# mv apache-tomcat-9.0.11 /usr/local/apache-tomcat-9.0.11-tom2
将解压后的apache-tomcat-9.0.11-tom2包创建软件接,命名tomcat2
# cd /usr/local
# ln -s apache-tomcat-9.0.11-tom2/ tomcat2
# ls
apache-tomcat-9.0.11 bingames lib libexecsharetomcat
apache-tomcat-9.0.11-tom2etcincludelib64sbin src tomcat2
# ll //软连接
进入webapps,并配置网页,在启动tomcat2
先修改端口配置
# cd tomcat2/conf
# ls
catalina.policy context.xml jaspic-providers.xsdserver.xml tomcat-users.xsd
catalina.propertiesjaspic-providers.xmllogging.properties tomcat-users.xmlweb.xml
# vim server.xml //在原有的端口稍作改变即可
/etc/profile.d/nginx.sh
# . /etc/profile.d/nginx.sh
//启动
# nginx
nginx的配置文件
主配置文件 /usr/local/nginx/conf/nginx.conf
2.安装mysql
//安装依赖包
# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
# groupadd -r -g 306 mysql
# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
下载二进制格式的mysql软件包
# cd /usr/src/
# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
# cd /usr/src/
# ls
debugkernelsmysql-5.7.22-linux-glibc2.12-x86_64.tar.gznginx-1.14.0nginx-1.14.0.tar.gz
# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
# ls/usr/local/
binetcgamesincludeliblib64libexecmysql-5.7.22-linux-glibc2.12-x86_64nginxsbinsharesrc
# cd/usr/local/
# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
//修改属主
# chown -R mysql.mysql /usr/local/mysql
# 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/
//设置环境变量
# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
# . /etc/profile.d/mysql.sh
# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
# cd /usr/local/mysql
# mkdir /opt/data
# chown -R mysql.mysql /opt/data/
//初始化数据库,最后生成的是一个临时密码
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-05T09:17:21.026072Z 1 A temporary password is generated for root@localhost: ?3/(1tqF=gwu
配置mysql
# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
# ldconfig -v
//生成配置文件
# cat > /etc/my.cnfquit
# cd /usr/local/nginx/
配置nginx实现动静分离
# vim conf/nginx.conf
keepalive_timeout65;
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_namelocalhost;
access_loglogs/host.access.logmain;
location ~* \.(do|jsp)$ {
proxy_pass http://web;
}
location ~* tomcat\.(png|css)$ {
proxy_pass http://web;
}
location / {
root html;
index index.html index.htm;
}
}
//测试
# 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
# nginx -s reload
浏览器测试,负载均衡达成,如需要每个ip访问的网页不变,则可以在 upstream web中加入ip_hash
静态资源访问nginx
http://i2.运维网.com/images/blog/201809/06/b60565e40d70d636b0f2f17ed1930cc9.jpg
动态资源访问tomcat
http://i2.运维网.com/images/blog/201809/06/71d21b2660d2a47fd491011fe268bf7b.jpg
之前创建的test
http://i2.运维网.com/images/blog/201809/06/1a776173566401c29d72dc249c8d694d.jpg
刷新
http://i2.运维网.com/images/blog/201809/06/800880a2182708ec14d540b291ee2aae.jpg
页:
[1]