设备:安装两台Tomcat一台Nginx
tomcat1:IP为192.168.200.130
tomcat2:IP为192.168.200.122
nginx:IP为192.168.200.111
两台tomcat操作一致
一:安装配置Tomcat
上传jdk源码包
[root@localhost ~]rz -E
jdk-8u191-linux-x64.tar.gz
[root@localhost ~]#tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost ~]#mv jdk-8u191-linux-x64/ /usr/local/java //解压放到/usr/local/下取名为java
上传tomcat源码包
[root@localhost ~]rz -E
apache-tomcat-8.5.40.tar.gz
[root@localhost ~]#tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost ~]#mv apache-tomcat/ /usr/local/tomcat //将解包的目录放到/usr/local/取名为tomcat
环境配置
[root@localhost ~]#vim /etc/profile
在末尾添加 如下代码:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
[root@localhost ~]#source /etc/profile //source执行文件
[root@localhost ~]#java -version //查看JAVA旧版本号
[root@localhost ~]# rm -rf /usr/bin/java //删除JAVA旧版本号
[root@localhost ~]#source /etc/profile //source执行文件
[root@localhost ~]#java -version //查看JAVA新版本号
[root@localhost ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@localhost ROOT]# ls
asf-logo.png bg-nav-item.png favicon.ico tomcat.css tomcat.svg
asf-logo-wide.gif bg-nav.png index.jsp tomcat.gif WEB-INF
bg-button.png bg-upper.png tomcat.png
bg-middle.png build.xml RELEASE-NOTES.txt tomcat-power.gif
[root@localhost ROOT]# cp index.jsp index.jsp.bak
[root@localhost ROOT]# echo "192.168.200.130" > index.jsp #第一台tomcat
[root@localhost ROOT]# echo "192.168.200.122" > index.jsp #第二台tomcat
开启Tomcat服务
[root@localhost ~]#/usr/local/tomcat/bin/startup.sh
Tomcat端口为8080
[root@localhost ~]# netstat -lnpt | grep :8080
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
安装Nginx
安装Nginx依赖包 openssl-devel是安装ssl必要的模块
[root@localhost ~]#yum install gcc gcc-c++ openssl-devel pcre-devel zlib-devel make -y
创建用户是必做的,nginx通过用户来操作文件
[root@localhost ~]#useradd -M -s /sbin/nologin nginx
上传Nginx源码包
[root@localhost ~]# rz -E
z waiting to receive.**B0100000023be50
[root@localhost ~]# tar xf nginx-1.14.2.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.14.2/
[root@localhost ~nginx-1.14.2/]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
--prefix 设定Nginx的安装目录
--user和--group 指定Nginx运行用户和组
--with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计
--with-http_ssl_module 启用SSL模块
--with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件
为主程序 nginx 创建链接文件
[root@localhost ~]# ls /usr/local/nginx/
conf html logs sbin
[root@localhost ~]#ln -sf /usr/local/nginx/sbin/nginx /usr/local/sbin/ //做Nginx的软连接,为了更方便的找到配置文件位置
[root@localhost ~]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 9月 10 17:12 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
编写便于service管理的脚本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash # chkconfig: 2345 99 20 # description: Nginx Server Control Scripts shell PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf scgi_params.default
fastcgi.conf.default koi-win uwsgi_params
fastcgi_params mime.types nginx.conf.default uwsgi_params.default
fastcgi_params.default mime.types.default scgi_params win-utf
[root@localhost conf]# cp nginx.conf nginx.conf.bak
Nginx 修改主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 10240; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream tomcat_server { #线程池规定IP号及权重 server 192.168.200.130:8080 weight=2; #weight表示权重,权重越高,越高被分配的几率越高 server 192.168.200.122:8080 weight=1; } server { listen 80; server_name localhost; charset utf-8; access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://tomcat_server; #线程池名 proxy_set_header Host $http_host; #此处要添加host头,将头信息返回服务器,不加host可能报400错误 } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
[root@localhost conf]# 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 conf]# nginx
[root@localhost conf]# nginx -HUP nginx
[root@localhost conf]# netstat -anpt | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16275/nginx: master
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
测试两台tomcat同时运行: