Home  >  Article  >  Backend Development  >  New Mac configuration PHP development environment tutorial_php skills

New Mac configuration PHP development environment tutorial_php skills

WBOY
WBOYOriginal
2016-05-16 19:59:041081browse

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:

Copy code The code is as follows:

sudo /Applications/Install OS X
Mavericks.app/Contents/Resources/createinstallmedia --volume /Volumes/untitled
--applicationpath /Applications/Install OS X Mavericks.app --nointeraction

untitled is your USB drive letter, depending on the actual situation.

Copy code The code is as follows:

Erasing Disk: 0%… 10%… 20%… 30%…100%…
>Copying installer files to disk…
Copy complete.
Making disk bootable…
Copying boot files…
>Copy complete.
>Done.

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:

Copy code The code is as follows:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

Self-check:
Copy code The code is as follows:

brew doctor

Commonly used commands: (all software uses PHP5.5 as an example)

Copy code The code is as follows:

brew update #Update brew installable packages, it is recommended to execute it every time
brew search php55 #Search php5.5
brew tap josegonzalez/php #Install extension1e74bc0c1b384a5ea8f9a5925de0fe3d
brew tap #View the list of installed extensions
brew install php55 #Install php5.5
brew remove php55 #Uninstall php5.5
brew upgrade php55 #Upgrade php5.5
brew options php55 #View php5.5 installation options
brew info php55 #View php5.5 related information
brew home php55 #Visit php5.5 official website
brew services list #View the services installed by the system through brew
brew services cleanup #Clear uninstalled and useless startup configuration files
brew services restart php55 #Restart php-fpm

Note: It is best not to use brew services related commands frequently, as the prompt will be removed

Copy code The code is as follows:

~ brew services restart php55
Warning: brew services is unsupported and will be removed soon.
You should use launchctl instead.
Please feel free volunteer to support it in a tap.

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

Copy code The code is as follows:

curl -L http://install.ohmyz.sh | sh

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:

Copy code The code is as follows:

brew cask search #List all software that can be installed
brew cask search php #Find all php-related applications
brew cask list #List all software installed through cask
brew cask info phpstorm #View phpstorm information
brew cask uninstall qq #Uninstall QQ

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

Copy code The code is as follows:

brew install wget watch tmux cmake openssl imagemagick graphicsmagick gearman geoip readline autoconf multitail source-highlight autojump zsh-completions sshfs

Upgrade the vim that comes with the system
Copy code The code is as follows:

brew install ctags macvim --env-std --override-system-vim

Install commonly used software

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

Copy code The code is as follows:

brew install mysql

MySQL开机启动:

复制代码 代码如下:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

安装完成之后开启MySQL安全机制:

复制代码 代码如下:

/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 options php55命令来查看安装php5.5的选项,这里我用下面的选项安装:
复制代码 代码如下:

brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql

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扩展:

复制代码 代码如下:

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-phalconphp55-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配置文件,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配置
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开机启动:

复制代码 代码如下:

ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist

安装php composer
复制代码 代码如下:

brew install composer
#检查一下情况
composer --version
Composer version 1.0.0-alpha8 2014-01-06 18:39:59

redis memcached这些软件brew 已经自动依赖安装上,如果想开机自动启动,或者查看使用说明 brew info redis即可。另外,composer的中文文档:猛戳这里
安装Nginx

brew install nginx --with-http_geoip_module
Nginx启动关闭命令:

复制代码 代码如下:

#Test whether the configuration has syntax errors
nginx -t

#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

Copy code The code is as follows:

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Nginx listening on port 80 requires root privileges to execute, so:
Copy code The code is as follows:

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

Configure nginx.conf
Create the required directories:
Copy code The code is as follows:

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/www

vim /usr/local/etc/nginx/nginx.conf

Enter the following:

Copy code The code is as follows:

worker_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/*;
}


Set nginx php-fpm configuration file
Copy code The code is as follows:

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 virtual host preparation work

#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:

Copy code The code is as follows:

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;
}


Create ssl default virtual host default-ssl

vim /usr/local/etc/nginx/sites-available/default-ssl

Enter:

Copy code The code is as follows:

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
接下来你可以通过下面这些连接访问:

复制代码 代码如下:

0edcbd5fc34e54e037629fa549452a4c -> index.html
70f9f3bf900def9900ca6bd92fd487bd -> info.php via phpinfo();
a4341a890d1c7d76776b86be3149942d -> 404.html
cea3df7c6d2306419bfc5f9976d4eebf -> index.html(SSL)
239e1000e06e9f5a238713dff6bfa1c0 -> info.php via phpinfo();(SSL)
9fcde7c39bbfc407d75df2f2cbec2fa7 -> 404.html(SSL)
03e31c618eeda2386e0116276c5e5113 -> phpmyadmin(SSL)

设置快捷服务控制命令

为了后面管理方便,将命令 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创作

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn