详解如何安装MacOSX环境并用homebrew搭建LNMP环境
来源:SegmentFault
时间:2023-02-24 17:31:43 106浏览 收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《全新安装Mac OSX 开发者环境 同时使用homebrew搭建 PHP,Nginx ,MySQL,Redis,Memcache ... ... (LNMP开发环境)》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下MySQL、nginx、PHP、MacOS、homebrew,希望所有认真读完的童鞋们,都有实质性的提高。
用了一年的Mac OS X了,之前不熟悉这个系统,用的是系统自带的PHP 以及DMG包安装的MySQL,时间长了,慢慢觉得MacBook的速度跟不上了,虽然关机次数不多,但是每次开机,或者唤醒电脑的时候,系统明显有一定时间的卡顿。特别表现在开机的时候。完全可以去泡一个来一桶了。 因此干掉MD101上的光驱,换上256G的Sandisk SSD, 干掉原厂的2G * 2,换上8G * 2 。 速度应该是杠杠的了,至少可以再服役2年吧。
趁着这次加硬盘的机会,就准备彻底重做开发环境。现在对Mac也有了一定的了解,特地记录一下本次的开发环境安装详情,给自己一个备忘,希望也可以帮助到刚接触Mac环境的同学们。本开发环境,全部基于HomeBrew安装。
主要软件版本:PHP5.5.14,Nginx 1.6.0,MySQL5.6.19
OS X Mavericks
重新安装系统,在苹果商店下载好OS X Mavericks安装文件,然后准备一支16G的USB3.0 U盘。制作OS X Mavericks 全新安装启动U盘。
插上U盘,在终端执行:
brew cask install alfred appcleaner firefox google-chrome phpstorm sublime-text sequel-pro sketch mplayerx thunder qq
Alfred 是个很不错的东西,推荐必须安装。它默认搜索目录不包含brew cask安装的软件,因此手动将
brew install mysqlMySQL开机启动:
/usr/local/opt/mysql/bin/mysql_secure_installation根据终端提示,输入root密码,然后依次确认一些安全选项。具体信息可以参考外国友人的这篇文章
#查看一下MySQL运行情况 ➜ ~ ps aux | grep mysql calvin 1695 0.0 0.5 2719864 90908 ?? S 1:38上午 0:00.31 /usr/local/Cellar/mysql/5.6.19/bin/mysqld --basedir=/usr/local/Cellar/mysql/5.6.19 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.6.19/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/CalvinsMacBook-Pro.local.err --pid-file=/usr/local/var/mysql/CalvinsMacBook-Pro.local.pid --socket=/tmp/mysql.sock --port=3306 calvin 1323 0.0 0.0 2444628 1020 ?? S 1:38上午 0:00.04 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --bind-address=127.0.0.1 --datadir=/usr/local/var/mysql #测试连接MySQL mysql -uroot -p Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 23 Server version: 5.6.19-log Homebrew Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>安装phpmyadmin
brew install phpmyadmin安装PHP
添加brew的PHP扩展库:
brew update brew tap homebrew/dupes brew tap josegonzalez/homebrew-php可以使用
brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysqlPHP编译过程中如果遇到
brew install php55-apcu php55-gearman php55-geoip php55-gmagick php55-imagick php55-intl php55-mcrypt php55-memcache php55-memcached php55-mongo php55-opcache php55-pdo-pgsql php55-phalcon php55-redis php55-sphinx php55-swoole php55-uuid php55-xdebug;扩展里面提一下php55-phalcon 和 php55-swoole. 一个是C语言写的PHP框架,安装来个人摸索熟悉一下,还没有真正的使用过,大致看了一下文档,感觉非常吊炸天。目前公司的项目是基于Yii2的,也看看这个框架。 另外一个swoole是国产的PHP高性能网络通信框架,貌似不错,可能在项目中会考虑用到它。
由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本。
echo 'export PATH="$(brew --prefix php55)/bin:$PATH"' >> ~/.bash_profile #for php echo 'export PATH="$(brew --prefix php55)/sbin:$PATH"' >> ~/.bash_profile #for php-fpm echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.bash_profile #for other brew install soft source ~/.bash_profile测试一下效果:
#brew安装的php 他在/usr/local/opt/php55/bin/php php -v PHP 5.5.14 (cli) (built: Jul 16 2014 15:43:06) (DEBUG) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans #Mac自带的PHP /usr/bin/php -v PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies #brew安装的php-fpm 他在/usr/local/opt/php55/sbin/php-fpm php-fpm -v PHP 5.5.14 (fpm-fcgi) (built: Jul 16 2014 15:43:12) (DEBUG) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans #Mac自带的php-fpm /usr/sbin/php-fpm -v PHP 5.4.24 (fpm-fcgi) (built: Jan 19 2014 21:32:57) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies修改php-fpm配置文件,
#测试php-fpm配置 php-fpm -t php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -t #启动php-fpm php-fpm -D php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/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` #也可以用上文提到的brew命令来重启php-fpm,不过他官方不推荐用这个命令了 brew services restart php55 #还可以用这个命令来启动php-fpm launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist启动php-fpm之后,确保它正常运行监听9000端口:
lsof -Pni4 | grep LISTEN | grep php php-fpm 30907 calvin 9u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 30917 calvin 0u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 30918 calvin 0u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 30919 calvin 0u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1:9000 (LISTEN) #正常情况,会看到上面这些进程PHP-FPM开机启动:
brew install composer #检查一下情况 composer --version Composer version 1.0.0-alpha8 2014-01-06 18:39:59redis memcached这些软件brew 已经自动依赖安装上,如果想开机自动启动,或者查看使用说明
brew install nginx --with-http_geoip_moduleNginx启动关闭命令:
#测试配置是否有语法错误 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.plistNginx开机启动
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plistNginx监听80端口需要root权限执行,因此:
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配置nginx.conf
创建需要用到的目录:mkdir -p /usr/local/var/logs/nginx mkdir -p /usr/local/etc/nginx/sites-available mkdir -p /usr/local/etc/nginx/sites-enabled mkdir -p /usr/local/etc/nginx/conf.d mkdir -p /usr/local/etc/nginx/ssl sudo mkdir -p /var/www sudo chown :staff /var/www sudo chmod 775 /var/wwwworker_processes 1; error_log /usr/local/var/logs/nginx/error.log debug; 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/logs/access.log main; sendfile on; keepalive_timeout 65; port_in_redirect off; include /usr/local/etc/nginx/sites-enabled/*; }设置nginx php-fpm配置文件
vim /usr/local/etc/nginx/conf.d/php-fpm #proxy the php scripts to php-fpm location ~ .php$ { try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_intercept_errors on; include /usr/local/etc/nginx/fastcgi.conf; }nginx虚拟主机准备工作
#创建 info.php index.html 404.html 403.html文件到 /var/www 下面 vi /var/www/info.php vi /var/www/index.html vi /var/www/403.html vi /var/www/404.html创建默认虚拟主机default
server { listen 80; server_name localhost; root /var/www/; access_log /usr/local/var/logs/nginx/default.access.log main; location / { index index.html index.htm index.php; autoindex on; include /usr/local/etc/nginx/conf.d/php-fpm; } location = /info { allow 127.0.0.1; deny all; rewrite (.*) /.info.php; } error_page 404 /404.html; error_page 403 /403.html; }创建ssl默认虚拟主机default-ssl
server { listen 443; server_name localhost; root /var/www/; access_log /usr/local/var/logs/nginx/default-ssl.access.log main; ssl on; ssl_certificate ssl/localhost.crt; ssl_certificate_key ssl/localhost.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { include /usr/local/etc/nginx/conf.d/php-fpm; } location = /info { allow 127.0.0.1; deny all; rewrite (.*) /.info.php; } error_page 404 /404.html; error_page 403 /403.html; }创建phpmyadmin虚拟主机
vim /usr/local/etc/nginx/sites-available/phpmyadmin #输入以下配置 server { listen 306; server_name localhost; root /usr/local/share/phpmyadmin; error_log /usr/local/var/logs/nginx/phpmyadmin.error.log; access_log /usr/local/var/logs/nginx/phpmyadmin.access.log main; ssl on; ssl_certificate ssl/phpmyadmin.crt; ssl_certificate_key ssl/phpmyadmin.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { index index.html index.htm index.php; include /usr/local/etc/nginx/conf.d/php-fpm; } }设置SSL
mkdir -p /usr/local/etc/nginx/ssl openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt创建虚拟主机软连接,开启虚拟主机
ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin启动|停止Nginx
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist接下来你可以通过下面这些连接访问:
http://localhost/ -> index.html
http://localhost/info -> info.php via phpinfo();
http://localhost/404 -> 404.html
https://localhost/ -> index.html(SSL)
https://localhost/info -> info.php via phpinfo();(SSL)
https://localhost/404 -> 404.html(SSL)
https://localhost:306 -> phpmyadmin(SSL)设置快捷服务控制命令
为了后面管理方便,将命令 alias 下,
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.php55.plist" alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.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'#让快捷命令生效 echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile source ~/.bash_profile #创建站点目录到主目录,方便快捷访问 ln -sfv /var/www ~/htdocs有时间会进一步整理一下,感觉有点乱,第一次在segmentfault上码字,写到一半的时候不小心滑一下触摸板,浏览器后退了,虽然有提示不要离开,点击了不离开之后,Safari白屏。 然后还特地去吐了个槽, 害我白白又浪费很久来写这个博客。希望能帮助到刚刚接触Mac的同学。enjoy Mac‘s development EVN :)
参考资料:Install Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X Mavericks using Homebrew
理论要掌握,实操不能落!以上关于《全新安装Mac OSX 开发者环境 同时使用homebrew搭建 PHP,Nginx ,MySQL,Redis,Memcache ... ... (LNMP开发环境)》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
-
475 收藏
-
266 收藏
-
273 收藏
-
283 收藏
-
210 收藏
-
371 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习