天天看點

nodejs伺服器部署教程一

第一篇教程緊緊讓你輸出一個hello world

環境介紹

伺服器環境:ubuntu(16.04)64位

本地環境:windows10 64位

連接配接工具:mobaxterm

ubuntu安裝和基本配置

我的ecs是在阿裡雲買的,購買的時候鏡像選擇ubuntu16.04,現在在搞活動比較便宜,我買的香港地區的不用備案,購買後本地打開mobaxterm,點選session,輸入ip确定,輸入root,然後輸入密碼,會看到下面的界面:

連接配接遠端伺服器,接下來我參考了阮一峰老師的這篇文章

addgroup wmui

添加使用者組

useradd -d /home/wmui -s /bin/bash -m wmui

建立wmui使用者

passwd wmui

設定密碼,如果忘記密碼,也可用此指令重置密碼

usermod -a -G wmui wmui

 添加使用者到組

visudo 

設定sudo權限

然後會跳轉到下面頁面

root ALL=(ALL:ALL) ALL

下面添加

wmui ALL=(ALL) NOPASSWD: ALL

ctrl+x

儲存退出

接下來打開一個新的視窗,測試是否登陸成功

ssh無密碼登陸配置

首先你需要在本地安裝git并生成

id_rsa.pub

,打開指令行

在本地生成公鑰和私鑰:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

在伺服器生成公鑰和私鑰:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

在伺服器視窗輸入:

echo "[your public key]" > ~/.ssh/authorized_keys

将本機的公鑰拷貝到伺服器的authorized_keys檔案

完成以上操作,測試是否生效,重新開機服務:

sudo service ssh restart

新打開一個視窗,輸入使用者名回車,登陸成功

安全項配置:

我在搭建時候沒有設定這一項,是以沒有測試這項

編輯SSH配置檔案/etc/ssh/sshd_config:修改port為1025到65536之間的任意一個整數

在末尾添加: AllowUsers [username]

此時登陸時需要端口号: -p [25000] [username]

fail2ban系統監控軟體安裝:

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install fail2ban

sudo service fail2ban status 檢視fail2ban運作狀态

sudo service fail2ban stop 關閉fail2ban

sudo service fail2ban start 開啟fail2ban

nodejs環境搭建

安裝常用軟體

sudo apt-get install vim openssl build-essential libssl-dev wget curl git

nvm安裝

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

打開新的視窗

nvm install v8.9.1

nvm use 8.9.1

nvm alias default 8.9.1

 預設版本

安裝常用node包

npm i pm2 webpack vue-cli -g

nginx伺服器代理設定

sudo apt-get install nginx

 通過nginx -v檢視版本号

打開/etc/nginx/conf.d/檔案夾,建立配置檔案test-8081.conf,内容如下:

upstream hello {
    server 127.0.0.1:8081;
}

server {
    listen 80;
    server_name hello.86886.wang;

    location / {
        proxy_set_header Host  $http_host;
        proxy_set_header X-Real-IP  $remote_addr;  
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Nginx-proxy true;
        proxy_pass http://hello;
        proxy_redirect off;
    }
}           

解析你的域名到你的伺服器ip,例如解析hello.86886.wang

sudo nginx -t

 檢視是否配置成功

sudo nginx -s reload

 重新開機伺服器

注意:我在第一次配置的時候遇到了黃色警告,但是不影響使用,如果你也遇到了,向下面一樣解決

打來etc/hosts,在

127.0.0.1 localhost

127.0.1.1 iZj6cas9txr6crspqecn4zZ

其中 iZj6cas9txr6crspqecn4zZ是你的ecs執行個體名稱

ok完成以上操作,接下來開始寫hello world

建立和部署hello world

以root使用者身份在根目錄下建立www目錄,www目錄下建立hello檔案夾,裡面就一個檔案,hello.js,内容如下:

const http = require('http')
http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/plain'})
res.end('hello world')
}).listen(8081)

console.log('server test')           

進入到www下hello檔案夾下

hello world測試:

pm2 start hello.js

pm2 list 檢視啟動的應用

pm2 show hello 檢視詳細資訊

pm2 logs 檢視目前資訊

pm2 stop hello 停止hello

pm2 delete hello 删除hello

如圖所示表示啟動成功,輸入hello.86886.wang就可以看到hello world了

接下來計劃:

nodejs伺服器部署教程二:部署一個基于vue的項目到線上

nodejs伺服器部署教程三:部署基于nodejs+vue+mongodb的項目

nodejs伺服器部署教程四:實作https

本文摘自:https://segmentfault.com/a/1190000010098126