brew

brew 是 Mac 上安装软件的利器,与centos上yum命令一样,简单明了(更多介绍参见这篇文章)。本篇文章使用brew傻瓜式的搭建lnmp/lamp本地开发环境。

apache

apache web服务器其实内置在Mac 系统中,只需要简单的命令就能调用开启

开启apache

sudo apachectl start
sudo apachectl restart
sudo apachectl stop

此时你访问http://localhost/~user_name(user_name换为你的用户名)就可以访问了。
接下来我们做简单的修改,这样就能直接通过http://localhost访问,是不是很棒棒呢。

修改/etc/apache2/httpd.conf内容

DocumentRoot /Users/username/Sites
<Directory /Users/username/Sites>
    Options Indexes MultiViews
    # apache 2.2
    # AllowOverride All
    # Order allow,deny
    # Allow from 127.0.0.1

    # apache 2.4
    Require local
</Directory>

Nginx

使用brew安装

brew install nginx

nginx的命令

#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
#测试配置是否有语法错误
nginx -t

配置nginx

cd /usr/local/etc/nginx/
mkdir conf.d

修改配置文件

vi nginx.conf

改为如下配置


error_log       /usr/local/var/log/nginx/error.log warn;

pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  256;
}

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      /usr/local/var/log/nginx/access.log main;
    port_in_redirect off;
    sendfile        on; 
    keepalive_timeout  65; 

    include /usr/local/etc/nginx/conf.d/*.conf;
}

增加自定义文件

vi ./conf.d/default.conf

添加监听端口

    listen       80;
    server_name  localhost;

    root /Users/username/Sites/; # 该项要修改为你准备存放相关网页的路径

    location / { 
        index index.php;
        autoindex on; 
    }   

    #proxy the php scripts to php-fpm  
    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_intercept_errors on; 
        fastcgi_pass   127.0.0.1:9000; 
    }   

}

开启php-fpm

sudo nginx
sudo php-fpm -D

mysql

mysql安装

unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
sudo chown -R your_user /usr/local/var/mysql/

提示如下,默认无密码,你可以自己设置密码

    mysql_secure_installation

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

mysql常用命令

mysql -h localhost -uroot -p
#导出数据库
mysqldump -uroot -p db > db.sql
#导入数据库
mysql -uroot -p db < db.sql
// or
mysql -uroot -p db -e "source /path/to/db.sql"
#开启远程登录

grant all privileges on ss.* to 'root'@'%' indentified by 'passoword' with grant option;
// or 
update user set Host="%" and User="root"
// 注意%是不包含localhost的
flush privileges;
#创建用户
CREATE USER 'test'@'localhost' IDENTIFIED BY 'password';
grant all privileges on *.* to test@'localhost' identified by 'test';
#创建表
CREATE SCHEMA testdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
#赋予数据库权限
GRANT ALL ON testdb.* TO 'test'@'localhost';

PHPMyAdmin

著名的mysql可视化管理应用
1. 直接在官网下载最新版
2. 解压到~/Sites/phpmyadmin下
3. 打开http://localhost/phpmyadmin/setup,安装一个服务

参考文章

http://yansu.org/2013/12/11/lamp-in-mac.html