Mac安装Nginx、Mysql、PHP、Redis

安装xcode命令行工具的命令
xcode-select --install
 

安装homebrew:

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

 

旧版本homebrew升级:

brew update

 

Nginx安装

brew install nginx

➜  ~ brew install nginx
Updating Homebrew...
==> Installing dependencies for nginx: pcre, openssl@1.1
==> Installing nginx dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.39.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pcre-8.39.sierra.bottle.tar.gz
?  /usr/local/Cellar/pcre/8.39: 203 files, 5.4M
==> Installing nginx dependency: openssl@1.1
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.0c.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.0c.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl@1.1/lib
    CPPFLAGS: -I/usr/local/opt/openssl@1.1/include

==> Summary
?  /usr/local/Cellar/openssl@1.1/1.1.0c: 6,225 files, 15.3M
==> Installing nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.2_1.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring nginx-1.10.2_1.sierra.bottle.1.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
?  /usr/local/Cellar/nginx/1.10.2_1: 8 files, 979.8K

 

配置非管理员开机nginx自动启动的权限和分组:

sudo chown root:wheel /usr/local/Cellar/nginx/1.10.2_1/sbin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.10.2_1/sbin/nginx

 

nginx相关命令:

# 查看nginx版本
➜  ~ nginx -v
nginx version: nginx/1.10.2

# 启动 nginx服务
sudo nginx

 #测试配置是否有语法错误

➜ ~ nginx -t

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

# 重新加载配置|重启|停止|退出

nginx nginx -s reload|reopen|stop|quit

 

浏览器中查看效果

nginx启动后,在浏览器中输入http://localhost:8080/,回车即可看到运行结果
显示的是/usr/local/Cellar/nginx/1.10.2_1/html/index.html文件的内容。

开机自启动nginx服务设置:

mkdir -p ~/Library/LaunchAgents
➜  ~ cp /usr/local/Cellar/nginx/1.10.2_1/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/~ cp /usr/local/Cellar/nginx/1.10.2_1/homebrew.mxcl.nginx.plist /Users/cony/Library/LaunchAgents/~ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
➜  ~ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist #取消开机启动

 

Mysql安装

brew install mysql

➜  ~ brew install mysql
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
macvim                                                    pgbadger                                                  proxychains-ng

==> Installing dependencies for mysql: openssl
==> Installing mysql dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2j.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2j.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
?  /usr/local/Cellar/openssl/1.0.2j: 1,695 files, 12M
==> Installing mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.17.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.7.17.sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/5.7.17/bin/mysqld --initialize-insecure --user=cony --basedir=/usr/local/Cellar/mysql/5.7.17 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
?  /usr/local/Cellar/mysql/5.7.17: 14,226 files, 444.4M

 

加入开机启动

➜  ~ cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/~ cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist /Users/cony/Library/LaunchAgents/~ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
➜  ~ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

 

mysql命令:

mysql.server start    # mysql 开启
mysql.server stop   # mysql 关闭

 

初始化mysql数据库:

➜  ~ cd /usr/local/Cellar/mysql/5.7.17/bin
➜  ~ ./mysql_install_db

 

设置root账户和密码:
./mysql_secure_installation 一直跟着提示走就行

 

Redis安装

~ brew install redis
==> Downloading https://homebrew.bintray.com/bottles/redis-3.2.6.sierra.bottle.tar.g
######################################################################## 100.0%
==> Pouring redis-3.2.6.sierra.bottle.tar.gz
==> Caveats
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf
==> Summary
?  /usr/local/Cellar/redis/3.2.6: 11 files, 1.7M

 

redis默认配置文件不允许以Deamon方式运行,因此需要先修改配置文件

~ vi /usr/local/etc/redis.conf

 

将daemonize修改为yes,然后载入配置文件即可实现后台进程启动

redis-server /usr/local/etc/redis.conf

 

加入开机启动

➜  ~ cp /usr/local/Cellar/redis/3.2.6/homebrew.mxcl.redis.plist /Users/cony/Library/LaunchAgents/

 

测试redis server是否启动

➜  ~ redis-cli ping
PONG

 

php7安装

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

➜  ~ brew install php70 --without-apache --with-fpm
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
No changes to formulae.

==> Installing php70 from homebrew/php
==> Installing dependencies for homebrew/php/php70: gettext, icu4c, jpeg, libxml2, unixodbc, readline
==> Installing homebrew/php/php70 dependency: gettext
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.19.8.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gettext-0.19.8.1.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

macOS provides the BSD gettext library and some software gets confused if both are in the library path.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/gettext/lib
    CPPFLAGS: -I/usr/local/opt/gettext/include

==> Summary
?  /usr/local/Cellar/gettext/0.19.8.1: 1,934 files, 16.9M
==> Installing homebrew/php/php70 dependency: icu4c
==> Downloading https://homebrew.bintray.com/bottles/icu4c-58.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring icu4c-58.2.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

macOS provides libicucore.dylib (but nothing else).

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/icu4c/lib
    CPPFLAGS: -I/usr/local/opt/icu4c/include

==> Summary
?  /usr/local/Cellar/icu4c/58.2: 242 files, 65M
==> Installing homebrew/php/php70 dependency: jpeg
==> Downloading https://homebrew.bintray.com/bottles/jpeg-8d.sierra.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring jpeg-8d.sierra.bottle.2.tar.gz
?  /usr/local/Cellar/jpeg/8d: 19 files, 708.3K
==> Installing homebrew/php/php70 dependency: libxml2
==> Downloading https://homebrew.bintray.com/bottles/libxml2-2.9.4_2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libxml2-2.9.4_2.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/libxml2/lib
    CPPFLAGS: -I/usr/local/opt/libxml2/include

==> Summary
?  /usr/local/Cellar/libxml2/2.9.4_2: 277 files, 9.8M
==> Installing homebrew/php/php70 dependency: unixodbc
==> Downloading https://homebrew.bintray.com/bottles/unixodbc-2.3.4.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring unixodbc-2.3.4.sierra.bottle.tar.gz
?  /usr/local/Cellar/unixodbc/2.3.4: 39 files, 952.5K
==> Installing homebrew/php/php70 dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-7.0.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-7.0.1.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.


Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
?  /usr/local/Cellar/readline/7.0.1: 46 files, 2M
Warning: homebrew/php/php70: this formula has no --with-fpm option so it will be ignored!
Warning: homebrew/php/php70: this formula has no --without-apache option so it will be ignored!
==> Installing homebrew/php/php70
==> Downloading https://homebrew.bintray.com/bottles-php/php70-7.0.14_7.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring php70-7.0.14_7.sierra.bottle.tar.gz
==> Caveats
The php.ini file can be found in:
    /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"

PHP70 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:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist

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

OS X 10.8 and newer come 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.php70.plist' in old versions
of this formula.

With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system
you have to install php with the --with-httpd24 option. See  brew options php70  for more details.

To have launchd start homebrew/php/php70 now and restart at login:
  brew services start homebrew/php/php70
==> Summary
?  /usr/local/Cellar/php70/7.0.14_7: 332 files, 38.8M

 

设置php的全局变量

➜  ~ vi ~/.zshrc
export LOCALBIN=/usr/local/bin
export SBIN=/usr/sbin
export PATH=$LOCALBIN:$SBIN:$PATH
export PHPBIN=$(brew --prefix homebrew/php/php70)/bin
export PATH=$PHPBIN:$PATH

➜  ~ source ~/.zshrc # 即时生效

 

安装需要的php扩展

➜  ~ brew install homebrew/php/php70-gearman homebrew/php/php70-mongodb homebrew/php/php70-phalcon homebrew/php/php70-redis
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
nativefier                                                                             spdlog
==> Updated Formulae
arangodb                                                  nghttp2                                                   passenger

==> Installing php70-gearman from homebrew/php
==> Installing dependencies for homebrew/php/php70-gearman: boost, libevent, gearman
==> Installing homebrew/php/php70-gearman dependency: boost
==> Downloading https://homebrew.bintray.com/bottles/boost-1.63.0.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring boost-1.63.0.sierra.bottle.tar.gz
?  /usr/local/Cellar/boost/1.63.0: 12,491 files, 397.1M
==> Installing homebrew/php/php70-gearman dependency: libevent
==> Downloading https://homebrew.bintray.com/bottles/libevent-2.0.22.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring libevent-2.0.22.sierra.bottle.1.tar.gz
?  /usr/local/Cellar/libevent/2.0.22: 734 files, 2.0M
==> Installing homebrew/php/php70-gearman dependency: gearman
==> Downloading https://homebrew.bintray.com/bottles/gearman-1.1.14.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gearman-1.1.14.sierra.bottle.tar.gz
==> Caveats
To have launchd start gearman now and restart at login:
  brew services start gearman
Or, if you don't want/need a background service you can just run:
  gearmand -d
==> Summary
?  /usr/local/Cellar/gearman/1.1.14: 211 files, 1.7M
==> Installing homebrew/php/php70-gearman
==> Downloading https://github.com/wcgallego/pecl-gearman/archive/gearman-2.0.1.tar.gz
==> Downloading from https://codeload.github.com/wcgallego/pecl-gearman/tar.gz/gearman-2.0.1
######################################################################## 100.0%
==> /usr/local/opt/php70/bin/phpize
==> ./configure --prefix=/usr/local/Cellar/php70-gearman/2.0.1 --with-php-config=/usr/local/opt/php70/bin/php-config --with-gearman=/usr/local/opt/gearman
==> make
==> Caveats
To finish installing gearman for PHP 7.0:
  * /usr/local/etc/php/7.0/conf.d/ext-gearman.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the gearman module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run `php -i "(command-line 'phpinfo()')"`
  * - Look for the info on the gearman module.
  * - If you see it, you have been successful!
==> Summary
?  /usr/local/Cellar/php70-gearman/2.0.1: 7 files, 102.9K, built in 22 seconds
==> Installing php70-mongodb from homebrew/php
==> Downloading https://homebrew.bintray.com/bottles-php/php70-mongodb-1.2.2_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring php70-mongodb-1.2.2_1.sierra.bottle.tar.gz
==> Caveats
To finish installing mongodb for PHP 7.0:
  * /usr/local/etc/php/7.0/conf.d/ext-mongodb.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the mongodb module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run `php -i "(command-line 'phpinfo()')"`
  * - Look for the info on the mongodb module.
  * - If you see it, you have been successful!
==> Summary
?  /usr/local/Cellar/php70-mongodb/1.2.2_1: 3 files, 655.2K
==> Installing php70-phalcon from homebrew/php
==> Downloading https://homebrew.bintray.com/bottles-php/php70-phalcon-3.0.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring php70-phalcon-3.0.2.sierra.bottle.tar.gz
==> Caveats
To finish installing phalcon for PHP 7.0:
  * /usr/local/etc/php/7.0/conf.d/ext-phalcon.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the phalcon module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run `php -i "(command-line 'phpinfo()')"`
  * - Look for the info on the phalcon module.
  * - If you see it, you have been successful!
==> Summary
?  /usr/local/Cellar/php70-phalcon/3.0.2: 5 files, 3.5M
==> Installing php70-redis from homebrew/php
==> Downloading https://github.com/phpredis/phpredis/archive/3.0.0.tar.gz
==> Downloading from https://codeload.github.com/phpredis/phpredis/tar.gz/3.0.0
######################################################################## 100.0%
==> /usr/local/opt/php70/bin/phpize
==> ./configure --prefix=/usr/local/Cellar/php70-redis/3.0.0 --with-php-config=/usr/local/opt/php70/bin/php-config
==> make
==> Caveats
To finish installing redis for PHP 7.0:
  * /usr/local/etc/php/7.0/conf.d/ext-redis.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the redis module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run `php -i "(command-line 'phpinfo()')"`
  * - Look for the info on the redis module.
  * - If you see it, you have been successful!
==> Summary
?  /usr/local/Cellar/php70-redis/3.0.0: 5 files, 405.3K, built in 20 seconds

 

直接运行,有报错找不到配置文件。

$ php-fpm

[11-Jan-2014 16:03:03] ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)

[11-Jan-2014 16:03:03] ERROR: failed to load configuration file '/private/etc/php-fpm.conf'

[11-Jan-2014 16:03:03] ERROR: FPM initialization failed

 

可以在/private/etc/ 目录下生成配置文件,需要root权限(sudo)

或者在普通用户有权限的目录里放置配置文件,通过--fpm-config参数指定配置文件的位置,如下:

# cp /private/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf

 

$ php-fpm --fpm-config /usr/local/etc/php-fpm.conf

[11-Jan-2014 16:10:49] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)

[11-Jan-2014 16:10:49] ERROR: failed to post process the configuration

[11-Jan-2014 16:10:49] ERROR: FPM initialization failed

 

错误信息显示:不能正确的打开”日志“文件,原因是默认在/usr/var目录下工作,可以修改配置文件指定正确的日志文件路径

 

$ vim /usr/local/etc/php-fpm.conf

修改php-fpm.conf文件中的error_log项,默认前缀是/usr/var ,但并没有这个路径

 
error_log = /usr/local/var/log/php-fpm.log

pid = /usr/local/var/run/php-fpm.pid

 

或者不修改配置文件中配置项的路径,在php-fpm的运行参数中(-p)指定放置运行时文件的相对路径前缀

 

$ php-fpm --fpm-config /usr/local/etc/php-fpm.conf  --prefix /usr/local/var

到此,php-fpm守护进程已经基本可以正确的启动了。

 

下面我们看下php.ini配置文件及扩展的安装。

首先看下编译参数,有些值是编译进执行程序的,无法更改。

$ php -i|grep config

找到配置文件(php.ini)、目录的位置,下面两项的值指定

'--with-config-file-path=/etc'

'--with-config-file-scan-dir=/Library/Server/Web/Config/php'

所以我们需要在/etc目录下创建php.ini,Mac在/private/etc,/etc下均提供了样例文件php.ini.default,通过查验,两个文件完全相同,所以复制哪一个都无所谓,Mac有提供md5而不是Linux下的md5sum:

$ md5 /private/etc/php.ini.default /etc/php.ini.default

MD5 (/private/etc/php.ini.default) = 1c47241665ea5efdc55fd5809f675449

MD5 (/etc/php.ini.default) = 1c47241665ea5efdc55fd5809f675449

 

/etc目录权限root:wheel,需要root权限或使用sudo,关于如何设置Mac的sudo命令需要的密码,请查看

http://support.apple.com/kb/HT4103?viewlocale=zh_CN&locale=zh_CN

http://support.apple.com/kb/PH6515?viewlocale=zh_CN

 

# cp /etc/php.ini.default  /etc/php.ini

变更own,以后修改不用老是切换root,生产环境最好不要改

# chown <你的用户名> /etc/php.ini

# chmod u+w /etc/php.ini

 

php扩展介绍

php70-amqp  高级消息队列协议
php70-ast   抽象语法树
php70-blitz 用C语言开发的快速的PHP模板引擎
php70-couchbase 新兴的NOSQL数据库
php70-gearman  异步任务分发处理利器
php70-geoip 根据IP获取地理位置及计算距离的方法,包括获取目标IP所在的国家地区等信息
php70-gmagick 图像生成和处理
php70-gmp 是一个开源的数学运算库,它可以用于任意精度的数学运算
php70-hprose 高性能远程对象服务引擎
php70-ioncubeloader 加密PHP的工具
...

加入开机自启

➜  ~  cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/~  cp /usr/local/opt/php70/homebrew.mxcl.php70.plist /Users/cony/Library/LaunchAgents/~ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
➜  ~ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php70.plist

 

配置nginx

➜  ~ vi /usr/local/etc/nginx/servers/test.local.com.conf
    server {
        listen       80;
        server_name  test.local.com;
        root /Users/cony/Work/php/test;

        location / {
            index  index.html index.htm index.php;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

➜  ~ sudo nginx -s reload
➜  ~ sudo vi /etc/hosts # 添加 127.0.0.1 test.local.com

链接:http://www.jianshu.com/p/ae3f88d52e21

转载于:https://www.cnblogs.com/wangzehuaw/p/7832560.html

<think>好的,用户想在Mac上配置PHP项目所需的NginxPHPMySQLRedis环境。我需要根据提供的引用内容来组织回答。首先,用户可能有两种选择:使用Docker安装或者本地直接安装。引用里提到了两种方法,比如引用[2]和引用[3]是关于Docker的,而引用[1]和引用[4]是本地安装的步骤。我应该分两部分来回答:Docker方式和本地安装方式。 Docker方式可能更方便,尤其是对于不想在本地安装太多服务或者需要环境隔离的情况。需要详细说明每个步骤,比如安装Docker,然后拉取镜像,运行容器,并确保各个服务之间的连接,比如PHPMySQLRedis的链接。引用[2]和[3]中的命令可能需要整理,比如启动MySQLRedisPHP容器,然后配置Nginx。特别要注意Nginx的配置文件中fastcgi_pass指向PHP容器的名称和端口,比如myphp:9000,这需要确保容器名称正确,并且网络连接正常。此外,数据持久化也是需要考虑的,使用-v参数挂载本地目录到容器中,这样数据不会丢失。 本地安装的话,用户需要分别安装每个服务。比如通过Homebrew安装NginxPHPMySQLRedis,然后逐一配置。引用[1]提到在php.ini中添加extension=redis来启用Redis扩展,这需要用户安装php-redis扩展,可能通过pecl安装。配置Nginx的时候,需要修改server块,处理PHP请求,将fastcgi_pass指向本地的PHP-FPM端口,通常是127.0.0.1:9000。MySQL安装后需要初始化,设置root密码,并可能修改身份验证方式,比如使用mysql_native_password,避免新版本的问题。 用户可能遇到的问题包括服务启动失败,端口冲突,扩展未正确加载,或者配置文件错误。需要提醒他们检查服务状态,查看日志,以及确认配置文件的路径是否正确。比如PHPphp.ini位置可能因版本不同而不同,可以用php --ini来查找。另外,权限问题也是常见的,比如/usr/local/etc/nginx下的目录权限,或者MySQL的数据目录权限。 还要注意不同安装方式的优缺点。Docker更干净,容易管理,适合开发多个项目,但可能需要学习Docker的基本操作。本地安装更直接,适合长期使用或生产环境,但可能遇到依赖冲突。 最后,生成相关问题时要覆盖两种安装方式,可能的问题包括性能比较、常见错误解决、多版本管理、数据备份和迁移,以及如何添加其他扩展等。</think>### 分步配置指南 #### 方法一:使用Docker快速搭建(推荐) 1. **安装Docker** - 从[Docker官网](https://www.docker.com/)下载并安装Docker Desktop for Mac。 2. **启动MySQL容器** ```bash docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql ``` - 参数说明:`-p 3306:3306` 映射端口,`-e MYSQL_ROOT_PASSWORD=root` 设置root密码[^2]。 3. **启动Redis容器** ```bash docker run --name redis -p 6379:6379 -d redis ``` 4. **启动PHP-FPM容器** ```bash docker run --name myphp -v /本地项目路径:/var/www/html -d php:8.0-fpm ``` - 通过`-v`挂载本地PHP项目目录到容器内[^3]。 5. **配置Nginx容器** - 创建`nginx.conf`文件: ```nginx server { listen 80; server_name localhost; location ~ \.php$ { fastcgi_pass myphp:9000; # 关联PHP容器 fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } } ``` - 启动Nginx: ```bash docker run --name nginx -p 80:80 -v /本地nginx配置目录:/etc/nginx/conf.d --link myphp:php -d nginx ``` `--link`确保NginxPHP容器通信。 #### 方法二:本地手动安装 1. **安装Homebrew** ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 2. **安装Nginx** ```bash brew install nginx brew services start nginx # 启动服务 ``` 3. **安装PHP 8.0及扩展** ```bash brew install php@8.0 brew install php-redis # Redis扩展 echo 'extension=redis' >> /usr/local/etc/php/8.0/php.ini # 启用扩展[^1] ``` 4. **安装MySQL** ```bash brew install mysql brew services start mysql mysql_secure_installation # 初始化安全设置[^4] ``` 5. **安装Redis** ```bash brew install redis brew services start redis ``` 6. **配置Nginx支持PHP** - 编辑`/usr/local/etc/nginx/servers/php_project.conf`: ```nginx server { listen 80; root /Users/你的用户名/项目路径; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; # PHP-FPM默认端口 include fastcgi_params; } } ``` - 重启Nginx:`brew services restart nginx` #### 验证步骤 1. **PHP连接MySQL测试** ```php <?php $conn = new mysqli("localhost", "root", "root"); if ($conn->connect_error) die("MySQL连接失败"); echo "MySQL连接成功"; ``` 2. **PHP连接Redis测试** ```php <?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Redis连接状态:" . $redis->ping(); ``` ### 常见问题解决 - **PHP扩展未加载**:检查`php.ini`路径是否正确,使用`php --ini`查看加载的配置文件。 - **端口冲突**:通过`lsof -i :端口号`查找占用进程并终止。 - **Docker容器互通失败**:使用`docker network create`创建自定义网络,替代`--link`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值