---
# System prepended metadata

title: AWS Deploy Document

---

# AWS Deploy Document
- [連線資訊](＃連線資訊)
- [安裝方法](＃安裝方法)
- [資料庫建置](＃資料庫建置)
- [圖檔](#圖檔)


### 連線資訊
- 前台: http:/{IP}/mall
    - 使用者1
    帳號: test@test.com
    密碼: 123
    - 使用者2
    帳號:
    密碼:
- 後台: http:/{IP}/portal
    - 使用者1
    帳號: test01
    密碼: test01
    商店: ccilu
    - 使用者2
    帳號: test02
    密碼: test02
    商店: ccilu
### 安裝方法

1. 註冊AWS帳號，開啟MFA(帳號比較安全)
2. 開EC2 
    - 目前是使用Amazon Linux 2 AMI
    - instance為t2.micro
    - 會有一組金鑰需要存下來
3. elastic IP
    - 配置一個靜態 IPv4 地址
4. security group
    - 開啟port 80 (http)
    - 開啟port 27017 (mongodb)
5. 連線進EC2
    - 進到EC2 console，勾選機器後按連線
    ![](https://i.imgur.com/ARFpRYj.png)
    - 我的習慣是使用ssh 連線，可以跟著網頁上步驟執行
        i. 開啟電腦的terminal後，cd到存金鑰的資料夾
        ii. `chmod 400` 代表開讀取權限，第一次執行即可
        iii. 可以直接複製最後一行的指令連線 
        (`ssh -i "aws-key.pem" ec2-user@ec2-18-179-83-121.ap-northeast-1.compute.amazonaws.com`)

7. 安裝 node
    - [安裝教學](https://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)
    建議不要直接安裝最新版的node, 可能會有error
    `nvm install [版本號]` 比較好    
8. 安裝 mongo
    - [安裝教學](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-amazon/)
9. 安裝git
    - ```$ sudo yum install git```
10. 安裝pm2
    - 用來管理 server 上的 Node.js 服務
    - ```$ npm install pm2 -g```
11. clone server repo
    - ```git clone```
    - ```npm i ```
    - ```add .env```
    - ```pm2 start index ```(在背後執行index.js)
12. clone public repo
    - ```git clone```
    - 將電視牆影片放到public資料夾底下
    - ```npm i```
    - ```add .env```
    - ```npm build```
    - ```pm2 start npm --name "next-js" -- dev```   
13. clone portal repo
    - ```git clone```
    - ```npm i```
    - ```add .env```
    - ```npm build```
14. 安裝nginx
    - `sudo amazon-linux-extras install nginx1`

    - ```sudo vim /etc/nginx/conf.d/default.conf``` 
    1. 前後端掛同一台機器
      - ```
            server {
                listen 80;
                location / {
                    proxy_pass http://127.0.0.1:3000;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Host $host;
                    proxy_cache_bypass $http_upgrade;
                }
                location ^~ /admin {
                    alias /home/ec2-user/3D-shopping-mall-portal/;
                index index.html;
                }
                location /api/1.0 {
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://127.0.0.1:8080;
                    proxy_redirect off;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade";
                }
                location /peer {
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;
                        proxy_set_header X-NginX-Proxy true;
                        proxy_pass http://127.0.0.1:9000;
                        proxy_redirect off;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection "upgrade";
                        proxy_cache_bypass $http_upgrade;
                } 
                location ~* \.(png|jpeg) {
                        add_header Access-Control-Allow-Origin * ;
                        root /home/ec2-user/3D-Shopping-Mall-API-Sever/uploadFile/;
                        try_files /store/$uri /merchandise/$uri $uri 404;
                }
            }
       2. 前端獨立一台 ssl掛前端機台
       - ```
         server {
           listen 443 ssl;
           ssl_certificate    /etc/ssl/certificate.crt;
           ssl_certificate_key /etc/ssl/private.key;
           access_log   /var/log/nginx/nginx.vhost.access.log;
           error_log    /var/log/nginx/nginx.vhost.error.log;

           server_name gawesome.com.tw;

           location / {
             proxy_pass http://127.0.0.1:3000;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Host $host;
             proxy_cache_bypass $http_upgrade;
          }
           location ^~ /admin {
             alias /home/ec2-user/3D-shopping-mall-portal/;
             index index.html;
           }
         }
         
         server {
           listen 80 default_server;
           server_name gawesome.com.tw;
           return 301 https://$host$request_uri;
         }

    - start nginx  
        `sudo systemctl start nginx`
14. SSL
    1. 買網域 godaddy
    2. 申請ssl https://app.zerossl.com/dashboard
    3. godaddy綁ＩＰ
    4. EC2記得開443 security group
    5. nginx可能會遇到403的error
        https://blog.51cto.com/u_4048786/3205223
        嘗試成功的方法是將nginx用戶名改成root
        `sudo vim /etc/nginx/nginx.conf` 改為 nginx root


#### 資料庫建置
目前是使用mongo compass來操作雲端的資料庫
1. 下載並安裝mongo compass
https://www.mongodb.com/docs/compass/master/

2. 連線位置設為Elastic ip 位址
![](https://i.imgur.com/jzfZXX1.png)

3. 連線後手動import 資料進資料庫
資料放置於此[雲端連結]![Uploading file..._sn5e0p8y7]
(https://drive.google.com/drive/folders/15g8Kifg15hDiashRc9KC40FLiMn0lgpR?usp=share_link)，檔名對應各collection的名稱

#### 圖檔
目前已放置於S3
連線所需資料請依照後端專案的`.env template`中所列新增於`.env`中

S3架設請參考：
https://www.youtube.com/watch?v=eQAIojcArRY
CloudFront架設請參考：
https://www.youtube.com/watch?v=-DDGYzKtNwc



