mac电脑php+mysql+nginx+phpmyadmin环境搭建

英文原文:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/php

参照博文做者: http://blog.liuweifeng.net/author/liuweifeng/node

首先,为了避免出现各类神奇的错误,请安装 XCode。Yosemite 发布后,XCode 的最新版本是 6.1.0(6A1052d)。而后,不可忽略的一步,请在终端里执行mysql

xcode-select --install  

来安装最新的 XCode 命令行工具(Xcode Command Line Tools)。nginx

安装 Homebrew

若是没有安装,请移步官网 →_→ Homebrew 官方网站git

若是你已经安装了 Homebrew,请在终端执行brew doctor检查各类可能的冲突和问题。而后执行 brew update && brew upgrade 升级 Homebrew 自身和自带的 formulasgithub

安装 PHP-FPM

添加brew的php扩展库:redis

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

安装开发包:sql

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

而后开始安装:shell

brew search php     //choose php version

  
  
  
  
  brew install php71 --with-fpm --with-gmp --with-imap --with-tidy —with-debug —with-mysql —with-libmysql 

安装php一下扩展apache

brew install php71-apcu\
 php71-gearman\
 php71-geoip\
 php71-gmagick\
 php71-imagick\
 php71-mcrypt\
 php71-memcache\
 php71-memcached\
 php71-mongo\
 php71-apache\
 php71-pdo-pgsql\
 php71-redis\
 php71-sphinx\
 php71-swoole\
 php71-uuid\
 php71-xdebug;

更改成新安装的php,打开~/.bash_profile

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"  //能够采起下面的方式

由于咱们打算安装 Nginx,Apache 就不须要了(貌似 OS X 自带了 Apache?)。

若是你想在命令行里使用 PHP 的话,你须要在你 shell 的 Profile 里设置下 $PATH:

# If you use Bash    
echo 'export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile  
# If you use ZSH
echo 'export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"' >> ~/.zshrc && . ~/.zshrc  

安装composer

brew install composer
composer --version

设置开机启动:

mkdir -p ~/Library/LaunchAgents  
cp /usr/local/Cellar/php71/7.1.5_17/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/  

启动 PHP-FPM:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist 

看下 PHP-FPM 是否是在监听 9000 端口:

lsof -Pni4 | grep LISTEN | grep php  

输出应该是这个样子的:

php-fpm   78168 liuweifeng    7u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  
php-fpm   78170 liuweifeng    0u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  
php-fpm   92277 liuweifeng    0u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  
php-fpm   94154 liuweifeng    0u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  

安装 MySQL

brew install mysql 

设置开机启动:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents 

MySQL 安全设置

mysql_secure_installation

能够设置 root 的密码、移除匿名用户、禁止 root 远程登陆,等等。

安装 phpMyAdmin

首先须要安装 autoconf

brew install autoconf 

设置 $PHP_AUTOCONF

# If you use Bash
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile  
# If you use ZSH
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc 

开始安装 phpMyAdmin :

brew install phpmyadmin  

安装 Nginx

brew install nginx 

设置开机自动启动

因为咱们须要启动 80 端口,因此须要使用 sudo 来运行 nginx

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/  
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

启动 Nginx

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

关闭 Nginx

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist  

更多配置

nginx.conf

建立配置文件中须要用到的目录

mkdir -p /usr/local/etc/nginx/logs  
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  

移除默认的 nginx.conf(其实在 /usr/local/etc/nginx/nginx.conf.default 还有个备份,你能够看一眼,若是你想看的话。。。),而后使用 curl从 Github 上下载个人自定义版本:

rm /usr/local/etc/nginx/nginx.conf  
curl -L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf  

这是一个尽量简单和轻量级的配置文件。

加载 PHP-FPM

从 Github 下载个人自定义配置文件:

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm 

建立默认的虚拟主机

curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default  
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl  
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin  

使用 Git 从 Github 上克隆个人实例虚拟主机配置(包括默认的 40四、403页面,和一个 phpinfo() rewrite)。

git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www  
rm -rf /var/www/.git  

克隆完记得把 .git 目录删除掉,这样就不会被 Git 记录了。

译者注:其实吧,我赶脚这个 404 和 403 挺丑的。。。

设置 SSL

建立须要的目录

mkdir -p /usr/local/etc/nginx/ssl

建立 4096bit RSA keys 和自签名证书:

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:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

最终测试

基本上完事了,点击下面的连接来感觉下吧:

服务配置

之后你确定会关闭或者重启上面设置的某个服务的,因此你可能须要设置一些别名。(须要修改bash_aliases中的php版本)

# If you use Bash
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases  
cat /tmp/.bash_aliases >> ~/.bash_aliases  
echo "source ~/.bash_aliases" >> ~/.bash_profile  
# If you use ZSH
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.zshrc  
cat /tmp/.bash_aliases >> ~/.zshrc  
echo "source ~/.bash_aliases" >> ~/.zshrc  

再打开一个终端窗口,来使上面的别名生效:

source ~/.bash_profile  
# or
source ~/.zshrc  

如今你能够使用别名而不是 launchctl 和那一长串参数了~

Nginx

你能够这样来启动、关闭、重启 Nginx:

nginx.start  
nginx.stop  
nginx.restart  

快速 tail 访问日志和错误日志:

nginx.logs.access  
nginx.logs.default.access  
nginx.logs.phpmyadmin.access  
nginx.logs.default-ssl.access  
nginx.logs.error  
nginx.logs.phpmyadmin.error  

检查配置:

sudo nginx -t 

PHP-FPM

启动、关闭、重启 PHP-FPM:

php-fpm.start  
php-fpm.stop  
php-fpm.restart 

检查配置:

php-fpm -t

MySQL

启动、关闭、重启 MySQL:

mysql.start  
mysql.stop  
mysql.restart  
相关文章
相关标签/搜索