brew php mysql_Mac下用Homebrew安装配置nginx+php+mysql(最新实践版)

程祺
2023-12-01

首先安装必要插件

1 安装Xcode Command Line Tools

xcode-select --install

注意

确保这步要完成,也可以直接安装xcode,装完之后要先打开一次

Tools 包含了 mac 下所有的常用开发包。

2 安装XQUARZ(可忽略)

[https://www.xquartz.org](https://www.xquartz.org)

curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.5.dmg -o /tmp/XQuartz.dmg

open /tmp/XQuartz.dmg

3 可以安装Homebrew了

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

4 成功后检查一下

brew doctor

5 更新到最新版本

brew update

brew upgrade

注意: 类似 Centos 下的 yum 和 ubuntu 下的 apt-get

安装PHP

1 准备工作

因为brew默认的formula中没有PHP-FPM,所以需要添加一下

brew tap homebrew/dupes

brew tap homebrew/php

2 开始安装

brew install --without-apache --with-fpm --with-mysql php70

(如果安装 7.1就是 php71)

正常情况下生成很多摘要,如下

==> ./configure --prefix=/usr/local/Cellar/php70/7.0.1 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/7.0

==> make

==> make install

==> /usr/local/Cellar/php70/7.0.3/bin/pear config-set php_ini /usr/local/etc/php/7.0/php.ini

==> Caveats

The php.ini file can be found in:

/usr/local/etc/php/7.0/php.ini

✩✩✩✩ PEAR ✩✩✩✩

If PEAR complains about permissions, 'fix' the default PEAR permissions and config:

chmod -R ug+w /usr/local/Cellar/php70/7.0.3/lib/php

pear config-set php_ini /usr/local/etc/php/7.0/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that

you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

PATH="/usr/local/bin:$PATH"

PHP56 Extensions will always be compiled against this PHP. Please install them

using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,

~/.zshrc, ~/.profile or your shell's equivalent configuration file:

export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:

* If this is your first install:

mkdir -p ~/Library/LaunchAgents

cp /usr/local/Cellar/php70/7.0.3/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/

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

* If this is an upgrade and you already have the homebrew.mxcl.php70.plist loaded:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist

cp /usr/local/Cellar/php70/7.0.3/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/

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

The control script is located at /usr/local/Cellar/php70/7.0.3/sbin/php70-fpm

Mountain Lion comes with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php56.plist' in old versionsof this formula.

To have launchd start php70 at login:

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

Then to load php70 now:

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

==> Summary

 /usr/local/Cellar/php70/7.0.3: 500 files, 41M, built in 4.1 minutes

3 参数调整

设置自定义php.ini文件

mkdir /usr/local/etc/php/7.0/conf.d

vim /usr/local/etc/php/7.0/conf.d/myphp.ini

把参数写进去

post_max_size = 100M

upload_max_filesize = 100M

max_execution_time = 120

short_open_tag = On

opcache

opcache.memory_consumption=128

opcache.interned_strings_buffer=8

opcache.max_accelerated_files=4000

opcache.revalidate_freq=60

opcache.fast_shutdown=1

opcache.enable_cli=1

4 开机启动

版本参数根据自己实际版本配置。按 tab补全即可。

按以上摘要的内容,设置开机启动

1. mkdir -p ~/Library/LaunchAgents

2. cp /usr/local/Cellar/php70/7.0.3/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/

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

5 检查端口

这时可以看到php已经运行在9000端口了

lsof -Pni4 | grep LISTEN | grep php

6 PHP组件安装

一般情况下,只要这样就可以了

brew install php70-mongo

安装NGINX

1 开始安装

brew install nginx

#开机启动需要ROOT权限

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

2 创建文件夹

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

sudo chown :staff /data/www

sudo chmod 775 /data/www

3 conf

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

worker_processes 1;

error_log /usr/local/etc/nginx/logs/error.log debug;

events {

worker_connections 1024;

}

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/etc/nginx/logs/access.log main;

sendfile on;

keepalive_timeout 65;

index index.html index.php;

include /usr/local/etc/nginx/sites-enabled/*;

}

4 php-fpm

vim /usr/local/etc/nginx/conf.d/php-fpm

location ~ .php{

try_files $uri = 404;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param CI_ENV development;

include fastcgi_params;

}

5 SSL支持

创建一个文件夹

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

生成 4096位 RSA 密钥和自签名:

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=blue.com" -keyout /usr/local/etc/nginx/ssl/blue.com.key -out /usr/local/etc/nginx/ssl/blue.com.crt

6 新建一个默认主机

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

server {

listen 80;

server_name localhost;

root /data/www/;

access_log /usr/local/etc/nginx/logs/default.access.log main;

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;

}

7 新建一个SSL主机

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

server {

listen 443;

server_name localhost;

root /data/www/;

access_log /usr/local/etc/nginx/logs/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;

}

把需要启用的主机配置文件链接到sites-enabled目录下:

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

8 启动NGINX

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

安装mysql

安装mysql5.7

5.6版本没坑。直接装就行。忽略最后更改密码这步。

在 mysql 官网上下载 dmg包:mysql下载包直达

配置

安装完后,会有个弹窗,给出临时密码。

配置 PATH 变量

打开/user/(你的用户名)/.bash_profile文件

加上export PATH=$PATH:/usr/local/mysql/bin

在终端中输入source ./.profile命令刷新一下

在终端中输入$PATH检查一下

更改密码

之前的临时密码可以登录一次,更改密码即可。但经常改不了。说权限不足。用以下方法更改。

* 打开一个终端窗口

* 输入 sudo /usr/local/mysql/support-files/mysql.server stop

* 输入 sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables

* 这时不要关窗口,再打开一个终端窗口

* 输入 sudo /usr/local/mysql/bin/mysql -u root

* 这时候会出现mysql>了,输入use mysql

* 最后输入 update user set authentication_string=password('新密码') where user='root';

常用命令

1 NGINX

启动/停止/重启 Nginx:

nginx.start

nginx.stop

nginx.restart

快速查看相关日志:

nginx.logs.access

nginx.logs.default.access

nginx.logs.default-ssl.access

nginx.logs.error

检查配置:

[sudo] nginx -t

2 PHP-FPM

启动/停止/重启 PHP-FPM:

php-fpm.start

php-fpm.stop

php-fpm.restart

检查配置:

[sudo] php-fpm -t

 类似资料: