linux安装jdk1.8 +nginx +springboot 实现通过nginx80转发8888端口访问springboot程序

172 阅读2分钟

linux安装jdk1.8 +nginx +springboot 实现通过nginx80转发8888端口访问springboot程序

1.写一个简单helloWorld程序

image-20230922164316616

image-20230922164253604

打包成jar包,用于后续在云耀云服务器L实例上运行

2.安装nginx

Nginx是一款轻量级的[Web云耀云服务器L实例]、反向代理云耀云服务器L实例,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。

安装nginx环境依赖

  1. yum install -y gcc-c++

    编译时依赖gcc环境

    image-20230922165004391

  2. yum install -y pcre pcre-devel

    提供nginx支持重写功能

    image-20230922165034725

  3. yum install -y zlib zlib-devel

    zlib 库提供了很多压缩和解压缩的方式,nginx 使用 zlib 对 http 包内容进行 gzip 压缩

    image-20230922165058567

  4. yum install -y openssl openssl-devel

    安全套接字层密码库,用于通信加密

image-20230922165124934

下载Nginx安装包

官网:nginx.org/

  1. mkdir -p /opt/software
  2. cd /opt/software
  3. wget http://nginx.org/download/nginx-1.20.2.tar.gz

解压nginx

tar -xzvf nginx-1.20.2.tar.gz

image-20230922165629173

配置环境

cd nginx-1.20.2

检查平台安装环境

cd nginx-1.20.2

./configure
--prefix=/usr/local/nginx
--pid-path=/var/local/nginx/nginx.pid
--lock-path=/var/local/nginx/nginx.lock
--error-log-path=/var/local/nginx/error.log
--http-log-path=/var/local/nginx/access.log
--with-http_gzip_static_module
--http-client-body-temp-path=/var/local/nginx/client
--http-proxy-temp-path=/var/local/nginx/proxy
--http-fastcgi-temp-path=/var/local/nginx/fastcgi
--http-uwsgi-temp-path=/var/local/nginx/uwsgi
--http-scgi-temp-path=/var/local/nginx/scgi

image-20230922170000014

编译安装

make && make install

image-20230922170231912

启动Nginx

cd /usr/local/nginx/sbin

./nginx

image-20230922170418203

判断是否启动成功

华为云开放云耀云服务器L实例端口

登陆华为云 ,点击查看详情image-20230922171403205

点击安全组,点击配置规则

image-20230926103201099

这里开放80端口外网访问

image-20230926103307084

看到这个即可

image-20230926103346754

直接输入云耀云服务器L实例ip访问 nginx默认80端口

image-20230926103514341

2.看到这个也表示成功

ps -ef|grep nginx

image-20230922170446631

3.curl http://localhost

看到这个也表示成功

image-20230922170654345

常用命令

强制停止./nginx -s stop
优雅停止命令./nginx -s quit  // 优雅停止,是等最后一次交互执行完再停止
配置文件是否有错./nginx -t
重新加载命令./nginx -s reload
查看Nginx版本./nginx -v
查看Nginx详细版本./nginx -V
启动nginx./nginx -c /usr/local/nginx/conf/nginx.conf

配置nginx80端口转发到8088端口

image-20230926113330384

location /user {
	        proxy_pass http://127.0.0.1:8888;
	        proxy_redirect off;
	        proxy_set_header Host $host;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header X-Real-IP $remote_addr;
    		}

ps -ef|grep nginx

查找刚刚启动的nginx进程id 然后杀死

kill -9 16885

image-20230926104731262

image-20230926105016532

cd /usr/local/nginx/sbin

./nginx -c /usr/local/nginx/conf/nginx.conf

此时只需要在云耀云服务器L实例上启动一个8888端口的helloworld程序 即可实现直接通过域名访问到程序

3.安装jdk1.8

www.oracle.com/java/techno…

image-20230926110007249

将下载好的jdk直接拖拽到云耀云服务器L实例

image-20230926110235191

解压jdk

tar -zxvf jdk-18.0.2.1_linux-x64_bin.tar.gz

image-20230926110337583

配置环境变量

vim /etc/profile

#java environment

export JAVA_HOME=/opt/software/jdk-18.0.2.1 export JRE_HOME=JAVAHOME/jreexportCLASSPATH=.:{JAVA_HOME}/jre export CLASSPATH=.:{JAVA_HOME}/lib:JREHOME/libexportPATH={JRE_HOME}/lib export PATH={JAVA_HOME}/bin:$PATH:

image-20230926111826727

  • 使配置文件立即生效,source /etc/profile

java -version

查看是否安装成功

image-20230926111924400

4.将helloworld程序上传,并且启动

image-20230926112059371

chmod 777 helloWorld-0.0.1-SNAPSHOT.jar

nohup java -jar helloWorld-0.0.1-SNAPSHOT.jar 2>&1 &

image-20230926112221846

nohup命令 关闭当前session不会中断程序,可以通过kill等命令终止。

nohup java -jar helloWorld-0.0.1-SNAPSHOT.jar 2>&1 &

其中 2>&1是用来将标准错误2重定向到标准输出1中。1前面的&是为了让bash将1解释成标准输出而不是文件1。而最后一个&是为了让bash在后台执行。

image-20230926112642388

最终实现直接通过ip不加端口访问helloWorld程序

image-20230926114517433