本文介紹一種比較便捷的安裝PHP環(huán)境的方法-yum安裝,這種安裝方式相比源碼安裝PHP,更加方便快捷。
使用yum安裝PHP的前提是分清本身操作系統(tǒng)版本和想安裝的PHP版本,版本的匹配和鏡像源兼容是環(huán)境安裝成功的關鍵。
本文以centos7和PHP7.2,沒有安裝過PHP環(huán)境的一臺服務器為例來說明。
epel 源
epel 是 Extra Packages for Enterprise Linux (EPEL),網(wǎng)上相關資料提示 更新yum源,就是基于epel的。
參考網(wǎng)址 https://fedoraproject.org/wiki/EPEL#Quickstart
首先我們先把PHP環(huán)境下需要創(chuàng)建的用戶創(chuàng)建完成
創(chuàng)建用戶和組
groupadd php-fpm
groupadd nginx
groupadd mysql
useradd -g php-fpm php-fpm
useradd -g nginx nginx
useradd -g mysql mysql
通過yum方式安裝PHP
第一步依然是尋找適合的版本
yum search php72
如圖所示,命令會列出所有與php7.2相關的擴展,模塊名稱和模塊說明依次羅列了出來?;緮U展fpm,pdo,mongodb,都在這里可以找到。
通過 yum install 模塊名稱,來安裝所需要的模塊
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環(huán)境,當然是支持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,
查看擴展模塊加載情況
通過php -m |grep 模塊名稱 來查看模塊的加載情況
如 查看mongodb的安裝情況
[root@10-9-77-82 ~]# php -m |grep mongodb
mongodb
查看PHP 擴展目錄
php-config --extension-dir
pdo_mysql.so 加載錯誤
在執(zhí)行 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
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
查找對應的缺失擴展
根據(jù)擴展名稱查找如下
yum search mbstring
php-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php55w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php56w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php70w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php71w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php72w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
yum install php72w-mbstring.x86_64
Running transaction
Installing : php72w-mbstring-7.2.21-1.w7.x86_64 1/1
Verifying : php72w-mbstring-7.2.21-1.w7.x86_64 1/1
Installed:
php72w-mbstring.x86_64 0:7.2.21-1.w7
編譯安裝 Nginx
tar -zxvf nginx-1.16.1.tar.gz
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-threads --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_stub_status_module --conf-path=/etc/nginx/conf/nginx.conf . --error-log-path=/var/log/nginx/error.log --sbin-path=/usr/local/nginx/sbin/nginx
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/etc/nginx/conf"
nginx configuration file: "/etc/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
mkdir -p /etc/nginx/conf
mkdir -p /var/log/nginx/
whereis redis-server
whereis redis-cli
驗證nginx 是否成功
.驗證Nginx是否開啟成功
phpize運行異常
使用pecl install swool 安裝擴展 Swool顯示如下問題
Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command.
這個需要 上文中擴展列表的php72w-devel.x86_64模塊
php72w-devel.x86_64 : Files needed for building PHP extensions
phpize是用來擴展php擴展模塊的,通過phpize可以建立php的外掛模塊,phpize 是屬于php-devel的內(nèi)容。
遇到這種問題,首先查找phpize位置
which phpize
再次核對當前的php-devel版本
yum info php-devel
Available Packages
Name : php-devel
Arch : x86_64
Version : 5.4.16
Release : 46.el7
Size : 602 k
Repo : base/7/x86_64
Summary : Files needed for building PHP extensions
URL : http://www.php.net/
License : PHP and Zend and BSD
Description : The php-devel package contains the files needed for building PHP
: extensions. If you need to compile your own PHP extensions, you will
: need to install this package.
The phpize command is meant to be run at the top level of an extension source dir (this source dir should contain a file name config.m4).
如果通過源碼編譯的方式 安裝PHP ,安裝擴展的基本步驟是這樣的
$ cd extname
$ phpize
$ ./configure
$ make
# make install
沒有源碼的情況下,在phpize目錄直接執(zhí)行 phpize命令,就會出現(xiàn)以上,查找不到 config.m4的錯誤??偨Y如下
編譯php擴展出現(xiàn)錯誤:Cannot find config.m4
原因:當前目錄沒在擴展的目錄下
再次執(zhí)行 安裝命令
pecl install swool
安裝成功
Build process completed successfully
Installing '/usr/lib64/php/modules/swoole.so'
Installing '/usr/include/php/ext/swoole/config.h'
install ok: channel://pecl.php.net/swoole-4.4.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=swoole.so" to php.ini
移除 mariadb
rpm -qa |grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
什么是 systemctl
systemctl 是一種Linux服務管理的方式
從CentOS 7.x開始,CentOS開始使用systemd服務來代替daemon。
systemd的設計目標是,為系統(tǒng)的啟動和管理提供一套完整的解決方案。
systemctl是 Systemd 的主命令,用于管理系統(tǒng)
根據(jù) Linux 慣例,字母d是守護進程(daemon)的縮寫。 Systemd 這個名字的含義,就是它要守護整個系統(tǒng)
關于systemctl詳細的介紹請移步這里
Systemd 入門教程:命令篇 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
Systemd 入門教程:實戰(zhàn)篇 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文件,看看內(nèi)容如下
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
查看服務狀態(tài)
systemctl status php-fpm
查找php-fpm的運行用戶
首先使用grep 命令查看詳情
ps -ef |grep php
每一行的第一列就是所屬用戶UID
為了解決問題
The directory is not writable by the Web process: /home/www/basic/web/assets
cat /etc/php-fpm.conf
最后一行有一個包含文件
include=/etc/php-fpm.d/*.conf
cd /etc/php-fpm.d/
ls
cat www.conf
; Start a new pool named 'www'.
[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache
chown -R apache assets/
用戶是apache
以下 我們以Nginx 和PHP-FPM兩個服務為例
查看服務狀態(tài)
service php-fpm status
php-fpm (pid 25929) is running...
sudo service nginx status
nginx (pid 25763) is running...
查看服務所屬運行用戶
使用grep 命令和查看配置文件兩種方式
ps -ef |grep nginx
從命令的結果我們可以跟蹤到Nginx 服務的主配置文件 /etc/nginx/nginx.conf,同時每一行的第一列就是服務的所屬用戶UID
重啟系統(tǒng),服務自動重啟
由于權限寫入
參考
CentOS7中systemctl的使用與CentOS6中service的區(qū)別