HTTP静态资源服务器搭建

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

猜你喜欢

转载自www.cnblogs.com/pascall/p/9661627.html