目錄名 | 作用 |
---|---|
bin | 存放啟動和關閉Tomcat指令碼 |
conf | 存放Tomcat不同的組態檔 |
doc | 存放Tomcat檔案 |
lib | 存放Tomcat執行需要的庫檔案 |
logs | 存放Tomcat執行時的LOG檔案 |
src | 存放Tomcat的原始碼 |
webapps | Tomcat的主要Web釋出目錄 |
work | 存放jsp編譯後產生的class檔案 |
Nginx實現負載均衡是通過反向代理實現
反向代理原理:
動靜分離原理
[root@localhost ~]# tar xf jdk-8u144-linux-x64.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# cp -rv jdk1.8.0_144/ /usr/local/java
[root@localhost opt]# vi /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 opt]# source /etc/profile
[root@localhost opt]# java -version
java version "1.8.0_144"
……省略部分
[root@localhost ~]# tar vxf apache-tomcat-8.5.23.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@localhost opt]# tomcatup
……省略部分
Tomcat started.
[root@localhost opt]# netstat -anpt | grep 8080
tcp6 0 0 :::8080 ::: * LISTEN 77010/java
[root@localhost tomcat8]# vim /usr/local/tomcat8/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="false" > //插入這兩行設定
</Context>
[root@localhost tomcat8]# mkdir -pv /web/webapp1 //建立網頁目錄
[root@localhost tomcat8]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<h1>
<% out.println("動態頁面 1,http://www.TOM1.com");%>
</h1>
</body>
<body>
<div>靜態頁面的圖片 1</div><br><img src="123.jpg">
<br><h1>
床前看月光,<br>
疑是地上霜。<br>
擡頭望山月,<br>
低頭思故鄉。<br>
</h1>
</body>
</html>
[root@localhost tomcat8]# tomcatdown
[root@localhost tomcat8]# tomcatup
[root@localhost ~]# tar xf jdk-8u144-linux-x64.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# cp -rv jdk1.8.0_144/ /usr/local/java
[root@localhost opt]# vi /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 opt]# source /etc/profile
[root@localhost opt]# java -version
java version "1.8.0_144"
……省略部分
[root@localhost ~]# tar vxf apache-tomcat-8.5.23.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@localhost opt]# tomcatup
……省略部分
Tomcat started.
[root@localhost opt]# netstat -anpt | grep 8080
tcp6 0 0 :::8080 ::: * LISTEN 77010/java
[root@localhost tomcat8]# vim /usr/local/tomcat8/conf/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="false" >
</Context>
[root@localhost tomcat8]# mkdir -pv /web/webapp1
[root@localhost tomcat8]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<h1>
<% out.println("動態頁面2,http://www.TOM1.com");%>
</h1>
</body>
<body>
<div>靜態頁面的圖片 2</div><br><img src="123.jpg">
<br><h1>
床前看月光,<br>
疑是地上霜。<br>
擡頭望山月,<br>
低頭思故鄉。<br>
</h1>
</body>
</html>
[root@localhost tomcat8]# tomcatdown
[root@localhost tomcat8]# tomcatup
### 1.安裝環境
[root@localhost opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -g www www -s /bin/false
補充:/bin/false是最嚴格的禁止login選項,一切服務都不能用。
/sbin/nologin只是不允許login系統
[root@localhost ~]# tar xzvf nginx-1.15.9.tar.gz -C /opt
[root@localhost ~]# cd /opt/nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module
[root@localhost nginx-1.15.9]# make -j3 && make install
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# 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 nginx-1.15.9]# nginx
[root@localhost nginx-1.15.9]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14763/nginx: master
[root@localhost nginx-1.15.9]# yum -y install psmisc
[root@localhost nginx-1.15.9]# killall -9 nginx
[root@localhost nginx-1.15.9]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/ki11 -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.15.9]# systemctl enable nginx.service
[root@localhost nginx-1.15.9]# systemctl start nginx.service
[root@localhost nginx-1.15.9]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-09-25 20:08:41 CST; 6s ago
Process: 14822 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, sta
[root@localhost img]# vim /usr/local/nginx/conf/nginx.conf
http {
……省略部分
upstream tomcat_server { //在http{}區域內插入這4行設定
server 192.168.100.22:8080 weight=1;
server 192.168.100.23:8080 weight=1;
}
###docBase:web 應用的檔案基準目錄
###reloadable 設定監視"類"是否變化
###path="" 設定預設"類"
server {
location ~ .*.jsp$ { //在server{}區域內插入以下設定
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;
}
[root@localhost nginx-1.15.9]# 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>
</body>
</html>
[root@localhost nginx-1.15.9]# mkdir /usr/local/nginx/html/img
[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html/img
[root@localhost img]# ls
123.jpg