Mac安装Lnmp环境

Nginx

  • 安装

    1
    brew install nginx --with-http_geoip_module
  • 启动与关闭

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #测试配置是否有语法错误
    nginx -t
    #打开 nginx
    sudo nginx
    #重新加载配置|重启|停止|退出 nginx
    nginx -s reload|reopen|stop|quit
    #也可以使用Mac的launchctl来启动|停止
    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
  • 开机启动

    1
    2
    ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
  • 监听80端口

    1
    2
    sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
    sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_1/bin/nginx

Redis

  • 安装

    1
    brew install redis
  • 启动redis服务

    1
    redis-server
  • 开机启动

    1
    2
    3
    ln -sfv /usr/local/Cellar/redis/3.0.6/homebrew.mxcl.redis.plist ~/Library/LaunchAgents

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Mysql

  • 安装

    1
    brew install mysql
  • 开机启动

    1
    2
    3
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  • 查看mysql运行情况

    1
    ps aux | grep mysql
  • 连接mysql,输入root密码,依次确认首次登入的选项,初始化:

    1
    mysql -uroot -p

PHP

  • 添加brew的PHP扩展库:

    1
    2
    brew tap homebrew/dupes
    brew tap josegonzalez/homebrew-php
  • 安装

    1
    brew install php70
  • 由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本。根据你的shell类型修改.bashrc或.zshrc

    1
    export PATH=$(brew --prefix php70)/bin:$(brew --prefix php70)/sbin:$PATH
  • 查看看PHP和PHP-FPM的版本是否正确

    1
    2
    php -v
    php-fpm - v
  • 修改php-fpm配置文件

    1
    vim /usr/local/etc/php/7.0/php-fpm.conf
  • 把大概第16行的配置修改成下面这样:

    1
    pid=run/php-fpm.pid
  • 命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #测试php-fpm配置
    php-fpm -t
    php-fpm -c /usr/local/etc/php/7.0/php.ini -y /usr/local/etc/php/7.0/php-fpm.conf -t

    #启动php-fpm
    php-fpm -D
    php-fpm -c /usr/local/etc/php/7.0/php.ini -y /usr/local/etc/php/7.0/php-fpm.conf -D

    #关闭php-fpm
    kill -INT `cat /usr/local/var/run/php-fpm.pid`

    #重启php-fpm
    kill -USR2 `cat /usr/local/var/run/php-fpm.pid`
  • 启动php-fpm之后,确保它正常运行监听9000端口:

    1
    2
    3
    4
    5
    6
    7
    lsof -Pni4 | grep LISTEN | grep php

    php-fpm 40398 Gavin 6u IPv4 0x1ffa29bae0d9e20b 0t0 TCP 127.0.0.1:9000 (LISTEN)

    php-fpm 40403 Gavin 0u IPv4 0x1ffa29bae0d9e20b 0t0 TCP 127.0.0.1:9000 (LISTEN)

    php-fpm 40404 Gavin 0u IPv4 0x1ffa29bae0d9e20b 0t0 TCP 127.0.0.1:9000 (LISTEN)
  • PHP-FPM开机启动

    1
    2
    ln -sfv /usr/local/opt/php70/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
  • 安装扩展

1
2
# 使用 pecl 安装
/usr/local/Cellar/php/7.2.5/bin/pecl install redis

设置快捷服务控制命令

  • 为了方便管理,把常用的命令设置成别名.写入家目录下的.bashrc或.zshrc中
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
    alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
    alias nginx.restart='nginx.stop && nginx.start'
    alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
    alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
    alias php-fpm.restart='php-fpm.stop && php-fpm.start'
    alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
    alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
    alias mysql.restart='mysql.stop && mysql.start'
    alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
    alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
    alias redis.restart='redis.stop && redis.start'
    alias memcached.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
    alias memcached.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
    alias memcached.restart='memcached.stop && memcached.start'
0%