V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Livid
V2EX  ›  PHP

在 Mac OS X 上安装 NGINX 1.10+ 和 PHP-FPM 5.6

  •  3
     
  •   Livid · 2016-06-10 09:03:20 +08:00 · 6876 次点击
    这是一个创建于 2879 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Mac OS X 的早期版本里其实自带了配置非常完备的 Apache ,所以那个时候如果要在 Mac OS X 上使用 phpMyAdmin ,只要打开 Personal Web Sharing 然后将 phpMyAdmin 安装到一个目录中即可。

    但是后来这个组件被去掉了。在最新的 OS X El Capitan 中,你需要安装 Server.app 来获得 Apache + PHP 。但 Server.app 是一个巨大的软件包,带有太多组件,安装之后会影响 OS X 的关机和重启速度。所以为了能够继续通过轻量级的方式用 phpMyAdmin ,研究了下面的这个步骤。

    请确定你机器上已经装好 Xcode , Command Line Utility 及 Homebrew 。

    NGINX

    首先通过 Homebrew 安装 NGINX :

    brew install --with-http2 nginx
    

    Homebrew 中的 NGINX 默认会在 8080 端口启动,这不是很方便,所以我们首先把这个地方改掉:

    vi /usr/local/etc/nginx/nginx.conf
    

    然后将第 36 行附近的 listen 8080 修改为 listen 80

    因为我们将端口修改到了需要 root 特权的 80 ,所以无法再通过 brew services 来使得 NGINX 自动启动。你需要执行以下步骤将 NGINX 的 launchd 配置文件复制到系统目录并更改所有者:

    sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
    sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
    

    然后通过 launchctl 加载:

    sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
    

    如果一切顺利的话,那么此时 NGINX 就已经成功在 80 端口启动了:

    $ curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.10.1
    Date: Fri, 10 Jun 2016 00:59:17 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 31 May 2016 13:47:07 GMT
    Connection: keep-alive
    ETag: "574d95db-264"
    Accept-Ranges: bytes
    

    然后我们继续安装 PHP 。

    PHP-FPM

    因为 Homebrew 没有自带 PHP 的安装包,所以你需要先导入一个外部的仓库地址:

    brew tap homebrew/php
    

    然后安装带有 FPM 和 MySQL 扩展的 PHP 5.6 版本:

    brew install --without-apache --with-fpm --with-mysql php56
    

    因为 PHP-FPM 将运行在 9000 端口,所以无需特殊设置,直接使用 Homebrew 设置自动启动:

    brew services start homebrew/php/php56
    

    在 NGINX 中启用 PHP-FPM

    默认情况下,所有的 NGINX 虚拟主机配置会从 /usr/local/etc/nginx/servers 这个位置读取,所以假设你把 phpMyAdmin 安装在 /www/tools 下的话,那么这里是一个示例配置文件:

    server {
        listen 80;
    
        server_name tools;
    
        root /www/tools;
    
        location / {
            index index.php index.html;
        }
    
        location ~ \.php$ {
            try_files      $uri = 404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    然后只要在 /etc/hoststools 这个主机名指向 127.0.0.1 即可通过 http://tools/ 来访问你放在 /www/tools 中的所有 PHP 工具。

    16 条回复    2016-07-18 18:14:37 +08:00
    leakeung
        1
    leakeung  
       2016-06-10 09:06:25 +08:00
    曾经我也是直接在 mac 上直接部署环境..回来.我放弃了.直接使用 mamp 很爽
    caizixian
        2
    caizixian  
       2016-06-10 09:23:05 +08:00
    Syc
        3
    Syc  
       2016-06-10 10:08:35 +08:00 via Android
    不如拿手机做环境,做个目录同步
    iamwb
        4
    iamwb  
       2016-06-10 10:19:10 +08:00 via Android
    Brew 里面的 Nginx ,能不能让我看看编译了哪些模块?
    echopan
        5
    echopan  
       2016-06-10 10:21:52 +08:00
    MAMP Pro 用起来爽
    Livid
        6
    Livid  
    MOD
    OP
       2016-06-10 10:48:13 +08:00
    @iamwb 是一个非常精简的版本:

    $ nginx -V
    nginx version: nginx/1.10.1
    built by clang 7.3.0 (clang-703.0.29)
    built with OpenSSL 1.0.2h 3 May 2016
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/Cellar/nginx/1.10.1 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.10.1/bin/nginx --with-cc-opt='-I/usr/local/Cellar/pcre/8.38/include -I/usr/local/Cellar/openssl/1.0.2h_1/include' --with-ld-opt='-L/usr/local/Cellar/pcre/8.38/lib -L/usr/local/Cellar/openssl/1.0.2h_1/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module --with-http_gunzip_module --with-http_v2_module

    更多的模块你可以用 brew search nginx 看到。
    Livid
        7
    Livid  
    MOD
    OP
       2016-06-10 10:49:13 +08:00
    目前用 brew search nginx 看到的可以安装的模块列表:

    $ brew search nginx
    nginx ✔
    homebrew/nginx/accept-language-nginx-module
    homebrew/nginx/accesskey-nginx-module
    homebrew/nginx/ajp-nginx-module
    homebrew/nginx/anti-ddos-nginx-module
    homebrew/nginx/array-var-nginx-module
    homebrew/nginx/auth-digest-nginx-module
    homebrew/nginx/auth-ldap-nginx-module
    homebrew/nginx/auth-pam-nginx-module
    homebrew/nginx/auto-keepalive-nginx-module
    homebrew/nginx/autols-nginx-module
    homebrew/nginx/cache-purge-nginx-module
    homebrew/nginx/captcha-nginx-module
    homebrew/nginx/counter-zone-nginx-module
    homebrew/nginx/ctpp2-nginx-module
    homebrew/nginx/dav-ext-nginx-module
    homebrew/nginx/dosdetector-nginx-module
    homebrew/nginx/echo-nginx-module
    homebrew/nginx/eval-nginx-module
    homebrew/nginx/extended-status-nginx-module
    homebrew/nginx/fancyindex-nginx-module
    homebrew/nginx/geoip2-nginx-module
    homebrew/nginx/headers-more-nginx-module
    homebrew/nginx/healthcheck-nginx-module
    homebrew/nginx/http-accounting-nginx-module
    homebrew/nginx/http-flood-detector-nginx-module
    homebrew/nginx/http-remote-passwd-nginx-module
    homebrew/nginx/log-if-nginx-module
    homebrew/nginx/lua-nginx-module
    homebrew/nginx/mod-zip-nginx-module
    homebrew/nginx/mogilefs-nginx-module
    homebrew/nginx/mp4-h264-nginx-module
    homebrew/nginx/mruby-nginx-module
    homebrew/nginx/naxsi-nginx-module
    homebrew/nginx/nchan-nginx-module
    homebrew/nginx/nginx-full
    homebrew/nginx/notice-nginx-module
    homebrew/nginx/php-session-nginx-module
    homebrew/nginx/push-stream-nginx-module
    homebrew/nginx/realtime-req-nginx-module
    homebrew/nginx/redis-nginx-module
    homebrew/nginx/redis2-nginx-module
    homebrew/nginx/rtmp-nginx-module
    homebrew/nginx/set-misc-nginx-module
    homebrew/nginx/small-light-nginx-module
    homebrew/nginx/stream-lua-nginx-module
    homebrew/nginx/subs-filter-nginx-module
    homebrew/nginx/tcp-proxy-nginx-module
    homebrew/nginx/txid-nginx-module
    homebrew/nginx/unzip-nginx-module
    homebrew/nginx/upload-nginx-module
    homebrew/nginx/upload-progress-nginx-module
    homebrew/nginx/upstream-order-nginx-module
    homebrew/nginx/ustats-nginx-module
    homebrew/nginx/var-req-speed-nginx-module
    homebrew/nginx/vod-nginx-module
    homebrew/nginx/websockify-nginx-module
    homebrew/nginx/xsltproc-nginx-module
    tanteng
        8
    tanteng  
       2016-06-10 10:52:07 +08:00
    顶!
    不过如果是 PHP 开发,还是建议用 vagrant+VirtualBox 装虚拟机
    SharkIng
        9
    SharkIng  
       2016-06-10 10:54:53 +08:00
    其实用 Docker 更好,或者 Vagrant
    shuimugan
        10
    shuimugan  
       2016-06-10 11:00:37 +08:00   ❤️ 2
    纯粹为了使用 phpMyAdmin 的话,装好 php 之后 php -S 127.0.0.1:8888 -t webroot 就可以了, php 自带一个轻量级 web server ,在 win 下, i5 4200u 简单压测 qps800+
    precisi0nux
        11
    precisi0nux  
       2016-06-10 11:37:04 +08:00 via iPhone
    为什么不用 DataGrip ,哪怕是 Sequal Pro ?
    Nexvar
        12
    Nexvar  
       2016-06-10 14:36:46 +08:00 via Android
    直接上 docker,开发部署都很方便
    开源的解决方案:laradock
    alang
        13
    alang  
       2016-06-21 10:35:33 +08:00
    楼上的各位,想掌握一门技术,还是知其所以然,自己动手配置一遍比较好。

    否则,都是别人配置好的东西拿过来用,可以节省时间,但是学不到技术。出了问题束手就擒。
    JJaicmkmy
        14
    JJaicmkmy  
       2016-06-23 22:39:04 +08:00
    为什么我用 brew 安装的 Nginx 是 1.8.1 ?
    JJaicmkmy
        15
    JJaicmkmy  
       2016-06-23 23:03:39 +08:00
    好吧,升级 homebrew 后解决。
    cd2want
        16
    cd2want  
       2016-07-18 18:14:37 +08:00
    @leakeung 表示我也是这么干的.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1067 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:35 · PVG 06:35 · LAX 15:35 · JFK 18:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.