MacPorts使用(05) 安装PHP7.4

2,323 阅读8分钟

参考: MacPorts Wiki HowTo PHP

MacPorts 包含以下版本的PHP,你可以安装其中的一个、多个或者全部的版本

  • php74: PHP v7.4.x. 最新的稳定版本. 安全更新到2022年11月28日
  • php73: PHP v7.3.x. 较旧的稳定版本. 功能更新到2020年12月6日. 安全更新到2021年12月6日.
  • php72: PHP v7.2.x. 较旧的稳定版本. 功能更新到2019年11月30日. 安全更新到2020年11月30.
  • php71: PHP v7.1.x. 较旧的稳定版本. 功能更新到2018年12月1日. 安全更新到2020年12月1
  • php70: PHP v7.0.x. 该版本于2019年1月10日到期,因此不建议使用.
  • php56: PHP v5.6.x. 该版本于2019年1月10日到期,因此不建议使用.
  • php55: PHP v5.5.x. 该版本于2018年12月31日到期,因此不建议使用.
  • php54: PHP v5.4.x. 该版本于2015年不再更新维护,因此不建议使用.
  • php53: PHP v5.3.x. 该版本于2014年不再更新维护,因此不建议使用.
  • php52: PHP v5.2.x. 该版本于2011年不再更新维护,因此不建议使用.

Step 1. 安装PHP SAPIs (服务API)

SAPI Port名称 安装命令
Command line php74 sudo port install php74
Apache 2 module php74-apache2handler sudo port install php74-apache2handler
FastCGI php74-cgi sudo port install php74-cgi
FastCGI Process Manager php74-fpm sudo port install php74-fpm

这里,我们安装 php74 和 php74-fpm

i. 安装 php74

sudo port install php74

安装完成后会提示:

php74 has the following notes:
    To customize php74, copy /opt/local/etc/php74/php.ini-development (if this
    is a development server) or /opt/local/etc/php74/php.ini-production (if this
    is a production server) to /opt/local/etc/php74/php.ini and then make
    changes. 

如果是开发环境,可以将/opt/local/etc/php74/php.ini-development,拷贝到/opt/local/etc/php74/php.ini

如果是生产环境,可以将/opt/local/etc/php74/php.ini-production拷贝到/opt/local/etc/php74/php.ini

然后修改 /opt/local/etc/php74/php.ini 配置文件,来修改PHP的配置

ii. 安装 php-fpm

sudo port install php74-fpm

安装完成后会有提示:

php74-fpm has the following notes:
    If this is your first install, you need to enable php74-fpm in your web
    server.
    To use php74-fpm, copy /opt/local/etc/php74/php-fpm.conf.default to
    /opt/local/etc/php74/php-fpm.conf and make changes if desired.

    A startup item has been generated that will aid in starting php74-fpm with
    launchd. It is disabled by default. Execute the following command to start
    it, and to cause it to launch at startup:
    
        sudo port load php74-fpm

根据提示,先将 /opt/local/etc/php74/php-fpm.conf.default 复制到 /opt/local/etc/php74/php-fpm.conf,然后在此文件上做配置修改

如果要启动 php74-fpm,使用以下命令:

sudo port load php74-fpm

iii. 如果是已经安装其他版本PHP,可以使用以下命令进行切换

  1. 查看当前php版本

    php -v
    

    php版本信息:

    PHP 5.6.40 (cli) (built: Oct 30 2019 15:38:57) 
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
        with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    
  2. 查看是否php-fpm是否已启动

    ps -ax|grep php-fpm 
    

    php-fpm进程信息

     2319 ??         0:00.23 /opt/local/sbin/php-fpm56
     2320 ??         0:00.00 /opt/local/sbin/php-fpm56
     2321 ??         0:00.00 /opt/local/sbin/php-fpm56
    
  3. 停止php-fpm

    sudo port unload php56-fpm
    

    停止成功时提示:

---> Unloading startupitem 'php56-fpm' for php56-fpm ```

  1. 切换php版本

    sudo port select --set php php74
    

    切换成功时提示:

    Selecting 'php74' for 'php' succeeded. 'php74' is now active.
    
  2. 启动php-fpm

    sudo port load php74-fpm
    

    启动成功时提示:

    --->  Loading startupitem 'php74-fpm' for php74-fpm
    
  3. 查看php版本和php-fpm进程是否启动

    # 查看当前php版本
    php -v
    
    PHP 7.4.4 (cli) (built: Mar 26 2020 16:26:41) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
    
    # 查看php-fpm进程是否成功启动
    ps -ax|grep php-fpm
    
    3006 ??         0:00.04 /opt/local/sbin/php-fpm74
    

3007 ?? 0:00.00 /opt/local/sbin/php-fpm74 3008 ?? 0:00.00 /opt/local/sbin/php-fpm74 ```

【Notice】,如果出现 sudo port load php74-fpm之后,php-fpm进程无法启动的情况,可以使用以下命令查看日志:

sudo tail -f /opt/local/var/log/php74/php-fpm.log

如果出现以下情况的报错:

[08-Apr-2020 10:17:47] ERROR: failed to post process the configuration
[08-Apr-2020 10:17:47] ERROR: FPM initialization failed
[08-Apr-2020 10:17:57] ERROR: No pool defined. at least one pool section must be specified in config file

解决方案如下:

进入PHP安装目录
cd /opt/local/etc/php74/php-fpm.d 

拷贝默认配置文件
sudo cp www.conf.default www.conf

Step 2. 安装PHP 扩展模块

MacPorts支持安装各种PHP扩展,具体支持的扩展可以使用以下命令查看:

port search --name --line --regex '^php74'

以下为MacPorts支持的PHP7.4的扩展,可以根据需要进行安装:

php74-apache2handler	7.4.4	lang www	php74 Apache 2 Handler SAPI
php74-APCu	5.1.18	php devel	APC User Cache
php74-calendar	7.4.4	php	a PHP extension for converting between different calendar formats
php74-cgi	7.4.4	lang www	php74 CGI SAPI
php74-curl	7.4.4	php net www	a PHP interface to the curl library, which lets you download files from servers with a variety of protocols
php74-dba	7.4.4	php databases	a PHP interface for accessing DBM databases such as BerkeleyDB
php74-dbase	7.0.1	php databases	a PHP interface for accessing dBase databases
php74-enchant	7.4.4	php textproc devel	a PHP interface to enchant
php74-event	2.5.4	php devel	efficiently schedule I/O, time and signal based events
php74-excel	1.0.2	php textproc	PHP interface to LibXL for reading and writing Microsoft Excel spreadsheets
php74-exif	7.4.4	php graphics	a PHP interface to the EXIF image metadata functions
php74-fpm	7.4.4	lang www	php74 FPM SAPI
php74-ftp	7.4.4	php net	a PHP extension for accessing file servers using the File Transfer Protocol
php74-gd	7.4.4	php graphics	a PHP interface to the gd library
php74-geoip	1.1.1	php devel	Map IP address to geographic places
php74-gettext	7.4.4	php devel	a PHP interface to the gettext natural language support functions
php74-gmagick	2.0.5RC1	php devel	Provides a wrapper to the GraphicsMagick library
php74-gmp	7.4.4	php devel math	a PHP interface to GMP, the GNU multiprocessing library through which you can work with arbitrary-length integers
php74-iconv	7.4.4	php textproc	a PHP interface to the libiconv character encoding conversion functions
php74-igbinary	3.1.2	php net devel	PHP serializer.
php74-imagick	3.4.4	php graphics	PHP extension to create and modify images with ImageMagick
php74-imap	7.4.4	php mail	a PHP interface to the IMAP protocol
php74-intl	7.4.4	php devel	internationalization extension for PHP
php74-ipc	7.4.4	php	interprocess communication extensions for PHP
php74-jsmin	3.0.0	php devel	PHP API for minifying and uglifying JavaScript
php74-ldap	7.4.4	php databases	a PHP interface to LDAP
php74-lzf	1.6.7	php devel	Handles LZF compression / decompression.
php74-mailparse	3.0.4	php mail devel	Email message manipulation
php74-mbstring	7.4.4	php textproc	a PHP extension for manipulating strings in multibyte encodings
php74-mcrypt	1.0.3	php security	a PHP interface to the mcrypt library, which offers a wide variety of algorithms
php74-memcached	3.1.5	php net devel	PHP bindings for memcache
php74-mongodb	1.7.4	php databases devel	MongoDB Database Driver
php74-mysql	7.4.4	php databases	a PHP interface to MySQL databases, including the mysqli and pdo_mysql extensions
php74-mysql-xdevapi	8.0.18	php devel	MySQL X DevAPI for PHP
php74-odbc	7.4.4	php databases	a PHP interface for accessing databases via Open DataBase Connectivity (ODBC)
php74-opcache	7.4.4	php	OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.
php74-openssl	7.4.4	php devel security	a PHP interface to OpenSSL signature-generation and -verification and data-encryption and -decryption functions
php74-oracle	7.4.4	php databases	a PHP interface to Oracle, including the oci8 and pdo_oci extensions
php74-pcntl	7.4.4	php sysutils	a PHP interface to Unix-style process creation, program execution, signal handling and process termination functions
php74-pdflib	4.1.4	php print textproc	PHP bindings for pdflib
php74-phalcon4	4.0.5	php	full stack PHP framework written as an extension
php74-posix	7.4.4	php sysutils	a PHP interface to additional POSIX functions
php74-postgresql	7.4.4	php databases	a PHP interface to PostgreSQL, including the pgsql and pdo_pgsql extensions
php74-propro	2.1.0	php	a reusable property proxy API
php74-pspell	7.4.4	php textproc	a PHP interface to the aspell library, which lets you check spelling and offer spelling suggestions
php74-psr	1.0.0	php	interfaces to PHP Standards Recommendations
php74-raphf	2.0.1	php	a reusable persistent handle and resource factory API
php74-redis	5.2.1	php databases	an API for communicating with a Redis database from PHP
php74-scrypt	1.4.2	php security	a PHP wrapper for scrypt
php74-snmp	7.4.4	php sysutils	a PHP interface to the Simple Network Management Protocol (SNMP)
php74-soap	7.4.4	php net	a PHP extension for writing SOAP clients and servers
php74-sockets	7.4.4	php net	a PHP interface to BSD socket communication functions
php74-sodium	7.4.4	php security	a PHP interface to libsodium, a modern and easy-to-use crypto library
php74-sqlite	7.4.4	php databases	a PHP interface to SQLite, including the sqlite3 and pdo_sqlite extensions
php74-sqlsrv	5.8.0	php databases	Microsoft sqlsrv drivers for PHP
php74-ssh2	1.2	php net	PHP bindings for libssh2
php74-stomp	2.0.2	php devel	PECL extension of stomp client
php74-svm	0.2.3	php math	PHP bindings for libsvm, a support vector machine implementation
php74-swoole	4.4.15	php net devel	an event-driven asynchronous & concurrent & coroutine networking engine with high performance for PHP
php74-taint	2.0.6	php	detects XSS and SQL-injection vulnerabilities
php74-tidy	7.4.4	php www	a PHP interface to tidy, the HTML cleaning and repair utility
php74-timezonedb	2019.3	php devel	A PECL Timezone Database.
php74-uploadprogress	1.1.3	php www devel	An extension to track progress of a file upload.
php74-uuid	1.0.5	php net www	A wrapper around libuuid from the ext2utils project.
php74-vld	0.17.0	php devel	Dump the internal representation of PHP scripts
php74-xdebug	2.9.4	php net devel	php debugging extension
php74-xmlrpc	7.4.4	php textproc	a PHP extension for writing XML-RPC clients and servers
php74-xsl	7.4.4	php textproc	a PHP interface to libxslt, which implements the XSL standard and lets you perform XSL transformations
php74-yaf	3.1.4	php net devel	a fast php framework written in c, built in php-ext
php74-yaml	2.0.4	php devel	a PHP interface to the YAML parsing library
php74-yaz	1.2.3	php net databases devel	PHP/PECL extension for the Z39.50 protocol
php74-zip	1.18.2	php archivers	PHP zip functions

以安装php-mysql扩展为例

sudo port install php74-mysql
--->  Computing dependencies for php74-mysql
--->  Fetching archive for php74-mysql
--->  Attempting to fetch php74-mysql-7.4.4_0+mysqlnd.darwin_19.x86_64.tbz2 from https://pek.cn.packages.macports.org/macports/packages/php74-mysql
--->  Attempting to fetch php74-mysql-7.4.4_0+mysqlnd.darwin_19.x86_64.tbz2.rmd160 from https://pek.cn.packages.macports.org/macports/packages/php74-mysql
--->  Installing php74-mysql @7.4.4_0+mysqlnd
--->  Activating php74-mysql @7.4.4_0+mysqlnd
--->  Cleaning php74-mysql
--->  Scanning binaries for linking errors
--->  No broken files found.                             
--->  No broken ports found.
--->  Some of the ports you installed have notes:
  php74-mysql has the following notes:
    To use mysqlnd with a local MySQL server, edit /opt/local/etc/php74/php.ini and set mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to the
    path to your MySQL server's socket file.
    
    For mysql5, use /opt/local/var/run/mysql5/mysqld.sock
    For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
    For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
    For mysql56, use /opt/local/var/run/mysql56/mysqld.sock
    For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
    For percona, use /opt/local/var/run/percona/mysqld.sock

其他常用扩展:

sudo port install php74-calendar php74-curl php74-dba php74-enchant php74-excel php74-ftp php74-gettext	php74-gmp php74-iconv php74-imap php74-mcrypt php74-memcached php74-mongodb php74-odbc php74-opcache php74-openssl php74-pcntl php74-redis php74-snmp php74-soap php74-sockets php74-sqlite php74-tidy php74-zip

安装完成后,重启PHP-FPM