Hi!请登陆

mac下使用brew配置nginx+php+mysql+PostgreSQL

2020-10-31 64 10/31

mac下使用brew配置nginx+php+mysql+PostgreSQL

大家在win下和Linux系统下配置PHP运行环境已经有很多参考资料了,以OZABC的经验,win下最好用的是UPUPW,linux求方便是centos+kangle,更加复杂的方法可以翻翻OZABC的文章归档,里面有很多可以参考的内容。

今天由于工作需要,必须在OSX下配置PHP的开发运行环境,经过一番折腾,终于搞定了!主要参考了Install Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X Mavericks or Yosemite这篇文章,推荐英文好的同学直接看原文。

下面记录一下!(请按照顺序配置)

zsh

请参考zsh + oh-my-zsh 默认shell的最佳替代品

xcode

xcode里面包含了很多命令行工具,为我们后续的操作提供技术支持!所以第一件事情就是安装最新版的xcode,请在Mac App Store中自行安装!

安装完毕后运行一下,然后同意协议,稍等片刻,等安装完毕后关闭xcode,最后运行

xcode-select --install

进入下一阶段!

Brew

第二步请确认你是否已经安装了OSX下的包管理工具brew了!如果没有安装,请移步http://brew.sh/速度安装好!

PHP

有了brew,剩下的问题就很简单了!我们先添加水龙头(软件源)

brew tap homebrew/php

基础安装

brew install php71

如果你有更多的需求,可以搜索包库,直接安装你想要的包,比如需要添加pdo,我们不需要重新编译php,而是这样子!

brew search php71
brew install php71-pdo

安装redis可能会报找不到include/igbinary.h,可以用下面的方法解决!

Homebrew PHP5.6 memcached installation error

系统路径

安装完毕后,我们想要将php加入系统路径,我们可以先查看brew安装php的路径

brew --prefix homebrew/php/php56

我们需要把MACOS自带的PHP改个名字,才能使我们新安装的生效

ll /usr/bin |grep php
-rwxr-xr-x   1 root   wheel   6.6K Feb  8  2017 php-config
-rwxr-xr-x   1 root   wheel    10M Apr 29 08:31 php56
-rwxr-xr-x   1 root   wheel   4.4K Feb  8  2017 phpize
# 重命名
sudo mv php-config php56-config
sudo mv php php56
sudo mv phpize phpize56

开机启动

# old
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
# new
brew services start php71

启动PHP-FPM

# old
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

配置文件

配置文件位于/usr/local/etc/php/7.1,看看吧!

查看运行

lsof -Pni4 | grep LISTEN | grep php

mysql

brew install mysql

开机启动

# old
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
# new
brew services start mysql

运行mysql

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

开始安全配置

mysql_secure_installation

nginx

brew install nginx

自动启动

# old
sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
# new
brew services start nginx

运行NGINX

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

php-nginx

server {
listen 80;
server_name foo.com;
root /path;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}

相关推荐