# outline
#### * 使用GCP 建立 Ubuntu 系統
#### * Ubuntu 安裝 Nginx
#### * Ubuntu 安裝 PostgreSQL(匯入DB)
#### * Ubuntu 部署 Springboot Jar 、 Angular dist
---
## * 使用GCP 建立 Ubuntu 系統
1. GCP / Compute Engine 建立一 VM
2. 上傳 key 至 GCP 上 (產生一組 SSH Key #TODO ) 至安全殼層金鑰
#### ssh 登入語法
ssh -i ~/.ssh/ssh-key name@xxx.xxx.xxx.xxx;
#### 遠端DB Mount 至 Local 127.0.0.1 語法
`
ssh -N -L 5434:127.0.0.1:5432 -i ~/.ssh/ssh-key name@xxx.xxx.xxx.xxx
`
#### SFTP
使用上建立 ssh key 登入 SFTP 登入型式選 "金鑰模式"
#### 更改系統時區
* 例如要將系統修改為台北時區 Asia/Taipei,請輸入
```
sudo timedatectl set-timezone Asia/Taipei
```
檢查時區設定
```
timedatectl
```
#### GCP 網址
https://console.cloud.google.com
---
## * Ubuntu 安裝 Nginx
指令如下
```
sudo apt update
sudo apt install nginx
sudo systemctl status nginx
```
---
## * Ubuntu 安裝 PostgreSQL(匯入DB)
#### 安裝指令
```
sudo apt update
sudo apt install postgresql postgresql-contrib
```
#### 建立Schema 及 設定 db user "postgres" 密碼 (可建立新db user 帳號 #TODO ) 指令如下:
```
sudo -i -u postgres
psql
ALTER USER postgres WITH PASSWORD '*****';
```
* 使用postgres及密碼由DBeaer 登入postgres DB
* 在建立 schema
* 匯入db sql
---
## Ubuntu 部署 Springboot Jar 、 Angular dist
* 安裝 JDK
sudo apt install openjdk-17-jdk-headless
#### 上傳 spring boot jar 及建置 Shell script(sh)File
* 建立專案目錄 及 service 目錄(放置spring boot jar)
sudo mkdir /luxup
* 更改目錄權限,方便系統執行及上傳檔案
```
sudo chmod 777 /luxup
```
* 使用sftp 上傳 spring boot jar 檔案, spring boot maven build 方式參閱最後一節
#### 建立 Linux System Service 執行 spring boot jar
* 建立service 檔案
```
sudo vi /etc/systemd/system/LuxupService.service
```
( vi 操作指令 : 按i key 進入編輯,輸入下指令, 按esc 再輸入 :wq! 儲存離開 )
* 檔案內容如下:
```
[Unit]
Description=LuxupService
[Service]
User=xxxxx
ExecStart=/usr/bin/java -jar /luxup/LuxupService/LuxupService.jar
ExitStatus=143
WorkingDirectory=/luxup/www
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
*User 需注意 可使用登入帳號
* 每次建立及更新 Service 需執行下列指令,進行生效
```
sudo systemctl daemon-reload
```
* 執行及停止查詢 Service 指令如下
```
sudo systemctl start LuxupService
sudo systemctl status LuxupService
sudo systemctl stop
```
tip 更換service port --server.port=8082
---
## 設定 Nginx 代理 Spring boot
* 修改 Nginx 設定檔
```
sudo vi /etc/nginx/nginx.conf
```
* 檔案內容如下:
```
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 4096;
# multi_accept on;
}
http {
upstream springboot
{
server localhost:8080 max_conns=100;
}
server
{
listen 80;
listen [::]:80;
server_name 35.194.245.17;
root /xxxxx/www;
location /XxxxxService/
{
proxy_pass http://localhost:8080;
}
}
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
```
#### 說明:
* 下行註解掉,原預設 root /var/www/html 可失效重設,可在這設定 root 目錄
#include /etc/nginx/sites-enabled/*;
* 設定 java spring boot jar 執行8080 port,direct 至80 port
```
location /XxxxxService/
{
proxy_pass http://localhost:8080;
}
```
* 注意有加/與沒有/ 是有分別. spring boot 專案需設定 contextFolder 的兩個方式
* (1)maven 設定: pom 加入 finalName
```
<build>
<finalName>XxxxxService</finalName>
```
* (2)maven 設定: pom 加入 finalName
```
application.properties 設定
server.servlet.context-path=/xxxxService
```
* Swagger servers的設定 @OpenAPIDefinition 設定:
```
@OpenAPIDefinition(
servers = {
@Server(url = "http://xxx.xxx.xxx.xxxx/xxxService", description = "Default Server URL")
}
)
```
* nginx reload 指令
```
nginx -s stop
sudo nginx -t
sudo systemctl reload nginx
```
---
## Angular build 及 Java spring boot build
* Angular程式 build
`
ng build --configuration production --base-href="/xxxx/"
`
整個目錄dist內目錄 sftp 至 location / {root 指定目錄內 (預設/var/www/html)
* inteliij build maven spring boot jar
開右方 Maven 視窗 ,找到 Lifecycle 裡的 package ,build完檔案產生於 target 目錄內 xxxx.jar , SFTP 上傳到 Server

Angular URL移除hash # , 造成Reload 404 問題, 在Nginx confic 上設定如下
```
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /myforms/ {
alias /myforms/www/landing/;
index index.html;
try_files $uri $uri/ /myforms/index.html;
}
```
參考網址 :
https://www.learninjava.com/angular-router-config-apache-nginx-tomcat/