Home >Backend Development >PHP Tutorial >New Mac configuration PHP development environment tutorial, macphp development tutorial_PHP tutorial
I have been using Mac OS X for a year. I was not familiar with this system before, so I used the PHP that comes with the system. As well as the MySQL installed by the DMG package, over time, I gradually felt that the speed of the MacBook could not keep up. Although it was not shut down many times, the system was obviously stuck for a certain period of time every time it was turned on or the computer was woken up. 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 based on HomeBrew installation.
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, a RAW file is about 40M. I hope that I will not fall into the trap when I migrate the photo library and photo stream. 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 is no problem 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 more usage methods of the artifact combination mentioned above, please refer to [iTerm2 official document](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 starts up:
Copy code The code is as follows:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
After the installation is complete, enable the MySQL security mechanism:
Copy code The code is as follows:
/usr/local/opt/mysql/bin/mysql_secure_installation
According to the terminal prompts, enter the root password, and then confirm some security options in sequence. For specific information, please refer to
This article from a foreign friend
Check the operation status of 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>
Install phpmyadmin
Copy code The code is as follows:
brew install phpmyadmin
Install PHP
Add brew’s PHP extension library:
Copy code The code is as follows:
brew update brew tap homebrew/dupes brew tap josegonzalez/homebrew-php
You can use the brew options php55 command to view the options for installing php5.5. Here I use the following options to install:
Copy code The code is as follows:
brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql
If you encounter the configure: error: Cannot find OpenSSL's 035d136d3952718cf434d86b1ed537d0 error during PHP compilation, execute xcode-select --install and reinstall Xcode Command Line Tools. There is a discussion about this on 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_
Wait for PHP compilation to complete and start installing common PHP extensions. During the extension installation process, brew will automatically install dependent packages, such as php55-pdo-pgsql
Postgresql will be automatically installed. Here I install the following PHP extension:
Copy code The code is as follows:
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;
Mention php55-phalcon and php55-swoole in the extension. One is a PHP framework written in C language. I installed it to get familiar with it personally. I haven’t really used it yet. I took a rough look at the documentation and found it very exciting. The company's current project is based on Yii2, so take a look at this framework.
Another swoole is the domestic PHP high-performance network communication framework, which looks good and may be considered for use in the project.
Since Mac comes with php and php-fpm, you need to add the system environment variable PATH to replace the built-in PHP version.
Copy code The code is as follows:
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
Test the effect:
Copy code The code is as follows:
The php installed by #brew is in /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 comes with 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
The php-fpm installed by #brew is in /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 comes with 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
Modify the php-fpm configuration file, vim /usr/local/etc/php/5.5/php-fpm.conf, find the pid related about line 25, remove the comment pid = run/php-fpm.pid, then php The pid file of -fpm will be automatically generated in /usr/local/var/run/php-fpm.pid, and the Nginx pid file to be installed below is also placed here.
Copy code The code is as follows:
#Test php-fpm configuration
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
#Start 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
#Closephp-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`
#restartphp-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`
#You can also use the brew command mentioned above to restart php-fpm, but it is not officially recommended to use this command
brew services restart php55
#You can also use this command to start php-fpm
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
After starting php-fpm, make sure it is running normally and listening to port 9000:
Copy code The code is as follows:
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)
#Normally, you will see the above processes
PHP-FPM starts up:
Copy code The code is as follows:
ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
Install php composer
Copy code The code is as follows:
brew install composer
#Check the situation
composer --version
Composer version 1.0.0-alpha8 2014-01-06 18:39:59
The software brew such as redis memcached has been automatically installed as a dependency. If you want to start automatically at boot, you can check the instructions for use brew info redis. In addition, composer’s Chinese documentation: click here
Install Nginx
brew install nginx --with-http_geoip_module
Nginx startup shutdown command:
Copy code The code is as follows:
#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;
}
Create phpmyadmin virtual host
vim /usr/local/etc/nginx/sites-available/phpmyadmin
Enter the following configuration
Copy code The code is as follows:
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;
} }
Set up 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创作