Mac服务器搭建

作者: 强子ly | 来源:发表于2019-05-03 17:56 被阅读2次
    • Nginx
    • Apache

    一、Nginx

    • 1.1、安装(使用Homebrew)
    $ brew install nginx
    
    • 1.2、启动
    $ nginx
    
    • 1.3、验证是否成功
    http://localhost:8080
    
    安装成功
    • 1.4、添加json文件
    /usr/local/Cellar/nginx/1.15.12/html   // 配置路径,⚠️1.15.12是我安装的版本号
    
    添加前 添加置后 test.json内容
    • 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
    
    展示

    二、Apache

    $ sudo apachectl start                   // 启动
    
    $ sudo apachectl stop                    // 关闭
    
    $ sudo apachectl restart                 // 重启
    
    http://127.0.0.1  (或 http://localhost)  // 查看是否启动成功
    
    启动成功
    /Library/WebServer/Documents             // Web根目录路径
    

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

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

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

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

    相关文章

      网友评论

        本文标题:Mac服务器搭建

        本文链接:https://www.haomeiwen.com/subject/rlhznqtx.html