Home > Article > Backend Development > New Mac configuration PHP development environment tutorial_php skills
I have been using Mac OS There are not many shutdowns, but every time I turn it on or wake up the computer, the system obviously freezes for a certain period of time. Especially when starting up. You can definitely go and soak one or have a bucket. Therefore, remove the optical drive on MD101 and replace it with a 256G Sandisk SSD. Remove the original 2G 2 and replace it with an 8G 2. The speed should be excellent, and it can serve for at least another 2 years.
Taking advantage of this opportunity to add a hard drive, I am going to completely redo the development environment. Now that I have a certain understanding of Mac, I specially recorded the installation details of this development environment to give myself a note. I hope it can also help those who are new to Mac
environment students. This development environment is all installed based on HomeBrew.
Main software versions: PHP5.5.14, Nginx 1.6.0, MySQL5.6.19
OS X Mavericks
Reinstall the system, download the OS X Mavericks installation file from the Apple Store, and then prepare a 16G USB3.0 USB flash drive. Create a boot USB flash drive for a new installation of OSX Mavericks.
Plug in the USB flash drive and execute in the terminal:
untitled is your USB drive letter, depending on the actual situation.
Seeing the above information indicates that the boot disk was successfully created. So easy to install :)
After installing the system, I haven’t migrated the files yet. Because I like photography, I have a lot of original images in RAW format in Aperture
In the photo library of Nikon D800, one RAW file is about 40M. When the time comes to migrate the photo library and photo stream, I hope I won’t fall into a trap. Let’s wait until the development environment is ready. Update the OS X system to 10.9.4
, and then install the latest Xcode, as well as software bought in the store. Make sure there are no problems with the system, go to the next step...
Brew
Brew is a package management tool for Mac. It hosts compilation configurations and patches suitable for Mac through Github, making it easy to install development tools. Mac comes with ruby
So it is very convenient to install, and it will also automatically install git for you. Official website: f121156d6da702a53f2955acdc9f639a; .
After the installation is completed, it is recommended to perform a self-check. If brew doctor sees
Your system is ready to brew. Then your brew is ready to use.
Installation:
Note: It is best not to use brew services related commands frequently, as the prompt will be removed
Stopping `php55`... (might take a while)
==> Successfully stopped `php55` (label: homebrew.mxcl.php55)
==> Successfully started `php55` (label: homebrew.mxcl.php55)
Oh My Zsh
Ohmyzsh & iTerm2 are two artifacts that must be installed under Mac os x. The two groups are used together with plug-ins. It is simply a god-like existence. Kill Messi instantly, Neymar:)
Oh, click on the official website
Install oh my zsh
Set default shell
Check the list of shells supported by the system. Mac 10.9.4 comes with zsh 5.0.2, which must be installed on Linux.
cat /etc/shells # List of acceptable shells for chpass(1). # Ftpd will not allow users to connect who are not using # one of these shells. /bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh zsh --version zsh 5.0.2 (x86_64-apple-darwin13.0) chsh -s /bin/zsh
Although Mac comes with zsh, if you want the latest version of zsh, then you can use brew install zsh to install the latest one.
/usr/local/bin/zsh --version zsh 5.0.5 (x86_64-apple-darwin13.3.0) The difference is not big, the default version is already very new.
homebrew-cask
Install cask:
brew tap phinze/homebrew-cask && brew install brew-cask
cask common commands:
Let’s talk about the advantages of cask compared to the Mac App Store:
More comprehensive support for commonly used software (especially developers), cask will give you some surprises;
The software updates quickly, usually the latest version will be updated on the Store for a long time;
Command installation feels more convenient than opening the Store. In addition, the Store’s speed in China is also XXOO.
iTerm2
Install iTerm2:
brew cask install iterm2
For the artifact combination mentioned above, for more usage methods, please refer to [iTerm2 official documentation](http://www.iterm2.com/#/section/documentatio
n), which will not be explained in detail here.
Install packages & software commonly used for development
Install development package
brew cask install alfred appcleaner firefox google-chrome phpstorm sublime-text sequel-pro sketch mplayerx thunder qq
Alfred is a very good thing, and it is recommended to install it. Its default search directory does not contain software installed by brew cask, so manually change `/opt/homebrew-
cask` added to Alfred’s search directory
MySQL PHP Nginx Redis Memcache
So much preparation work has been done before. In fact, due to limited space and ability, zsh, iTerm2 brew, etc. cannot be finished in a short while. More choices, more joy, just on Google
Search, now we get to the point. come on :)
Install MySQL
MySQL开机启动:
安装完成之后开启MySQL安全机制:
根据终端提示,输入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的PHP扩展库:
PHP编译过程中如果遇到configure: error: Cannot find OpenSSL's 035d136d3952718cf434d86b1ed537d0错误,执行xcode-select --install 重新安装一下Xcode Command Line Tools 在GitHubHomeBrew上有关于这个讨论:
_For future reference of anybody looking for Command Line Tools with Xcode 5,
open up a Terminal window and type xcode-select --install. A window will
appear informing you command line tools are required. Click Install and you
should be good to go_
等待PHP编译完成,开始安装PHP常用扩展,扩展安装过程中brew会自动安装依赖包,例如php55-pdo-pgsql
会自动装上postgresql,这里我安装以下PHP扩展:
扩展里面提一下php55-phalcon 和php55-swoole.一个是C语言写的PHP框架,安装来个人摸索熟悉一下,还没有真正的使用过,大致看了一下文档,感觉非常吊炸天。目前公司的项目是基于Yii2的,也看看这个框架。
另外一个swoole是国产的PHP高性能网络通信框架,貌似不错,可能在项目中会考虑用到它。
由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本。
测试一下效果:
修改php-fpm配置文件,vim /usr/local/etc/php/5.5/php-fpm.conf,找到pid相关大概在25行,去掉注释pid = run/php-fpm.pid, 那么php-fpm的pid文件就会自动产生在/usr/local/var/run/php-fpm.pid,下面要安装的Nginx pid文件也放在这里。
启动php-fpm之后,确保它正常运行监听9000端口:
PHP-FPM开机启动:
brew install nginx --with-http_geoip_module
Nginx启动关闭命令:
#Open nginx
sudo nginx
#Reload configuration|Restart|Stop|Exit nginx
nginx -s reload|reopen|stop|quit
#You can also use Mac’s launchctl to start|stop
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Nginx starts up
vim /usr/local/etc/nginx/nginx.conf
Enter the following:
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/*;
}
#Create info.php index.html 404.html 403.html file under /var/www
vi /var/www/info.php vi /var/www/index.html vi /var/www/403.html vi /var/www/404.html
Create a default virtual host default
vim /usr/local/etc/nginx/sites-available/default
Enter:
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;
}
vim /usr/local/etc/nginx/sites-available/default-ssl
Enter:
vim /usr/local/etc/nginx/sites-available/phpmyadmin
输入以下配置
设置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
接下来你可以通过下面这些连接访问:
设置快捷服务控制命令
为了后面管理方便,将命令 alias 下,vim ~/.bash_aliases 输入一下内容:
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
本文由Fish创作