1.搭建服務器原因
前后端分離已成為互聯網項目開發的業界標准使用方式,通過nginx+tomcat的方式(也可以中間加一個nodejs)有效的進行解耦,並且前后端分離會為以后的大型分布式架構、彈性計算架構、微服務架構、多端化服務(多種客戶端,例如:瀏覽器,車載終端,安卓,IOS等等)打下堅實的基礎。這個步驟是系統架構從猿進化成人的必經之路。
核心思想是前端html頁面通過ajax調用后端的restuful api接口並使用json數據進行交互。
在互聯網架構中:
Web服務器:一般指像nginx,apache這類的服務器,他們一般只能解析靜態資源。
應用服務器:一般指像tomcat,jetty,resin這類的服務器可以解析動態資源也可以解析靜態資源,但解析靜態資源的能力沒有web服務器好。
一般都是只有web服務器才能被外網訪問,應用服務器只能內網訪問。
2.web服務器搭建幾種方式
2.1 使用http-server搭建(node.js-npm):需指定外網
安裝node.js指令:yum install -y nodejs
安裝http-server指令:npm install -g http-server
將http-server添加到全局變量:ln -s /usr/local/node/bin/http-server /usr/local/bin/http-server
在項目根目錄執行:http-server -a 192.168.211.129 -p 8000
退出(關閉)服務:Ctrl+C
2.2 使用static-server搭建(node.js-npm):無需指定外網
安裝static-server指令:npm install -g static-server
在項目目錄下指定該項目的入口文件:static-server -i index.html -p 8000
退出(關閉)服務:Ctrl+C
2.3 使用python搭建
安裝python3.6:
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0a1.tar.xz (獲取源碼壓縮包)
xz -d Python-3.6.0a1.tar.xz (解壓xz)
tar -xvf Python-3.6.0a1.tar (解壓tar)
cd Python-3.6.0a1
./configure --prefix=/usr/local/python (配置編譯)
make (編譯源碼)
make install (安裝)
啟動靜態web服務器(項目的根目錄為執行命令):python -m http.server 8000
退出(關閉)服務:Ctrl+C
注*****:python3以后,/usr/local/python/lib/python3.6/下BaseHTTPServer.py, SimpleHTTPServer.py, CGIHTTPServer.py沒有了,而是合閉到了 /usr/local/python/lib/python3.6/http/server.py文件里,因此不能使用命令[python -m SimpleHTTPServer 8000]啟動。
2.4 使用Ruby搭建
安裝Ruby:
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz (獲取壓縮源碼)
tar -zvxf ruby-2.4.1.tar.gz (解壓源碼)
cd ruby-2.4.1
./configure --prefix=/usr/local/ruby (配置編譯)
make (編譯)
make install (安裝)
啟動服務(項目根路徑下):ruby -run -e httpd . -p 8000
退出(關閉)服務:Ctrl+C
2.5 使用Nginx搭建*(推薦)
安裝依賴:
yum install gcc pcre-devel zlib zlib-devel openssl openssl-devel
安裝Nginx:
wget http://nginx.org/download/nginx-1.13.7.tar.gz (獲取壓縮源碼)
tar -zxvf nginx-1.13.7.tar.gz (解壓源碼)
cd nginx-1.13.7
./configure --prefix=/usr/local/nginx (配置編譯)
make (編譯)
make install (安裝)
將nginx添加到全局變量:ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
配置nginx.conf文件:
cd /usr/local/nginx/conf
vim nginx.conf
- user root # <--- #user nobody;
- ...
- server {
- listen 8000;
- server_name localhost;
- location / {
- root E:\Work\Workspace;
- index index.html index.htm;
- }
- 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
nginx服務指令:
啟動:nginx
重啟:nginx -s reload
停止:nginx -s stop
3.模擬json應用服務器搭建
3.1 使用json-server搭建(基於node--npm環境)
安裝json-server環境:npm install -g json-server
創建JSON數據文件:data.json (/home/當前用戶/)
添加數據:
- {
- "posts": [
- { "id": 1, "title": "json-server", "author": "typicode" }
- ],
- "comments": [
- { "id": 1, "body": "some comment", "postId": 1 },
- { "id": 2, "body": "some comment", "postId": 1 }
- ],
- "profile": { "name": "typicode" }
- }
啟動服務(data.json目錄下):
json-server --watch data.json --static ./LayUIDemo --port 8000 --host 192.168.211.129
json-server --watch data.json --port 8000 --host 192.168.211.129(推薦)
注:--watch:是否啟動監聽(默認啟動,布爾值)
--static:指定靜態文件路徑 (用於配置web服務器,一般不指定,web服務器使用nginx搭建)
--port:指定訪問端口號(默認3000)
--host:指定訪問ip地址(默認localhost)
其他指令使用:json-server --help 或者 json-server -h
退出(停止)服務:Ctrl+C