Mac中 Nginx安裝

一、前言

本文介紹一下,如何在Mac系統(tǒng)中安裝Nginx,把詳細(xì)過程記錄下來,方便以后查看,也方便大家學(xué)習(xí)。

二、正文

1、安裝 Homebrew

homebrew是什么?它是Mac中的一款軟件包管理工具,通過brew可以很方便的在Mac中安裝軟件或者是卸載軟件。不了解的同學(xué)看以看官網(wǎng)(brew.sh/index_zh-cn…), 然后在我們命令行中復(fù)制如下命令:

復(fù)制代碼

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
運(yùn)行,如下所示:

image.png

安裝成功后的話,我們可以使用命令 “brew update”更新下;如下命令:

image.png

有關(guān)brew常用的指令如下:

  1. brew搜索軟件命令: brew search nginx\
  2. brew安裝軟件命令: brew install nginx\
  3. brew卸載軟件命令: brew uninstall nginx\
  4. brew升級命令: sudo brew update\
  5. 查看安裝信息(比如查看安裝目錄等) sudo brew info nginx\
  6. 查看已經(jīng)安裝的軟件:brew list

2、brew安裝nginx

2.1、使用brew安裝nginx,如下命令所示:

復(fù)制代碼

brew install nginx

image.png

2.2、查看nginx的配置信息,如下命令:

brew info nginx

image.png

如上面的截圖,F(xiàn)rom:xxx 這樣的,是nginx的來源,Docroot默認(rèn)為 /usr/local/var/www, 在/usr/local/etc/nginx/nginx.conf 配置文件中默認(rèn)的端口為8080, 且nginx將在/usr/local/etc/nginx/servers 目錄中加載所有文件。并且我們可以通過最簡單的命令'nginx' 來啟動nginx.

2.3、查看nginx安裝目錄, 如下命令:

復(fù)制代碼

open /usr/local/etc/nginx/

image.png

打開nginx目錄后,可以看到我們上面的使用 brew info nginx 查看信息所說的 server目錄以及nginx.conf的配置文件,那么我們的nginx被安裝到什么地方呢?我們從上面的截圖可以看到,是在 這個目錄下 /usr/local/Cellar/nginx,執(zhí)行如下命令可以查看到:

open /usr/local/Cellar/nginx

image.png

進(jìn)入上面的 1.15.5文件后,如下圖所示:

image.png

在該目錄下可以看到一個名字為html的快捷方式的文件夾,進(jìn)入該目錄后,它有兩個文件50.html和index.html,如下圖所示:

image.png

其實(shí)它是指向的就是 /usr/local/var/wwww目錄的,為什么這么說,我們來看下進(jìn)入該命令后,查看下面有哪些文件就可以看到,如下圖:

image.png

3、啟動nginx服務(wù),如下命令:
brew services start nginx // 重啟的命令是: brew services restart nginx

重啟后,我們驗(yàn)證下,因?yàn)閚ginx默認(rèn)的端口號是8080,因此我們頁面訪問 http://localhost:8080 即可,看到如下信息:

image.png

如果成功的話,一般都是 歡迎的界面(index.html頁面我自己改過),下面我們繼續(xù)查看下nginx.conf 配置信息,使用如下命令:

cat /usr/local/etc/nginx/nginx.conf // 或者使用 sudo open /usr/local/etc/nginx/nginx.conf -a 'sublime text' 使用編輯器sublime打開。

user nginx;

worker_processes 1;

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       8080; 
    server_name  localhost; 

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html; 
        index  index.html index.htm; 
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
include servers/*;

如上,就可以使用nginx搭建本地服務(wù)了。

三、總結(jié)nginx常見的配置
nginx的配置文件路徑:/usr/local/etc/nginx/nginx.conf
nginx的服務(wù)器默認(rèn)路徑:/usr/local/var/www
nginx的安裝路徑:/usr/local/Cellar/nginx/1.15.5

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容