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
来源: http://www.bubuko.com/infodetail-2770949.html