nginx反向代理+nginx黑白名单+nginx负载均衡+平滑升级+配置jdk环境-7.30

一、反向代理

1.前端服务器配置

前端服务器:用于接收和响应客户端,代理另一台主机

Nginx 安装

(1).下载安装nginx

[root@web ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz

(2).解压

[root@web ~ ]# tar -zxvf nginx-1.26.1.tar.gz

(3).编译安装 nginx
(1)安装 pcre

⾸先安装 pcre。此软件是为了⽀持 rewrite(重写、复写)功能⽽存在的。rewrite 是实现 url 重定向的重要命令,它会根据正则表达式来匹配内容,从⽽跳转到⽬标上⾯去。

[root@web ~]# yum -y install pcre-devel

(2)安装 OpenSSL

当没有使⽤ ssl 证书对服务器数据进⾏加密认证时,⽤户的数据将会以明⽂的形式进⾏传输,⽽此时,⽤户的数据可以被⼀些抓包⼯具获取,就容易造成⽤户的信息泄露。

所以为了改善这种情况,作为运维⼈员需要去为⽹站配置 ssl 证书,实现 https 协议的访问

[root@web ~]# yum -y install openssl-devel

(3)在安装之前还要安装⽤来编译的⼯具

gcc、gcc-c++、make;lrzsz 是⽤来从 Windows 上直接将⽂件导⼊到 Linux 内的⼯具。

[root@web ~]# yum -y install gcc gcc-c++ make

(4)编译安装 nginx

如果之前在系统上有 yum 安装的 nginx,那么在编译安装之前 需要先卸载掉原来的 nginx。

卸载nginx:yum -y remove nginx

(5)进行编译

[root@web ~]# ls nginx-1.26.1  nginx-1.26.1.tar.gz

 cd到安装包⽬录下  

[root@web ~]# cd nginx-1.26.1/

[root@web nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

命令分步解释:

[root@web nginx-1.26.1]# make && make install

[root@web nginx-1.26.1]# useradd -s /bin/nologin -M nginx

(6)检查目录

[root@web nginx-1.26.1]# tree /usr/local/nginx  

[root@web nginx-1.26.1]# cd /usr/local/nginx/

[root@web nginx]# ls

conf  html  logs  sbin

(7)备份目录

[root@web nginx]# cp -r conf/ conf.bak  

[root@web nginx]# ./sbin/nginx  

(8)开放端口或者服务

[root@web nginx]# firewall-cmd --zone=public --add-port=80/tcp --permanent success [root@web nginx]# firewall-cmd --reload success  

[root@web nginx]# vim /usr/local/nginx/conf/nginx.conf

(9)创建符号链接(软链接)

[root@web nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/

[root@web nginx]# ls -l

/usr/bin/nginxlrwxrwxrwx. 1 root root 27 7月 29 16:16 /usr/bin/nginx -> /usr/local/nginx/sbin/nginx

[root@web nginx]# nginx

[root@web nginx]# nginx -s stop

[root@web nginx]# netstat -lnput|grep nginx

[root@web nginx]# nginx

[root@web nginx]# netstat -lnput|grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17228/nginx: master

(10)启动和关闭nginx服务

./nginx ./nginx -s reload

修改了配置文件后,重载nginx服务网

./nginx -s reload

(11)脚本启动nginx服务:

[root@web nginx]# vim ~/nginx.sh

[root@web nginx]# bash ~/nginx.sh

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17228/nginx: master

nginx正在执行,或者是80端口被占用

[root@web nginx]# nginx -s stop

[root@web nginx]# bash ~/nginx.sh

12)以systemctl控制nginx

[root@web nginx]# vim /usr/lib/systemd/system/nginx.service

如果直接使用sbin目录下的nginx,就无法使用systemctl

这两套命令只能用一套

(1)[root@web nginx]# systemctl daemon-reload

​ [root@web nginx]# systemctl stop nginx

(2)[root@web nginx]# nginx -s reload [root@web nginx]# nginx -s stop

(13)添加监控块

[root@web nginx]# vim /usr/local/nginx/conf/nginx.conf

[root@web nginx]# systemctl restart nginx

2.后端服务器

[root@web ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz

[root@web ~ ]# tar -zxvf nginx-1.26.1.tar.gz

[root@web ~]# yum -y install gcc gcc-c++

[root@web ~]# yum -y install openssl-devel pcre-devel make

[root@web ~]# lsnginx-1.26.1 nginx-1.26.1.tar.gz

[root@web ~]# cd nginx-1.26.1/

[root@web nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

[root@web nginx-1.26.1]# make && make install  

[root@web nginx-1.26.1]# useradd -s /bin/nologin -M nginx  

[root@nginx nginx-1.26.1]# echo "我是后端服务" > /usr/local/nginx/html/index.html

[root@nginx nginx-1.26.1]# firewall-cmd --zone=public --add-port=80/tcp --permanent

success  [root@nginx nginx-1.26.1]# firewall-cmd --reload

success  

修改配置文件  

[root@nginx nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf

将其修改为utf-8(防止出现乱码)  

[root@nginx nginx-1.26.1]# /usr/local/nginx/sbin/nginx  

[root@nginx nginx-1.26.1]# /usr/local/nginx/sbin/nginx -s reload  

浏览器访问:192.168.2.27

[root@nginx ~]# echo "this is java web server" > /usr/local/nginx/html/index.html

[root@nginx ~]# #启动nginx

[root@nginx ~]# /usr/local/nginx/sbin/nginx

[root@nginx ~]# #使用curl访问当前项目

[root@nginx ~]# curl localhost

this is java web server

3.前端服务器配置

[root@web ~]# echo "this is static server" > /usr/local/nginx/html/index.html 
[root@web ~]# #启动nginx服务 
[root@web ~]# /usr/local/nginx/sbin/nginx 
[root@web ~]# ps -aux|grep nginx 
[root@web ~]# curl localhost 
this is static server 
[root@web ~]# curl 192.168.2.25 
this is static server 
使用25代理27主机,当用户访问25的时候,25不响应,27主机响应 
使用25主机nginx反向代理27的服务器 
[root@web ~]# #location proxy_pass 协议 域名 端口

修改配置文件 /usr/local/nginx/conf/nginx.conf

[root@web ~]# vim /usr/local/nginx/conf/nginx.conf


 

[root@web ~]# /usr/local/nginx/sbin/nginx -s reload

浏览器访问IP:192168.2.25,访问到的内容是后端服务器的

现在的静态服务器实际上是代理服务器,nginx代理其他服务的时候,不需要对方同意

二、设置黑名单、白名单

1.安装配置nginx环境

2.修改一下index.html中的内容

[root@nginx-1 nginx-1.26.1]# echo "you are luckly" > /usr/local/nginx/html/index.html [root@nginx-1 nginx-1.26.1]# curl localhost

you are luckly  

3.修改配置文件 此台服务器IP:192.168.2.28  

前端服务器IP:192.168.2.25  

后端服务器IP:192.1682.27  

设置除开27(后端服务器)可以访问,其他主机都不可以访问  在配置文件中的server模块中设置,allow为允许,deny为禁止,可以对IP生效,也可以对网段生效  

[root@nginx-1 nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf

4.重启服务

[root@nginx-1 nginx-1.26.1]# /usr/local/nginx/sbin/nginx -s reload  

5.测试 

分别在前端服务器(25)、后端服务器(27)进行测试:  

前端服务器(25):

[root@web ~]# curl 192.168.2.28
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>

后端服务器(27):

[root@nginx ~]# curl 192.168.2.28
you are luckly

6.说明:

[root@web ~]# curl 192.168.2.28
curl: (7) Failed connect to 192.168.2.28:80; 没有到主机的路由

如果出现这种情况,查看是否能ping通外网,是否能ping通192.168.2.28这台主机,最后检查防火墙有没有关

三、负载均衡

1.负载均衡状态

2.环境准备

让每一台主机能够获得相应的压力

轮询:依次的将任务部署给不同的主机

1.准备四台机器:
编号主机名IP地址
1static-server192.168.2.25
2001192.168.2.27
3002192.168.2.28
4003192.168.2.29

2.都配置nginx环境

wget https://nginx.org/download/nginx-1.26.1.tar.gz  

tar -zxvf nginx-1.26.1.tar.gz   yum -y install pcre-devel  

yum -y install openssl-devel  yum -y install gcc gcc-c++ make  

cd nginx-1.26.1/  

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream  

make && make install  

useradd -s /bin/nologin -M nginx  

3.写测试页面:

[root@static-server ~]# echo "I am static server" > /usr/local/nginx/html/index.html  

[root@001 ~]# echo "I am 001 server" > /usr/local/nginx/html/index.html  

[root@002 ~]# echo "I am 002 server" > /usr/local/nginx/html/index.html  

[root@003 ~]# echo "I am 003 server" > /usr/local/nginx/html/index.html 

4.启动服务(static-server、001、002、003):

/usr/local/nginx/sbin/nginx

3.负载均衡策略

(1)配置代理和轮询

[root@static-server ~]# vim /usr/local/nginx/conf/nginx.conf

轮询:

代理:

[root@static-server ~]# /usr/local/nginx/sbin/nginx -s reload

浏览器访问static-server的IP:192.168.2.25

static-server会代理001、002、003这三台机器,刷新浏览器会出现轮询

(2)weight加权

权重默认为1,谁权重大,谁优先处理请求

[root@static-server ~]# vim /usr/local/nginx/conf/nginx.conf

[root@static-server ~]# /usr/local/nginx/sbin/nginx -s reload

浏览器访问IP:192.168.2.25

001出现的次数最多,003出现的次数最少

(3)ip_hash

当对后端的多台动态应用服务器做负载均衡时,ip_hash指令能够将某个客户端IP的请求通过哈希算法定位到同一台后端服务器上。

这样,当来自某一个IP的用户在后端Web服务器A上登录后,再访问该站点的其他URL,能保证其访问的还是后端web服务器A。

注意: 使用ip_hash指令无法保证后端服务器的负载均衡,可能导致有些后端服务器接收到的请求多,有些后端服务器接受的请求少,而且设置后端服务器权重等方法将不起作用

[root@static-server ~]# vim /usr/local/nginx/conf/nginx.conf

[root@static-server ~]# /usr/local/nginx/sbin/nginx -s reload

浏览器访问IP:192.168.2.25

第一次访问到001的测试页面,之后刷新浏览器一直是001的测试页面,不会变。

(4)least_conn

least_conn:最少连接,把请求转发给连接数较少的后端服务器。轮询算法是把请求平均地转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,leastconn这种方式就可以达到更好的负载均衡效果。

(5)url_hash

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用ur_hash,可以使得同一个url (也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再次收到请求,就可以从 缓存中读取。

 

四、平滑升级

不停用业务,使用平滑升级

需要有kill命令的支持

kill不仅仅用于杀死进程,还可以向软件进程发送信号

常用的-9和-15一个是强杀,一个是正常杀

格式:kill 信号 进程信号

-USR2 平滑启动一个进程,平滑升级

-WINCH 优雅关闭子进程

-QUIT 优雅关闭主进程

步骤:

1.不停止原有服务,但是必须使用原生方式启动或者更改nginx脚本(会创建一个新的进程)

2.重新编译nginx新版本

3.使用kill -USR@启动新版本

4.把旧的nginx子进程全部退出

5.优雅的退出nginx的老进程,系统里就只剩下新的nginx了

实施步骤:

1.查看nginx当前版本

#查看nginx当版本

[root@static-server ~]# /usr/local/nginx/sbin/nginx -v

nginx version: nginx/1.26.1
2.上传新的新nginx版本并解压

平滑升级到1.27

服务持续期间对nginx进行升级

# 上传新的新nginx版本并解压(1.27版本)

[root@static-server ~]# wget https://nginx.org/download/nginx-1.27.0.tar.gz

[root@static-server ~]#tar -zxvf nginx-1.27.0.tar.gz 
3.对新版本进行编译安装,安装目录必须和旧版本一致
# 对新版本进行编译安装,安装目录必须和旧版本一致

[root@static-server ~]#cd nginx-1.27.0/

[root@static-server ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

[root@static-server ~]#make && make install
4.对新版本进行编译安装,安装目录必须和旧版本一致
# 重装新的版本以后,会出现新的启动工具

[root@static-server nginx-1.27.0]# ls /usr/local/nginx/sbin

nginx  nginx.old

5.查看nginx的新旧版本

#查看nginx的新旧版本
[root@static-server nginx-1.27.0]# /usr/local/nginx/sbin/nginx -v

nginx version: nginx/1.27.0

[root@static-server nginx-1.27.0]#/usr/local/nginx/sbin/nginx.old -v

nginx version: nginx/1.26.1
6.查看进程

[root@static-server nginx-1.27.0]# ps -aux|grep nginx

# 升级过程中保持服务不中断

[root@static-server nginx-1.27.0]# ps -aux|grep nginx
root       4432  0.0  0.2  46172  2036 ?        Ss   10:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4762  0.0  0.2  46600  2176 ?        S    16:09   0:00 nginx: worker process
root       7749  0.0  0.0 112808   680 pts/1    R+   16:34   0:00 grep --color=auto nginx
7.使用老的nginx进程创建新的进程
# 使用老的nginx进程创建新的进程

# 格式:kill -USR2 老版本的pid编号

[root@static-server nginx-1.27.0]# kill -USR2 4432
[root@static-server nginx-1.27.0]# ps -aux|grep nginx
root       4432  0.0  0.2  46172  2036 ?        Ss   10:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4762  0.0  0.2  46600  2176 ?        S    16:09   0:00 nginx: worker process
root       7751  0.0  0.3  46128  3344 ?        S    16:37   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      7752  0.0  0.1  46584  1912 ?        S    16:37   0:00 nginx: worker process
root       7754  0.0  0.0 112824   980 pts/1    R+   16:37   0:00 grep --color=auto nginx
[root@static-server nginx-1.27.0]# ps -aux|grep nginx
root       4432  0.0  0.2  46172  2036 ?        Ss   10:17   0:00 nginx: master p
nginx      4762  0.0  0.2  46600  2176 ?        S    16:09   0:00 nginx: worker p
root       7751  0.0  0.3  46128  3344 ?        S    16:37   0:00 nginx: master p
nginx      7752  0.0  0.1  46584  1912 ?        S    16:37   0:00 nginx: worker p
root       7756  0.0  0.0 112824   980 pts/1    R+   16:39   0:00 grep --color=au
8.关闭老版本的所有子进程和老版本的主进程

# 此时会出现两套master进程,这个时候处理客户请求的就是新nginx服务了

# 关闭老版本的所有子进程

# 关闭老版本的主进程

[root@static-server nginx-1.27.0]# kill -WINCH 4762
[root@static-server nginx-1.27.0]# ps -aux|grep nginx
root       4432  0.0  0.2  46172  2036 ?        Ss   10:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       7751  0.0  0.3  46128  3344 ?        S    16:37   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      7752  0.0  0.1  46584  1912 ?        S    16:37   0:00 nginx: worker process
nginx      7758  0.0  0.1  46600  1928 ?        S    16:40   0:00 nginx: worker process
root       7760  0.0  0.0 112824   976 pts/1    R+   16:40   0:00 grep --color=auto nginx
[root@static-server nginx-1.27.0]# kill -QUIT 4432
[root@static-server nginx-1.27.0]# ps -aux|grep nginx
root       7751  0.0  0.3  46128  3344 ?        S    16:37   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      7752  0.0  0.1  46584  1912 ?        S    16:37   0:00 nginx: worker process
root       7762  0.0  0.0 112808   680 pts/1    R+   16:41   0:00 grep --color=auto nginx

9.使用curl查看当前服务器的版本

# 使用curl查看当前服务器的版本

[root@static-server nginx-1.27.0]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.27.0
Date: Tue, 30 Jul 2024 08:42:08 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 16
Connection: keep-alive
Last-Modified: Tue, 30 Jul 2024 07:02:18 GMT
ETag: "66a88ffa-10"
Accept-Ranges: bytes

五、配置jdk环境

配置tomcat 10运行环境 tomcat9 可以在jdk8的环境运行

tomcat10必须在jdk17以上的版本运行

tomcat-22版本下载网址:https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz

步骤:

#下载jdk包
[root@static-server ~]# wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz
--2024-07-30 17:28:10--  https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz
正在解析主机 download.oracle.com (download.oracle.com)... 184.28.252.147
正在连接 download.oracle.com (download.oracle.com)|184.28.252.147|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:195275103 (186M) [application/x-gzip]
正在保存至: “jdk-22_linux-x64_bin.tar.gz”

100%[======================================>] 195,275,103 1.28MB/s 用时 5m 43s 

2024-07-30 17:33:57 (556 KB/s) - 已保存 “jdk-22_linux-x64_bin.tar.gz” [195275103/195275103])

[root@static-server ~]# ls
anaconda-ks.cfg              nginx-1.26.1         nginx-1.27.0         nginx.sh
jdk-22_linux-x64_bin.tar.gz  nginx-1.26.1.tar.gz  nginx-1.27.0.tar.gz


#解压jdk压缩包
[root@static-server ~]# tar -xf jdk-22_linux-x64_bin.tar.gz 
[root@static-server ~]# ls
anaconda-ks.cfg              nginx-1.26.1         nginx-1.27.0.tar.gz
jdk-22.0.2                   nginx-1.26.1.tar.gz  nginx.sh
jdk-22_linux-x64_bin.tar.gz  nginx-1.27.0

#进入到jdk-22.0.2/目录下
[root@static-server ~]# cd jdk-22.0.2/
[root@static-server jdk-22.0.2]# ls
bin  conf  include  jmods  legal  lib  LICENSE  man  README  release


#进入到bin目录下看是否能运行java
[root@static-server jdk-22.0.2]# cd bin
[root@static-server bin]# ./java
用法:java [options] <mainclass> [args...]
           (执行类)
   或  java [options] -jar <jarfile> [args...]
           (执行 jar 文件)
   或  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (执行模块中的主类)
   或  java [options] <sourcefile> [args]
           (执行源文件程序)

 将主类、源文件、-jar <jarfile>、-m 或
 --module <module>/<mainclass> 后的参数作为参数
 传递到主类。

 其中,选项包括:

    -cp <目录和 zip/jar 文件的类搜索路径>
    -classpath <目录和 zip/jar 文件的类搜索路径>
    --class-path <目录和 zip/jar 文件的类搜索路径>
                  使用 : 分隔的, 用于搜索类文件的目录, JAR 档案
                  和 ZIP 档案列表。
    -p <模块路径>
    --module-path <模块路径>...
                  : 分隔的元素列表,每个元素都是
                  模块或包含模块的目录的文件路径。每个模块都是
                  模块化 JAR 或展开的模块目录。
    --upgrade-module-path <模块路径>...
                  : 分隔的元素列表,每个元素都是
                  模块或包含模块(用于替换运行时映像中的
                  可升级模块)的目录的文件路径。每个模块都是
                  模块化 JAR 或展开的模块目录。
    --add-modules <模块名称>[,<模块名称>...]
                  除了初始模块之外要解析的根模块。
                  <模块名称> 还可以为 ALL-DEFAULT, ALL-SYSTEM,
                  ALL-MODULE-PATH.
    --enable-native-access <module name>[,<module name>...]
                  允许模块中的代码访问 Java 运行时之外的代码和数据。
                  <module name> 也可以是 ALL-UNNAMED,以指示类路径上的代码。
    --list-modules
                  列出可观察模块并退出
    -d <module name>
    --describe-module <模块名称>
                  描述模块并退出
    --dry-run     创建 VM 并加载主类, 但不执行 main 方法。
                  此 --dry-run 选项对于验证诸如
                  模块系统配置这样的命令行选项可能非常有用。
    --validate-modules
                  验证所有模块并退出
                  --validate-modules 选项对于查找
                  模块路径中模块的冲突及其他错误可能非常有用。
    -D<名称>=<值>
                  设置系统属性
    -verbose:[class|module|gc|jni]
                  为给定子系统启用详细输出
    -version      将产品版本输出到错误流并退出
    --version     将产品版本输出到输出流并退出
    -showversion  将产品版本输出到错误流并继续
    --show-version
                  将产品版本输出到输出流并继续
    --show-module-resolution
                  在启动过程中显示模块解析输出
    -? -h -help
                  将此帮助消息输出到错误流
    --help        将此帮助消息输出到输出流
    -X            将额外选项的帮助输出到错误流
    --help-extra  将额外选项的帮助输出到输出流
    -ea[:<程序包名称>...|:<类名>]
    -enableassertions[:<程序包名称>...|:<类名>]
                  按指定的粒度启用断言
    -da[:<程序包名称>...|:<类名>]
    -disableassertions[:<程序包名称>...|:<类名>]
                  按指定的粒度禁用断言
    -esa | -enablesystemassertions
                  启用系统断言
    -dsa | -disablesystemassertions
                  禁用系统断言
    -agentlib:<库名>[=<选项>]
                  加载本机代理库 <库名>, 例如 -agentlib:jdwp
                  另请参阅 -agentlib:jdwp=help
    -agentpath:<路径名>[=<选项>]
                  按完整路径名加载本机代理库
    -javaagent:<jar 路径>[=<选项>]
                  加载 Java 编程语言代理, 请参阅 java.lang.instrument
    -splash:<图像路径>
                  使用指定的图像显示启动屏幕
                  自动支持和使用 HiDPI 缩放图像
                  (如果可用)。应始终将未缩放的图像文件名 (例如, image.ext)
                  作为参数传递给 -splash 选项。
                  将自动选取提供的最合适的缩放
                  图像。
                  有关详细信息, 请参阅 SplashScreen API 文档
    @argument 文件
                  一个或多个包含选项的参数文件
    --disable-@files
                  阻止进一步扩展参数文件
    --enable-preview
                  允许类依赖于此发行版的预览功能
要为长选项指定参数, 可以使用 --<名称>=<值> 或
--<名称> <值>。


#对文件进行备份
[root@static-server bin]# cd
[root@static-server ~]# mv jdk-22.0.2/ /usr/local/jdk22/
[root@static-server ~]# ls /usr/local/jdk22/
bin   include     jmods  lib      man     release
conf  jdk-22.0.2  legal  LICENSE  README
[root@static-server ~]# ls
anaconda-ks.cfg              nginx-1.26.1         nginx-1.27.0         nginx.sh
jdk-22_linux-x64_bin.tar.gz  nginx-1.26.1.tar.gz  nginx-1.27.0.tar.gz
[root@static-server ~]# cd /usr/local/jdk22/
[root@static-server jdk22]# ls
bin   include     jmods  lib      man     release
conf  jdk-22.0.2  legal  LICENSE  README
[root@static-server jdk22]# pwd
/usr/local/jdk22
[root@static-server jdk22]# sed -n '$p' /etc/profile
PATH=$JAVA_HOME/bin:$PATH
[root@static-server jdk22]# sed -i '$aexport JAVA_HOME=/usr/local/jdk22/' /etc/profile
[root@static-server jdk22]# source /etc/profile
[root@static-server jdk22]# $JAVA_HOME
-bash: /usr/local/jdk22/: 是一个目录
[root@static-server jdk22]# java
-bash: java: 未找到命令
[root@static-server jdk22]# sed -i '$aPATH=$JAVA_HOME/bin:$PATH' /etc/profile
[root@static-server jdk22]# sed -n '$p' /etc/profile
PATH=$JAVA_HOME/bin:$PATH
[root@static-server jdk22]# source /etc/profile
[root@static-server jdk22]# java
用法:java [options] <mainclass> [args...]
           (执行类)
   或  java [options] -jar <jarfile> [args...]
           (执行 jar 文件)
   或  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (执行模块中的主类)
   或  java [options] <sourcefile> [args]
           (执行源文件程序)

 将主类、源文件、-jar <jarfile>、-m 或
 --module <module>/<mainclass> 后的参数作为参数
 传递到主类。

 其中,选项包括:

    -cp <目录和 zip/jar 文件的类搜索路径>
    -classpath <目录和 zip/jar 文件的类搜索路径>
    --class-path <目录和 zip/jar 文件的类搜索路径>
                  使用 : 分隔的, 用于搜索类文件的目录, JAR 档案
                  和 ZIP 档案列表。
    -p <模块路径>
    --module-path <模块路径>...
                  : 分隔的元素列表,每个元素都是
                  模块或包含模块的目录的文件路径。每个模块都是
                  模块化 JAR 或展开的模块目录。
    --upgrade-module-path <模块路径>...
                  : 分隔的元素列表,每个元素都是
                  模块或包含模块(用于替换运行时映像中的
                  可升级模块)的目录的文件路径。每个模块都是
                  模块化 JAR 或展开的模块目录。
    --add-modules <模块名称>[,<模块名称>...]
                  除了初始模块之外要解析的根模块。
                  <模块名称> 还可以为 ALL-DEFAULT, ALL-SYSTEM,
                  ALL-MODULE-PATH.
    --enable-native-access <module name>[,<module name>...]
                  允许模块中的代码访问 Java 运行时之外的代码和数据。
                  <module name> 也可以是 ALL-UNNAMED,以指示类路径上的代码。
    --list-modules
                  列出可观察模块并退出
    -d <module name>
    --describe-module <模块名称>
                  描述模块并退出
    --dry-run     创建 VM 并加载主类, 但不执行 main 方法。
                  此 --dry-run 选项对于验证诸如
                  模块系统配置这样的命令行选项可能非常有用。
    --validate-modules
                  验证所有模块并退出
                  --validate-modules 选项对于查找
                  模块路径中模块的冲突及其他错误可能非常有用。
    -D<名称>=<值>
                  设置系统属性
    -verbose:[class|module|gc|jni]
                  为给定子系统启用详细输出
    -version      将产品版本输出到错误流并退出
    --version     将产品版本输出到输出流并退出
    -showversion  将产品版本输出到错误流并继续
    --show-version
                  将产品版本输出到输出流并继续
    --show-module-resolution
                  在启动过程中显示模块解析输出
    -? -h -help
                  将此帮助消息输出到错误流
    --help        将此帮助消息输出到输出流
    -X            将额外选项的帮助输出到错误流
    --help-extra  将额外选项的帮助输出到输出流
    -ea[:<程序包名称>...|:<类名>]
    -enableassertions[:<程序包名称>...|:<类名>]
                  按指定的粒度启用断言
    -da[:<程序包名称>...|:<类名>]
    -disableassertions[:<程序包名称>...|:<类名>]
                  按指定的粒度禁用断言
    -esa | -enablesystemassertions
                  启用系统断言
    -dsa | -disablesystemassertions
                  禁用系统断言
    -agentlib:<库名>[=<选项>]
                  加载本机代理库 <库名>, 例如 -agentlib:jdwp
                  另请参阅 -agentlib:jdwp=help
    -agentpath:<路径名>[=<选项>]
                  按完整路径名加载本机代理库
    -javaagent:<jar 路径>[=<选项>]
                  加载 Java 编程语言代理, 请参阅 java.lang.instrument
    -splash:<图像路径>
                  使用指定的图像显示启动屏幕
                  自动支持和使用 HiDPI 缩放图像
                  (如果可用)。应始终将未缩放的图像文件名 (例如, image.ext)
                  作为参数传递给 -splash 选项。
                  将自动选取提供的最合适的缩放
                  图像。
                  有关详细信息, 请参阅 SplashScreen API 文档
    @argument 文件
                  一个或多个包含选项的参数文件
    --disable-@files
                  阻止进一步扩展参数文件
    --enable-preview
                  允许类依赖于此发行版的预览功能
要为长选项指定参数, 可以使用 --<名称>=<值> 或
--<名称> <值>。


#查看java版本
[root@static-server jdk22]# java -version
java version "22.0.2" 2024-07-16
Java(TM) SE Runtime Environment (build 22.0.2+9-70)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.2+9-70, mixed mode, sharing)
[root@static-server jdk22]# 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值