從Yum安裝PHP7.2環境想到的
- 2019 年 10 月 6 日
- 筆記
本文介紹一種比較便捷的安裝PHP環境的方法-yum安裝,這種安裝方式相比源碼安裝PHP,更加方便快捷。
使用yum安裝PHP的前提是分清本身作業系統版本和想安裝的PHP版本,版本的匹配和鏡像源兼容是環境安裝成功的關鍵。
本文以centos7和PHP7.2,沒有安裝過PHP環境的一台伺服器為例來說明。
epel源
epel 是 Extra Packages for Enterprise Linux (EPEL),網上相關資料提示 更新yum源,就是基於epel的。
參考網址 https://fedoraproject.org/wiki/EPEL#Quickstart

通過yum方式安裝PHP
第一步依然是尋找適合的版本
yum search php72
如圖所示,命令會列出所有與php7.2相關的擴展,模組名稱和模組說明依次羅列了出來。基本擴展fpm,pdo,mongodb,都在這裡可以找到。

常見擴展
以下是search php72 顯示的擴展,常見擴展用不同的顯示塊進行了標記,表示已安裝。
php72w-common.x86_64y Installed: php72w-common.x86_64 0:7.2.21-1.w7
yum install php72w-cli.x86_64
Installed: php72w-cli.x86_64 0:7.2.21-1.w7
Dependency Installed: libargon2.x86_64 0:20161029-3.el7
php72w-fpm.x86_64 : PHP FastCGI Process Manager
Running transaction Installing : php72w-fpm-7.2.21-1.w7.x86_64 1/1 Verifying : php72w-fpm-7.2.21-1.w7.x86_64 1/1
Installed: php72w-fpm.x86_64 0:7.2.21-1.w7
php72w-mysql.x86_64 : A module for PHP applications that use MySQL databases
Installed: php72w-mysql.x86_64 0:7.2.21-1.w7
Dependency Installed: php72w-pdo.x86_64 0:7.2.21-1.w7
yum install php72w-devel.x86_64
php72w-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php72w-cli.x86_64 : Command-line interface for PHP
php72w-common.x86_64 : Common files for PHP
php72w-dba.x86_64 : A database abstraction layer module for PHP applications php72w-devel.x86_64 : Files needed for building PHP extensions
php72w-embedded.x86_64 : PHP library for embedding in applications
php72w-enchant.x86_64 : Enchant spelling extension for PHP applications
php72w-fpm.x86_64 : PHP FastCGI Process Manager
php72w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php72w-imap.x86_64 : A module for PHP applications that use IMAP php72w-interbase.x86_64 : A module for PHP applications that use Interbase/Firebird databases
php72w-intl.x86_64 : Internationalization extension for PHP applications php72w-ldap.x86_64 : A module for PHP applications that use LDAP
php72w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php72w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php72w-mysqlnd.x86_64 : A module for PHP applications that use MySQL
databases php72w-odbc.x86_64 : A module for PHP applications that use ODBC databases
php72w-opcache.x86_64 : An opcode cache Zend extension
php72w-pdo.x86_64 : A database access abstraction module for PHP applications
php72w-pdo_dblib.x86_64 : MSSQL database module for PHP
php72w-pear.noarch : PHP Extension and Application Repository framework
php72w-pecl-apcu.x86_64 : APCu – APC User Cache php72w-pecl-apcu-devel.x86_64 : APCu developer files (header)
php72w-pecl-geoip.x86_64 : Extension to map IP addresses to geographic places
php72w-pecl-igbinary.x86_64 : Replacement for the standard PHP serializer
php72w-pecl-igbinary-devel.x86_64 : Igbinary developer files (header)
php72w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
php72w-pecl-imagick-devel.x86_64 : Imagick developer files (header)
php72w-pecl-libsodium.x86_64 : Wrapper for the Sodium cryptographic library
php72w-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php72w-pecl-mongodb.x86_64 : PECL package MongoDB driver
php72w-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
php72w-pecl-xdebug.x86_64 : PECL package for debugging PHP scripts php72w-pgsql.x86_64 : A PostgreSQL database module for PHP
php72w-phpdbg.x86_64 : Interactive PHP debugger php72w-process.x86_64 : Modules for PHP script using system process interfaces
php72w-pspell.x86_64 : A module for PHP applications for using pspell interfaces
php72w-recode.x86_64 : A module for PHP applications for using the recode library
php72w-snmp.x86_64 : A module for PHP applications that query SNMP-managed devices
php72w-soap.x86_64 : A module for PHP applications that use the SOAP protocol
常見基礎命令
使用yum 方式安裝完成的php環境,當然是支援php 命令行常見命令的,比如
查看配置文件基本資訊
php --ini
php7以後主配置文件和擴展文件是分開的
Configuration File (php.ini) Path: /etc Loaded Configuration File: /etc/php.ini Scan for additional .ini files in: /etc/php.d Additional .ini files parsed: /etc/php.d/bcmath.ini, /etc/php.d/bz2.ini, /etc/php.d/calendar.ini, /etc/php.d/ctype.ini, /etc/php.d/curl.ini, /etc/php.d/dom.ini, /etc/php.d/exif.ini, /etc/php.d/fileinfo.ini, /etc/php.d/ftp.ini, /etc/php.d/gd.ini, /etc/php.d/gettext.ini, /etc/php.d/gmp.ini, /etc/php.d/iconv.ini, /etc/php.d/igbinary.ini, /etc/php.d/imagick.ini,
查看擴展模組載入情況
grep 擴展名稱
php -m |grep mongodb [root@10-9-77-82 ~]# php -m |grep mongodb
mongodb
查看PHP 擴展目錄
php-config --extension-dir
pdo_mysql.so 載入錯誤
在執行 php -ini 時遇到以下錯誤,提示未能載入mysqli.so和pdo_mysql.so.so
PHP Warning: PHP Startup: Unable to load dynamic library 'mysqli.so' (tried: /usr/lib64/php/modules/mysqli.so (libmysqlclient.so.18: cannot open shared object file: No such file or directory), /usr/lib64/php/modules/mysqli.so.so (/usr/lib64/php/modules/mysqli.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_mysql.so' (tried: /usr/lib64/php/modules/pdo_mysql.so (libmysqlclient.so.18: cannot open shared object file: No such file or directory), /usr/lib64/php/modules/pdo_mysql.so.so (/usr/lib64/php/modules/pdo_mysql.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
載入相關模組,解決載入 mysql.so 報錯
yum install php72w-mysql.x86_64
檢測驗證
php -m |grep mysql

什麼是 systemctl
systemctl 是一種Linux服務管理的方式。
從CentOS 7.x開始,CentOS開始使用systemd服務來代替daemon。

systemd的設計目標是,為系統的啟動和管理提供一套完整的解決方案。
systemctl是 Systemd 的主命令,用於管理系統
根據 Linux 慣例,字母d是守護進程(daemon)的縮寫。Systemd 這個名字的含義,就是它要守護整個系統
關於systemctl詳細的介紹請移步這裡
Systemd 入門教程:命令篇 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html Systemd 入門教程:實戰篇 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
認識 /usr/lib/systemd/system
對於那些支援 Systemd 的軟體,安裝的時候,會自動在/usr/lib/systemd/system目錄添加一個配置文件,我們cd 到這個目錄下,找到php-fpm.service文件,看看內容如下
cd /usr/lib/systemd/system [root@10-9-77-82 system]# cat php-fpm.service [Unit] Description=The PHP FastCGI Process Manager After=syslog.target network.target [Service] Type=notify PIDFile=/var/run/php-fpm/php-fpm.pid EnvironmentFile=/etc/sysconfig/php-fpm ExecStart=/usr/sbin/php-fpm --nodaemonize --fpm-config /etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
以下是systemctl 常用命令
啟動服務
systemctl start php-fpm


查看所有啟動的服務
systemctl list-units --type=service

如圖,我們可以看到服務名稱php-fpm.service 和crond.service
查看服務狀態
systemctl status php-fpm

總結
yum是一個軟體包管理器,相比源碼編譯安裝,yum安裝方式更加方便,快捷,可以自動解決軟體包之間的依賴關係。
本文中的操作示例,換做不同的作業系統,和不同的php版本,或者nginx,mysql,對應的包,源,都會有變化。
yum安裝軟體,使用者不需要指定安裝目錄,也就是說沒法控制yum軟體包的安裝目錄。
而我們需要理解的是yum的使用套路,首先使用search 命令找到合適的源,然後安裝,尋找配置文件,啟動服務。運行過程中,有修改,再針對性的安裝或者調整。
對於centos7 管理軟體服務的不同,就像這篇文章中(https://blog.csdn.net/u012834750/article/details/80501440) 提到的,centos7中的命令大變樣,會不會覺得之前學習的命令都用不上了,使用者總是得擁抱變化,學習新的方式,所以說命令總是記不完的,對於新東西,學習要抓住核心本質。
技術文章,還請點擊閱讀全文,獲取更好的閱讀體驗。技術文章太燒腦,一首鼓樓,放鬆一下。