Mac服務器搭建

  • Nginx

Nginx是一款輕量級的Web服務器、反向代理服務器,由於它的內存佔用少,啓動極快,高併發能力強,在互聯網項目中廣泛應用

  • Apache

阿帕奇這裏不做過多介紹,Mac自帶,詳情可去網上搜索


一、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是我安裝的版本號
  • 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即可。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章