1、准备三台服务器
192.168.52.34 nginx
192.168.52.35 tomcat1
192.168.52.36 tomcat2
2、关闭防火墙
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0
3、在192.168.52.34安装nginx
[root@localhost ~]# rz [root@localhost ~]# ls anaconda-ks.cfg nginx-1.16.1.tar.gz original-ks.cfg [root@localhost ~]#
4、安装编译文件
[root@localhost ~]# yum -y install gcc gcc-c++ pcre-devel zlib-devel
5、解压、安装、编译nginx
[root@localhost ~]# tar zxf nginx-1.16.1.tar.gz [root@localhost ~]# cd nginx-1.16.1/ [root@localhost nginx-1.16.1]# ./configure && make && make install
6、开启nginx
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx
7、在192.168.52.35上安装tomcat
[root@localhost ~]# rz [root@localhost ~]# ls anaconda-ks.cfg apache-tomcat-8.5.35.tar.gz jdk-8u131-linux-x64_.rpm original-ks.cfg
8、解压tomcat、jdk
[root@localhost ~]# tar zxf apache-tomcat-8.5.35.tar.gz [root@localhost ~]# rpm -ivh jdk-8u131-linux-x64_.rpm Preparing... ################################# [100%] Updating / installing... 1:jdk1.8.0_131-2000:1.8.0_131-fcs ################################# [100%] Unpacking JAR files... tools.jar... plugin.jar... javaws.jar... deploy.jar... rt.jar... jsse.jar... charsets.jar... localedata.jar... [root@localhost ~]#
9、将tomcat复制到opt/tomcat中并开启
[root@localhost ~]# cp -r apache-tomcat-8.5.35 /opt/tomcat [root@localhost ~]# cd /opt/tomcat/ [root@localhost tomcat]# ls bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps work [root@localhost tomcat]# cd bin/ [root@localhost bin]# ./startup.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Tomcat started.
10、编写测试页面
[root@localhost bin]# cd /opt/tomcat/webapps/ROOT/ [root@localhost ROOT]# mv index.jsp index.pl [root@localhost ROOT]# ls asf-logo-wide.svg bg-middle.png bg-nav.png favicon.ico RELEASE-NOTES.txt tomcat.gif tomcat-power.gif WEB-INF bg-button.png bg-nav-item.png bg-upper.png index.pl tomcat.css tomcat.png tomcat.svg [root@localhost ROOT]# vim index.jsp 1111
11、在192.168.52.36上安装tomcat2
[root@localhost ~]# rz [root@localhost ~]# ls anaconda-ks.cfg apache-tomcat-8.5.35.tar.gz jdk-8u131-linux-x64_.rpm original-ks.cfg [root@localhost ~]# tar zxf apache-tomcat-8.5.35.tar.gz [root@localhost ~]# rpm -ivh jdk-8u131-linux-x64_.rpm Preparing... ################################# [100%] Updating / installing... 1:jdk1.8.0_131-2000:1.8.0_131-fcs ################################# [100%] Unpacking JAR files... tools.jar... plugin.jar... javaws.jar... deploy.jar... rt.jar... jsse.jar... charsets.jar... localedata.jar... [root@localhost ~]# cp -r apache-tomcat-8.5.35 /opt/tomcat [root@localhost ~]# cd /opt/tomcat/ [root@localhost tomcat]# ls bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps work [root@localhost tomcat]# cd bin/ [root@localhost bin]# ./startup.sh Using CATALINA_BASE: /opt/tomcat Using CATALINA_HOME: /opt/tomcat Using CATALINA_TMPDIR: /opt/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar Tomcat started.
[root@localhost bin]# cd /opt/tomcat/webapps/ [root@localhost webapps]# ls docs examples host-manager manager ROOT [root@localhost webapps]# cd ROOT/ [root@localhost ROOT]# mv index.jsp index.pl [root@localhost ROOT]# vim index.jsp 2222
12、编写nginx配置文件
在配置文件加入 vim /usr/local/nginx/conf/nginx.conf #keepalive_timeout 0; keepalive_timeout 65; upstream zcy { server 192.168.52.35:8080; server 192.168.52.36:8080; } #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://zcy; }
13、重启nginx
[root@localhost ~]# pkill nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
14、不断刷新nginxIP地址,就实现负载均衡