本文介绍一种比较便捷的安装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源.png首先我们先把PHP环境下需要创建的用户创建完成
创建用户和组
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相关的扩展,模块名称和模块说明依次罗列了出来。基本扩展fpm,pdo,mongodb,都在这里可以找到。
yumphp72.png通过 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环境,当然是支持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 加载错误
在执行 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
检测验证
mysql.so.pngphp -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
查找对应的缺失扩展
根据扩展名称查找如下
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的内容。
遇到这种问题,首先查找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目录直接执行 phpize命令,就会出现以上,查找不到 config.m4的错误。总结如下
编译php扩展出现错误:Cannot find config.m4
原因:当前目录没在扩展的目录下
再次执行 安装命令
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。
systemctl-centos7.pngsystemd的设计目标是,为系统的启动和管理提供一套完整的解决方案。
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
chmod-php.png
systemctl-php-fpm.png
查看所有启动的服务
systemctl list-units --type=service
systemctl-list.png
如图,我们可以 看到服务名称php-fpm.service 和crond.service
查看服务状态
systemctl status php-fpm
systemctl-status.png
查找php-fpm的运行用户
首先使用grep 命令查看详情
centos-php.pngps -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两个服务为例
查看服务状态
service php-fpm status
php-fpm (pid 25929) is running...
sudo service nginx status
nginx (pid 25763) is running...
查看服务所属运行用户
使用grep 命令和查看配置文件两种方式
ps -ef |grep nginx
php-grep.png
从命令的结果我们可以跟踪到Nginx 服务的主配置文件 /etc/nginx/nginx.conf,同时每一行的第一列就是服务的所属用户UID
重启系统,服务自动重启
由于权限写入
参考
CentOS7中systemctl的使用与CentOS6中service的区别
网友评论