腾讯云CentOS 7搭建简单Tomcat+nginx集群
1.安装Tomcat
进入 /usr/local/ 目录 cd /usr/local
下载 wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.22/bin/apache-tomcat-9.0.22.tar.gz
解压 tar -zxvf apache-tomcat-9.0.22.tar.gz
新建文件夹 mkdir tomcat-9.0.22
重命名 mv ./apache-tomcat-9.0.22 ./tomcat-9.0.22/tomcat1
解压 tar -zxvf apache-tomcat-9.0.22.tar.gz
重命名 mv ./apache-tomcat-9.0.22 ./tomcat-9.0.22/tomcat2
2.配置Tomcat
添加环境变量 vim /etc/profile
export CATALINA_HOME=/usr/local/tomcat-9.0.21/tomcat1 export CATALINA_BASE=/usr/local/tomcat-9.0.21/tomcat1 export TOMCAT_HOME=/usr/local/tomcat-9.0.21/tomcat1 export CATALINA_2_HOME=/usr/local/tomcat-9.0.21/tomcat2 export CATALINA_2_BASE=/usr/local/tomcat-9.0.21/tomcat2 export TOMCAT_2_HOME=/usr/local/tomcat-9.0.21/tomcat2
打开tomcat2/bin下的catalina.sh文件vim /usr/local/tomcat-9.0.22/tomcat2/bin/catalina.sh,在# OS specific support. $var _must_ be set to either true or false.下增加
export CATALINA_BASE=$CATALINA_2_BASE
export CATALINA_HOME=$CATALINA_2_HOME
打开tomcat2/conf下的server.xml文件vim /usr/local/tomcat-9.0.22/tomcat2/conf/server.xml,修改端口,避免启动服务器端口冲突
用一张png图片替换tomcat1/webapps/ROOT/下的tomcat.png图片,以区分两个服务器
分别启动两个tomcat,
cd /usr/local/tomcat-9.0.22/tomcat1/bin/
./startup.sh
cd /usr/local/tomcat-9.0.22/tomcat2/bin/
./startup.sh
请求测试对应的服务器
tomcat1:
tomcat2:
3.安装Nginx
yum install nginx
4.配置Nginx
vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
upstream tomcat9{
server IP地址:8080 weight=2;
server IP地址:9080 weight=2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 请求地址;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tomcat9;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
启动Nginx
cd /bin
./systemctl strat nginx