两台Tomcat配置
1:实施准备
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
2:查看JDK是否安装
[root@localhost ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
3:安装配置Tomcat
[root@localhost ~]# tar zxvf apache-tomcat-8.5.30.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.30 /usr/local/tomcat8
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
[root@localhost ~]# netstat -anpt | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 45388/java
浏览器测试Tomcat网站:127.0.0.1:8080
7:建立java的web站点
(1)建立web目录
[root@localhost ~]# mkdir -p /web/webapp1
(2)建立java测试页面
[root@localhost ~]# vi /web/webapp1/index.jsp
test web
(3)修改server.xml文件
[root@localhost ~]# vi /usr/local/tomcat8/conf/server.xml
在148--151行
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="true">
</Context>
(4)重启tomcat服务
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
(5)访问网站测试
nginx部分
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -g www www -s /bin/false [root@localhost ~]# tar xf nginx-1.12.0.tar.gz [root@localhost ~]# cd nginx-1.12.0 [root@localhost nginx-1.12.0]#
./configure --prefix=/usr/local/nginx --user=www -- group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_mo dule --with-http_flv_module
注释:
//--user=,--group= 指定运行的用户和组 //--with-file-aio 启用文件修改支持 //--with-http_stub_status_module 启用状态统计 //--with-http_gzip_static_module 启用 gzip 静态压缩 //--with-http_flv_module 启用 flv 模块,提供寻求内存使用基于时间的偏移量文件 //--with-http_ssl_module 启用 SSL 模块
[root@localhost nginx-1.12.0]# make && make install
编写脚本实现Nginx服务启动、停止、重载等操作
vim /etc/init.d/nginx
添加以下内容:
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
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)
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -anpt | grep nginx
else
echo "Nginx is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
esac
exit 0
为脚本添加执行权限
chmod +x /etc/init.d/nginx
添加nginx为系统服务
chkconfig --add nginx
启动nginx服务
systemctl start nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
添加红色部分内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 {
server 192.168.1.10:80 weight=1;
server 192.168.1.11:80 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ .*.jsp$ {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_server;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/img;
expires 30d;
}
下面是编辑 Nginx 静态页面文件
[root@localhost ~]# vim /usr/local/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>静态页面</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>静态页面</h1>
<p>这是个静态页面</p>
<img src="2.jpg"width="300"height="300">
</body>
</html>
[root@nginx ~]# mkdir /usr/local/nginx/html/img #创建静态文件目录
[root@nginx ~]#cp /root/logo.jpg /usr/local/nginx/html/img
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t #测试配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #启动nginx
查看 Nginx 服务进程
[root@localhost ~]# ps aux | grep nginx
root 13304 0.0 0.0 20492 624 ? Ss 17:59 0:00 nginx: master proc
ess /usr/local/nginx/sbin/nginx -c /usr/local/nginx/confnginx.conf
www 13305 0.0 0.1 20940 1348 ? S 17:59 0:00 nginx: worker proc
ess
root 13311 0.0 0.0 112664 972 pts/0 S+ 17:59 0:00 grep --color=auto
nginx 查看端口号及 PID 进程号。
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13304/nginx: master
测试静态网页
测试负载均衡