這篇要介紹在 OpenSUSE 上安裝Nginx、Java、Tomcat,並將前端畫面放置在Nginx,後端放置在Tomcat中,當中碰到小卡關的地方
安裝軟體時,會有儲存庫缺失問題,需要加入儲存庫
Tomcat因為Java安裝路徑不對,無法啟動
同樣的安裝指令安裝起來的Java版本不同
Nginx如何設置才讀到到畫面
前端畫面call後端發生CORS問題
安裝軟體時,會有儲存庫缺失問題,需要加入儲存庫 SUSE 有專用安裝軟體的套件,zypper & Yast,這裡只使用zypper,zypper安裝指令為 (以Java為例) zypper install java-1_8_0-openjdk
在安裝前可以先查詢,指令為 zypper search java,搜尋後發現沒有java,這是因為我們的儲存庫 repository 中沒有支援java,我們只要加入即可
1 zypper addrepo https://download.opensuse.org/repositories/Java:Factory/openSUSE_Leap_15.1/Java:Factory.repo
java repository
此時使用 zypper search java ,就可以看到java版本供選擇,加入儲存庫後要安裝時又發生一個問題,說有一個giflib套件沒有加入,再加入即可
1 zypper addrepo https://download.opensuse.org/repositories/openSUSE:Leap:15.1/standard/openSUSE:Leap:15.1.repo
giflib repository
1 zypper install java-1_8_0-openjdk
Tomcat因為Java安裝路徑不對,無法啟動
tomcat(source code install, download from http://ftp.mirror.tw/pub/apache/tomcat/tomcat-9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz )
1 sudo wget http://ftp.mirror.tw/pub/apache/tomcat/tomcat-9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz
1 sudo tar xvf /opt/apache-tomcat-9* -C /opt/tomcat --strip-components=1
1 sudo useradd -M -s /bin/nologin -d /opt/tomcat tomcat
1 sudo mkdir /opt/tomcat/Log
1 sudo chown -R tomcat:users /opt/tomcat
1 vi /etc/systemd/system/tomcat.service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib64/jvm/jre-1.8.0-openjdk/bin Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx8192M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom -Dfile.encoding=UTF8' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh User=tomcat Group=users RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
設定開機啟動
重讀service腳
啟動tomcat
確認tomcat
同樣的安裝指令安裝起來的Java版本不同 去客戶那邊安裝時,測試環境我安裝起來是1.8.0_252,客戶自行安裝的java版本卻變成1.8.0_202,猜測可能是OpenSuse跟Suse的repository不一樣,故採用直接手動安裝的方式
使用AdoptOpenJDK
選擇欲安裝的版本與系統,把.tar.gz檔案載下來
scp 方式丟上遠端server
cp 到預安裝的路徑,並解壓縮
1 $ sudo tar xvf /usr/lib64/java/OpenJDK8U-jre_x64_linux_hotspot_8u252b09.tar.gz -C /usr/lib64/java --strip-components=1
設定環境變數
1 2 3 4 5 $ sudo vim /etc/profile export JAVA_HOME=/usr/lib64/javaexport PATH="$JAVA_HOME /bin:$PATH "
6.reload /etc/profile
檢查java版本
1 2 3 4 $ java -version openjdk version "1.8.0_252" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.252-b09, mixed mode)
將Tomcat中的java路徑更改後暫停再重啟,確認CGroup的啟動路徑已經改為設定的環境變數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ sudo vim /etc/systemd/system/tomcat.service $ sudo systemctl enable tomcat $ sudo systemctl daemon-reload $ sudo systemctl start tomcat $ sudo systemctl status tomcat tomcat.service - Apache Tomcat Web Application Container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: active (running) since 三 2020-06-17 13:58:34 CST; 23min ago Process: 14578 ExecStop=/opt/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS) Process: 14628 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 14635 (java) Tasks: 31 (limit : 512) CGroup: /system.slice/tomcat.service └─14635 /usr/lib64/java/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Dja... 6月 17 13:58:34 linux-g5k0 systemd[1]: Starting Apache Tomcat Web Application Container... 6月 17 13:58:34 linux-g5k0 startup.sh[14628]: Tomcat started. 6月 17 13:58:34 linux-g5k0 systemd[1]: Started Apache Tomcat Web Application Container.
Nginx如何設置才讀到到畫面 加入nginx repository
1 sudo zypper addrepo -G -t yum -c 'http://nginx.org/packages/sles/12' nginx
更新 repository
安裝 nginx
編輯default.conf檔
1 2 3 4 5 cd /etc/nginx/conf.d/cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf_bk vi /etc/nginx/conf.d/default.conf
1 2 3 4 5 6 7 server { location /yourPath { root /usr/share/nginx; index index.html index.htm; try_files $uri $uri / /yourPath/index.html; } }
前端畫面call後端發生CORS問題 call後端一直call不到,browser一直顯示
1 Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response
而且看了NetWork的地方同一request竟然call了兩次,一次是以http Method Option去call,一次是以原本的post去call,但是請求失敗,上網了解後原來是因為某些條件觸發了預檢請求(Option),先確認說server可以接受什麼樣的request,所以去檢查是不是有沒有對起來的地方,發現我server接受的Access-Control-Request-Headers: 沒有authorization,於是我在後端程式加入authorization就好了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Access-Control-Allow-Headers: X-requested-with, Content-Type Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS, DELETE Access-Control-Allow-Origin: * Access-Control-Max-Age: 3600 Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH Connection: keep-alive Content-Length: 0 Date: Fri, 12 Jun 2020 02:37:43 GMT Keep-Alive: timeout=20 Request Headers view source Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-TW, zh; q=0.9,en-US; q=0.8,en; q=0.7, zh-CN; q=0.6 Access-Control-Request-Headers: authorization, content-type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class CORSFilter implements Filter { public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; HttpServletRequest request = (HttpServletRequest) req; request.setCharacterEncoding("UTF-8" ); response.setHeader("Access-Control-Allow-Origin" , "*" ); response.setHeader("Access-Control-Allow-Methods" , "POST, GET, PUT, OPTIONS, DELETE" ); response.setHeader("Access-Control-Max-Age" , "3600" ); response.setHeader("Access-Control-Allow-Headers" , "X-requested-with, Content-Type, authorization" ); chain.doFilter(req, res); } public void init (FilterConfig filterConfig) {} public void destroy () {} }
跨來源資源共用(CORS)