說起做一個阿里伺服器ECS配置文章也是因為買了一個伺服器好幾年了, 一直沒有好好玩過, 基本也就是自己放一些node專案和vue專案展示用, 今天初始化一下伺服器,重新配置一番,順便做個新手筆記給大家參考
伺服器配置
這是我自己的伺服器配置, 自己玩是足夠了

ECS雲伺服器配置及環境
- 登入阿里雲伺服器官網

- 左側導航 => 控制檯

- 進入雲伺服器ECS

- 進入例項
選擇你當時購買的伺服器機房
複製程式碼

- 管理
- 停止(關機)
- 才能切換系統
複製程式碼

- 返回例項,找到更多 => 磁碟和映象 => 更換系統盤

- 公共映象 (映象和我一樣哈, 不然會有一些稍微的不同)
centOS 7.2 64
複製程式碼
- 設定密碼
- 大家字母 + 小寫字母 + 數字
- 儘量取個簡單點,方便記
複製程式碼

- 系統啟動成功完成之後,所有配置都是在命令列中操作
putty軟體連線 ECS伺服器
軟體我上傳到了百度網盤開箱即用,體積不到1M, 當然也有其他的軟體請自行百度

- IP
你伺服器的公網IP
複製程式碼
- 埠
22
複製程式碼
- 連線型別
ssh
複製程式碼
- 登入
- 第一次會生成金鑰
- 自己個人電腦 是
- 公用電腦 否
複製程式碼
- putty命令列
- root
- 密碼 (切換系統的時候設定的密碼)
- 輸入的時候沒有提示,直接輸入
- 就能進入伺服器
複製程式碼

- 遠端連線(阿里雲官網提供的)
第一次會彈窗 讓你記錄下密碼,很重要,儲存到電腦上,會經常用到
複製程式碼

Node 環境搭建
windows系統的電腦搭建Node環境是很簡單的,伺服器也一樣
- 下載FileZilla
方便直接建立目錄上傳檔案等等
複製程式碼
- 登入站點


- 新建一個檔案來管理
我新建了一個tomato檔案專門來存放我配置的東西方便自己查詢操作
複製程式碼
- 開啟putty命令列
cd / 進入根目錄
ls 檢視目錄
cd tomato/node/ 進入我剛建立的tomato檔案下的node資料夾
wget https://nodejs.org/dist/v12.11.0/node-v12.11.0-linux-x64.tar.gz 下載node壓縮包
tar xvf node-v12.11.0-linux-x64.tar.gz 解壓node壓縮包
rm -rf node-v12.11.0-linux-x64.tar.gz 刪除node壓縮包
cd node-v12.11.0-linux-x64/bin ls檢視目錄,你會看到 node npm
複製程式碼
設定全域性(環境變數),在任何目錄都可能執行 node 和 npm 命令
ln -s /tomato/node/node-v12.11.0-linux-x64/bin/node /usr/local/bin/node
ln -s /tomato/node/node-v12.11.0-linux-x64/bin/npm /usr/local/bin/npm
複製程式碼
完成上述配置,即可在任何目錄執行node和npm命令了



利用FileZilla上傳專案
- 主機
- 公網ip
- 埠
- 22
- 協議
- ssh
- 登入型別
- 正常
- 使用者名稱
- root
- 密碼
- 你的密碼
複製程式碼

寫一個js,用node執行起來,然後通過公網ip的形式訪問, 這裡我寫了一個簡單的介面檔案
複製程式碼
http.js
const http = require('http')
const request = require('http').request
const fs = require('fs')
// 代理跨域
const fn = response => {
const options = {
host: "localhost",
port: 4001,
method: "get",
path: "/"
}
//執行就發起請求了
const req = request(options, res => {
// console.log(res);
let data = {}
res.setEncoding("utf8")
res.on("data", (chunk) => {
console.log(`響應主體:${chunk}`);
// 將data賦值為第三方代理請求回來的資料
data = chunk
})
res.on("end", (chunk) => {
response.write(data)
response.end()
})
})
// 監聽報錯
req.on("error", err => {
console.log(err);
})
req.write("")
req.end()
}
const server = http.createServer((request, response) => {
// 和請求相關的資訊都在 request 裡面
// 後臺給客戶端返回的資料, 都在 response 裡面
// CORS跨域 設定允許跨域
response.setHeader("Access-Control-Allow-Origin", "*")
// 設定響應頭, 第一個引數 http 狀態碼, 第二個引數是一個物件
// response.writeHead(2010, {
// "Content-Type": "text/pligin;charset=utf-8" //設定內容的型別
// })
// response.write()可以呼叫多次
// response.write()0向客戶端返回資料, 這裡返回的中文會亂碼,因此需要設定響應頭
// response.write("向客戶端返回資料~~~")
// response.write(request.url) // 請求路徑
// response.write(request.method) // 請求方式
// response.write(JSON.stringify(request.headers)) // 請求頭資訊
// response.end()結束響應, 不然會一直處於響應狀態不結束, 只能呼叫一次
// response.end("~~~我是response.end()返回的資料") // end也可以返回資料, 必須傳字串
if (request.method === "GET") {
response.writeHead(200, {
"Content-Type": "text/html;charset=utf-8"
})
switch (request.url) {
case "/":
// response.write(fs.readFileSync("../demo/index.html", "utf8")) // 同步執行
response.write(JSON.stringify({
a: 1,
b: 2
})) // 同步執行
response.end()
break;
case "/home":
//home
fs.readFile("../demo/index.html", "utf8", (err, data) => { // 非同步執行
response.write(data)
response.end()
})
break
case "/request": //代理跨域
fn(response)
break
default:
// 404
response.write("<h1>404頁面<h1>")
response.end()
break;
}
}
// response.end()
// 配置請求
})
// 監聽6300埠
server.listen(6300, () => {
console.log("服務監聽在6300埠");
})
複製程式碼


安全組開放訪問埠
- 進入例項
- 安全組
- 配置規則
- 新增安全規則
- 協議型別 TCP
- 80/80
- 6000/9999
- 443/443
- 協議型別 HTTP/HTTPS
- 80/80
- 授權型別:地址段訪問
- 授權物件
- 0.0.0.0/0
複製程式碼



域名繫結伺服器
- 在阿里雲購買域名


- 通過域名訪問剛剛的node專案

域名需要備案
只需要進入備案專區
按照流程操作, 官方有流程圖,就可以備案成功
備案成功的時間大概最快需要15天左右, 慢的話需要一個月
備案通過了,才能夠使用域名訪問虛擬主機的專案
複製程式碼
LAMP環境搭建及配置
linux + apache + mysql + php
時間較長
伺服器都可以用命令操作的
預設是沒有圖形介面的,但是可以自個百度 安裝圖形介面
centOS 圖形介面
複製程式碼
來自阿里雲官方論壇的文件按照這個文件配置就可以了
LAMP指Linux+Apache+MySQL/MariaDB+Perl/PHP/Python,是一組常用來搭建動態網站或者伺服器的開源軟體。它們本身都是各自獨立的程式,但是因為常被放在一起使用,擁有了越來越高的相容度,共同組成了一個強大的Web應用程式平臺。
常用命令
// ECS伺服器
cat /etc/redhat-release // 檢視系統版本
systemctl status firewalld // 命令檢視當前防火牆的狀態
systemctl stop firewalld // 臨時關閉防火牆,重啟Linux後,防火牆還會開啟
ystemctl disable firewalld // 永久關閉防火牆
// Apache
httpd -v // 檢視Apache的版本號
systemctl start httpd // 啟動Apache服務
systemctl restart httpd // 重啟Apache服務
systemctl enable httpd // 開機自啟動Apache服務
// MySQL
systemctl start mysql // 啟動MySQL服務
systemctl enable mysql // 開機自啟動MySQL服務
mysqladmin -u root password // 修改MySQL的root使用者密碼
mysql -uroot -p // 登入MySQL資料庫
\q // 退出MySQL
複製程式碼
阿里雲ECS伺服器優惠碼/優惠卷地址
