前言

在mac 电脑下安装lnmp 环境,跑php服务。

安装包

  1. 链接: https://pan.baidu.com/s/1AD5LgZk22Ghs0bRW5XMd4w
  2. 提取码: 4t4d
  3. or 自己到官网上下载
  4. https://pecl.php.net/

准备

先检测安装mac 终端下有没有 brew

  1. brew -v

没有则安装

  1. /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

1、编译安装php7.4.12

1.1、解压文件

  1. tar xzvf php-7.4.12.tar.gz

1.2、编译

  1. ./configure --prefix=/usr/local/php7.4.12 --with-mysqli=mysqlnd --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-sqlite3 --with-freetype --with-jpeg --with-pic --with-zlib --enable-xml --enable-gd --enable-gd-jis-conv --disable-rpath --enable-bcmath --enable-shmop --enable-mbregex --enable-mbstring --with-iconv=/opt/homebrew/opt/libiconv --with-curl --with-bz2=/opt/homebrew/opt/bzip2 --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-inline-optimization --enable-fpm --with-xmlrpc --with-sodium --enable-soap --without-pear --enable-calendar --enable-ftp --enable-ctype --enable-exif --enable-phar --with-zip --enable-opcache
  2. make && make install

一般上面编译时都会出错,提示缺少依赖包

1.2.1、依赖安装

01、libxml2

  1. //直接安装
  2. brew install libxml2
  3. //or 重新安装
  4. brew reinstall libxml2
  5. //环境变量配置
  6. echo 'export PATH="/opt/homebrew/opt/libxml2/bin:$PATH"' >> ~/.zshrc
  7. export LDFLAGS="-L/opt/homebrew/opt/libxml2/lib"
  8. export CPPFLAGS="-I/opt/homebrew/opt/libxml2/include"
  9. export PKG_CONFIG_PATH="/opt/homebrew/opt/libxml2/lib/pkgconfig"

02、openssl

  1. //重新安装
  2. brew reinstall openssl@1.1
  3. //配置环境变量
  4. echo 'export PATH="/opt/homebrew/Cellar/openssl@1.1/1.1.1l/bin:$PATH"' >> ~/.zshrc
  5. export PKG_CONFIG_PATH="/opt/homebrew/Cellar/openssl@1.1/1.1.1l/lib/pkgconfig/":$PKG_CONFIG_PATH

03、sqlite3

  1. //安装
  2. brew reinstall sqlite3
  3. //配置
  4. echo 'export PATH="/opt/homebrew/Cellar/sqlite/3.36.0/bin:$PATH"' >> ~/.zshrc
  5. //export PKG_CONFIG_PATH="/opt/homebrew/Cellar/sqlite/3.36.0/lib/pkgconfig/":$PKG_CONFIG_PATH
  6. export PKG_CONFIG_PATH="/opt/homebrew/opt/libxml2/lib/pkgconfig":"/opt/homebrew/Cellar/openssl@1.1/1.1.1l/lib/pkgconfig":"/opt/homebrew/Cellar/sqlite/3.36.0/lib/pkgconfig"

04、zlib

  1. //安装
  2. brew reinstall zlib
  3. //配置
  4. export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"
  5. export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"
  6. export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"
  7. //修改版
  8. export LDFLAGS="-L/opt/homebrew/opt/libxml2/lib -L/opt/homebrew/opt/zlib/lib"
  9. export CPPFLAGS="-I/opt/homebrew/opt/libxml2/include -I/opt/homebrew/opt/zlib/include"
  10. export PKG_CONFIG_PATH="/opt/homebrew/opt/libxml2/lib/pkgconfig":"/opt/homebrew/Cellar/openssl@1.1/1.1.1l/lib/pkgconfig":"/opt/homebrew/Cellar/sqlite/3.36.0/lib/pkgconfig":"/opt/homebrew/opt/zlib/lib/pkgconfig"

05、bzip2

  1. //安装
  2. brew reinstall bzip2
  3. //配置
  4. echo 'export PATH="/opt/homebrew/opt/bzip2/bin:$PATH"' >> ~/.zshrc
  5. export LDFLAGS="-L/opt/homebrew/opt/bzip2/lib"
  6. export CPPFLAGS="-I/opt/homebrew/opt/bzip2/include"
  7. //修改版
  8. //找到安装目录
  9. /opt/homebrew/opt/bzip2
  10. 指定目录替换 重新编译
  11. ./configure --prefix=/usr/local/php7.4.12 --with-mysqli=mysqlnd --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-sqlite3 --with-freetype --with-jpeg --with-pic --with-zlib --enable-xml --enable-gd --enable-gd-jis-conv --disable-rpath --enable-bcmath --enable-shmop --enable-mbregex --enable-mbstring --with-iconv --with-curl --with-bz2=/opt/homebrew/opt/bzip2 --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-inline-optimization --enable-fpm --with-xmlrpc --with-sodium --enable-soap --without-pear --enable-calendar --enable-ftp --enable-ctype --enable-exif --enable-phar --with-zip --enable-opcache

06、curl

  1. //安装
  2. brew install curl
  3. //配置
  4. echo 'export PATH="/opt/homebrew/opt/curl/bin:$PATH"' >> ~/.zshrc
  5. export LDFLAGS="-L/opt/homebrew/opt/curl/lib"
  6. export CPPFLAGS="-I/opt/homebrew/opt/curl/include"
  7. export PKG_CONFIG_PATH="/opt/homebrew/opt/curl/lib/pkgconfig"

07、libpng

  1. brew install libpng

08、libjpeg

  1. brew install libjpeg

09、freetype2

  1. brew install freetype2

10、libiconv

  1. //安装
  2. brew install libiconv
  3. //配置
  4. echo 'export PATH="/opt/homebrew/opt/libiconv/bin:$PATH"' >> ~/.zshrc
  5. export LDFLAGS="-L/opt/homebrew/opt/libiconv/lib"
  6. export CPPFLAGS="-I/opt/homebrew/opt/libiconv/include"
  7. //重新编译
  8. ./configure --prefix=/usr/local/php7.4.12 --with-mysqli=mysqlnd --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-sqlite3 --with-freetype --with-jpeg --with-pic --with-zlib --enable-xml --enable-gd --enable-gd-jis-conv --disable-rpath --enable-bcmath --enable-shmop --enable-mbregex --enable-mbstring --with-iconv=/opt/homebrew/opt/libiconv --with-curl --with-bz2=/opt/homebrew/opt/bzip2 --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-inline-optimization --enable-fpm --with-xmlrpc --with-sodium --enable-soap --without-pear --enable-calendar --enable-ftp --enable-ctype --enable-exif --enable-phar --with-zip --enable-opcache
  9. //重新配置
  10. export PKG_CONFIG_PATH="/opt/homebrew/opt/libxml2/lib/pkgconfig":"/opt/homebrew/opt/openssl/lib/pkgconfig":"/opt/homebrew/opt/sqlite/lib/pkgconfig":"/opt/homebrew/opt/zlib/lib/pkgconfig":"/opt/homebrew/opt/curl/lib/pkgconfig":"/opt/homebrew/opt/libpng/lib/pkgconfig":"/opt/homebrew/opt/jpeg/lib/pkgconfig":"/opt/homebrew/opt/freetype/lib/pkgconfig"

11、oniguruma

  1. brew reinstall oniguruma

12、libsodium

  1. brew reinstall libsodium

13、libzip

  1. //安装
  2. brew reinstall libzip
  3. //配置
  4. export PKG_CONFIG_PATH="/opt/homebrew/opt/libxml2/lib/pkgconfig":"/opt/homebrew/opt/openssl/lib/pkgconfig":"/opt/homebrew/opt/sqlite/lib/pkgconfig":"/opt/homebrew/opt/zlib/lib/pkgconfig":"/opt/homebrew/opt/curl/lib/pkgconfig":"/opt/homebrew/opt/libpng/lib/pkgconfig":"/opt/homebrew/opt/jpeg/lib/pkgconfig":"/opt/homebrew/opt/freetype/lib/pkgconfig":"/opt/homebrew/opt/oniguruma/lib/pkgconfig":"/opt/homebrew/opt/libsodium/lib/pkgconfig":"/opt/homebrew/opt/libzip/lib/pkgconfig"

查看编译扩展安装、环境变量情况

  1. pkg-config --list-all
  2. echo $PKG_CONFIG_PATH

1.3、 安装

上面编译完,即可安装。

  1. make && make install

1.3.1、PHP 编译问题PEAR package PHP_Archive not installed的解决

在 —without-pear 跟上 —disable-phar 就可以解决这个问题了
./configure —without-pear —disable-phar

  1. //重新编译
  2. ./configure --prefix=/usr/local/php7.4.12 --with-mysqli=mysqlnd --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-sqlite3 --with-freetype --with-jpeg --with-pic --with-zlib --enable-xml --enable-gd --enable-gd-jis-conv --disable-rpath --enable-bcmath --enable-shmop --enable-mbregex --enable-mbstring --with-iconv=/opt/homebrew/opt/libiconv --with-curl --with-bz2=/opt/homebrew/opt/bzip2 --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-inline-optimization --enable-fpm --with-xmlrpc --with-sodium --enable-soap --without-pear --enable-calendar --enable-ftp --enable-ctype --enable-exif --disable-phar --with-zip --enable-opcache
  3. //安装
  4. make && make install

成功编译安装完成后,再安装pear

  1. wget http://pear.php.net/go-pear.phar
  2. /usr/local/php7.4.12/bin/php go-pear.phar
  3. /usr/local/php/bin/php go-pear.phar

2、安装nginx

  1. //安装
  2. brew install nginx
  3. //加载文件
  4. sudo nginx -c /usr/local/etc/nginx/nginx.conf
  5. //重新启动
  6. sudo nginx -s reload

安装成功后的部分字样

Docroot is: /opt/homebrew/var/www
The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /opt/homebrew/etc/nginx/servers/.

3、安装mysql

  1. //安装
  2. brew install mysql
  3. //操作命令
  4. $ mysql.server start
  5. $ mysql.server restart
  6. $ mysql.server stop
  7. $ mysql.server status
  8. $ mysql -u root -p

成功部分字样:

To connect run:
mysql -uroot

mysql@5.7 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH, run:
echo ‘export PATH=”/opt/homebrew/opt/mysql@5.7/bin:$PATH”‘ >> ü/.zshrc

For compilers to find mysql@5.7 you may need to set:
export LDFLAGS=”-L/opt/homebrew/opt/mysql@5.7/lib”
export CPPFLAGS=”-I/opt/homebrew/opt/mysql@5.7/include”

For pkg-config to find mysql@5.7 you may need to set:
export PKG_CONFIG_PATH=”/opt/homebrew/opt/mysql@5.7/lib/pkgconfig”

To start mysql@5.7:
brew services start mysql@5.7
Or, if you don’t want/need a background service you can just run:
/opt/homebrew/opt/mysql@5.7/bin/mysqld_safe —datadir=/opt/homebrew/var/mysql
==> Summary
🍺 /opt/homebrew/Cellar/mysql@5.7/5.7.35: 320 files, 233.8MB

4、安装memcached扩展

  1. //下载后,解压
  2. mkdir -p /usr/local/libmemcached # -p是强制递归创建目录
  3. // 遇到权限问题 执行
  4. sudo mkdir -p /usr/local/libmemcached
  5. // 输入密码后,进行解压
  6. tar zxvf libmemcached-1.0.18.tar.gz
  7. //解压后,编译
  8. ./configure --with-php-config=/usr/local/php7.4.12/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --with-zlib-dir=/opt/homebrew/opt/zlib
  9. //安装
  10. make && make install

5、安装redis与对应php-redis扩展

  1. //安装redis 服务软件
  2. brew install redis
  3. //找到redis扩展文件,进行解压
  4. tar zxvf redis-5.3.4.tgz
  5. //解压后,编译
  6. ./configure --with-php-config=/usr/local/php7.4.12/bin/php-config
  7. //安装
  8. make && make install

redis 服务软件安装成功后字样:

To start redis:
brew services start redis
Or, if you don’t want/need a background service you can just run:
/opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf
==> Summary
🍺 /opt/homebrew/Cellar/redis/6.2.5: 14 files, 2MB

redis php 扩展安装成功后字样:

Libraries have been installed in:
/Users/yhuser/Downloads/lnmp/redis-5.3.4/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR’
flag during linking and do at least one of the following:

  • add LIBDIR to the `DYLD_LIBRARY_PATH’ environment variable
    during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

Build complete.
Don’t forget to run ‘make test’.

Installing shared extensions: /usr/local/php7.4.12/lib/php/extensions/no-debug-non-zts-20190902/

==============全文结束================