Mac 搭建服务器

  • Nginx

Nginx是一款轻量级的Web服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用

  • Apache

阿帕奇这里不做过多介绍,Mac自带,详情可去网上搜索


一、Nginx

  • 1.1、安装(使用Homebrew)
$ brew install nginx

  • 1.2、启动
$ nginx

  • 1.3、验证是否成功
http://localhost:8080

image
  • 1.4、添加json文件
/usr/local/Cellar/nginx/1.15.12/html   // 配置路径,⚠️1.15.12是我安装的版本号

image
image
image
  • 1.5、配置Nginx
/usr/local/etc/nginx/nginx.conf.default   // nginx.conf.default路径

将以下代码替换到nginx.conf.default到里面,

server {        
    listen       8080;    
    server_name  localhost;         
    #access_log  logs/host.access.log  main; 
    location ~* {             
        add_header Content-Type "application/json";
        root   html;             
        if (!-f $request_filename) {                 
            rewrite ^/(.*)  /$1.json last;
        }             
        index  index.php index.html index.htm;
    }         
    error_page 405 =200 http://$host$request_uri;     
}

我用了一个叫 SubEthaEdit 的软件编辑的,在AppStore里面下载即可。

  • 1.6、展示
http://localhost:8080/test.json

image

二、Apache

$ sudo apachectl start                   // 启动

$ sudo apachectl stop                    // 关闭

$ sudo apachectl restart                 // 重启

http://127.0.0.1  (或 http://localhost)  // 查看是否启动成功

image
/Library/WebServer/Documents             // Web根目录路径

将指定文件放到Web根目录下即可直接访问,例:

http://localhost/test.json
http://本机IP/test.json

⚠️⚠️⚠️使用过后,记得关闭服务器,否则会一直消耗电脑内存

如果想让一个局域网内的其他人访问,直接把你的ip地址替换localhost即可

你可能感兴趣的:(Mac 搭建服务器)