# 在 AWS 建立 EC2 ## 申請 AWS 學生帳號 首先,先到AWS網站申請學生帳號 https://aws.amazon.com/tw/education/awseducate/ 請是用學校的電子郵件帳號申請,在電子郵件驗證確認後, 即可登入學生帳號. 因為 Infrastructure as a service 是採使用者付費的機制. 因此,請記得每次使用完畢後, 請記得 shutdown 或 delete 不用的 Instance. ## 登入 AWS 學生帳號 步驟一 Login 到 AWS 教育帳號 To login AWS educate account: https://aws.amazon.com/tw/education/awseducate/ 步驟二 建立 Linux 虛擬機器 Steps for creating an EC2 instance (啟動 Linux 虛擬機器) 記得要建立新的金鑰, key pair, 並把 .pem 保管好. Windows 使用者: 建議將您的金鑰對儲存在 user 目錄中名為 .ssh 的子目錄中 (例如,C:\user\{yourusername}\.ssh\MyKeyPair.pem). 步驟三 連線到 Linux 虛擬機器 方法一 使用 Windows PowerShell ``` PS C:\Users\Rebecca> cd .ssh PS C:\Users\Rebecca\.ssh> ssh -i 'C:\Users\Rebecca\.ssh\myKeyPair.pem' ubuntu@3.89.140.238 ``` 方法二 是用 PuTTY ``` step 1: 先將 .pem 轉檔為 .ppk https://aws.amazon.com/tw/premiumsupport/knowledge-center/convert-pem-file-into-ppk/ Step 2: Connecting your EC2 instance from Windows Using PuTTY https://docs.aws.amazon.com/en_us/AWSEC2/latest/UserGuide/putty.html ``` ## 安裝 nodeJS: 快速打造一個 hello World 的程式 步驟一 安裝 nodeJS ``` Step 1: $ sudo apt update Step 2: $ sudo apt install nodejs Step 3: $ nodejs --version v8.10.0 Step 4: $ sudo apt install npm Step 5: $ npm --version 3.5.2 ``` 步驟二 寫 hello world 程式 step 1: ``` $ npm init $ npm install express --save ``` step 2: ```javascript= var port = process.env.VCAP_APP_PORT ||3000; var express = require('express'); var app = express(); app.get('/', function(req, res) { res.send('Hello Express!'); }); app.listen(port, function(req, res){ console.log('Server started on port ' + port); }); ``` 步驟三 打開防火牆 Step 1: 到 AWS EC2 管理平台 Step 2: 打開 "Security Groups" 頁面. 選擇 instance, 在 "inbound" 頁箋新增 port 3000