\r\n

brew

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

apache

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

开启apache

\r\n

sudo apachectl start\r\nsudo apachectl restart\r\nsudo apachectl stop\r\n

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

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

\r\n

DocumentRoot /Users/username/Sites\r\n<Directory /Users/username/Sites>\r\n    Options Indexes MultiViews\r\n    # apache 2.2\r\n    # AllowOverride All\r\n    # Order allow,deny\r\n    # Allow from 127.0.0.1\r\n\r\n    # apache 2.4\r\n    Require local\r\n</Directory>\r\n

\r\n

Nginx

\r\n

使用brew安装

\r\nbrew install nginx\r\n

nginx的命令

\r\n

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

\r\n

配置nginx

\r\n

cd /usr/local/etc/nginx/\r\nmkdir conf.d\r\n

\r\n

修改配置文件

\r\nvi nginx.conf\r\n

改为如下配置

\r\n

\r\nerror_log       /usr/local/var/log/nginx/error.log warn;\r\n\r\npid        /usr/local/var/run/nginx.pid;\r\n\r\nevents {\r\n    worker_connections  256;\r\n}\r\n\r\nhttp {\r\n    include       mime.types;\r\n    default_type  application/octet-stream;\r\n\r\n    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '\r\n                      '$status $body_bytes_sent "$http_referer" '\r\n                      '"$http_user_agent" "$http_x_forwarded_for"';\r\n\r\n    access_log      /usr/local/var/log/nginx/access.log main;\r\n    port_in_redirect off;\r\n    sendfile        on; \r\n    keepalive_timeout  65; \r\n\r\n    include /usr/local/etc/nginx/conf.d/*.conf;\r\n}\r\n

\r\n

增加自定义文件

\r\nvi ./conf.d/default.conf\r\n

添加监听端口

\r\n

    listen       80;\r\n    server_name  localhost;\r\n\r\n    root /Users/username/Sites/; # 该项要修改为你准备存放相关网页的路径\r\n\r\n    location / { \r\n        index index.php;\r\n        autoindex on; \r\n    }   \r\n\r\n    #proxy the php scripts to php-fpm  \r\n    location ~ \.php$ {\r\n        include /usr/local/etc/nginx/fastcgi.conf;\r\n        fastcgi_intercept_errors on; \r\n        fastcgi_pass   127.0.0.1:9000; \r\n    }   \r\n\r\n}\r\n

\r\n

开启php-fpm

\r\n

sudo nginx\r\nsudo php-fpm -D\r\n

\r\n

mysql

\r\n

mysql安装

\r\n

unset TMPDIR\r\nmysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp\r\nsudo chown -R your_user /usr/local/var/mysql/\r\n

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

    mysql_secure_installation\r\n\r\nTo connect run:\r\n    mysql -uroot\r\n\r\nTo have launchd start mysql now and restart at login:\r\n  brew services start mysql\r\nOr, if you don't want/need a background service you can just run:\r\n  mysql.server start\r\n\r\n

\r\n

mysql常用命令

\r\n

mysql -h localhost -uroot -p\r\n#导出数据库\r\nmysqldump -uroot -p db > db.sql\r\n#导入数据库\r\nmysql -uroot -p db < db.sql\r\n// or\r\nmysql -uroot -p db -e "source /path/to/db.sql"\r\n#开启远程登录\r\n\r\ngrant all privileges on ss.* to 'root'@'%' indentified by 'passoword' with grant option;\r\n// or \r\nupdate user set Host="%" and User="root"\r\n// 注意%是不包含localhost的\r\nflush privileges;\r\n#创建用户\r\nCREATE USER 'test'@'localhost' IDENTIFIED BY 'password';\r\ngrant all privileges on *.* to test@'localhost' identified by 'test';\r\n#创建表\r\nCREATE SCHEMA testdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;\r\n#赋予数据库权限\r\nGRANT ALL ON testdb.* TO 'test'@'localhost';\r\n

\r\n

PHPMyAdmin

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

参考文章

\r\nhttp://yansu.org/2013/12/11/lamp-in-mac.html