Centos php环境部署
Centos php环境部署
一. yum安装
1.1 yum源管理工具
yum-utils,减化了源管理配置的复杂度.
我们不用手工编写类似这样的配置文件.
yum -y install yum-utils
yum-config-manager管理yum存储库目录/etc/yum.repos.d/
,对目录增删改查.
查询仓库内容:
yum repolist all # 显示所有资源库
yum repolist enabled # 显示所有已启动的资源库
yum repolist disabled # 显示所有被禁用的资源库
1.1.1 添加repository
yum-config-manager --add-repo repository_url
1.1.2 禁用/启用
yum-config-manager --disable itemName
yum-config-manager --enable itemName
1.1.3 删除
直接删除/etc/yum.repos.d/
对应的库就可以.
1.2 yum源
centos7 默认PHP版本都很低,很少人使用自带的源.
https://wiki.centos.org/zh/AdditionalResources/Repositories
网上维护的常用源有:
- epel-release
- remi
- webtatic
- ius
1.2.1 官方源
centos官方源
CentOS 官方下载地址:https://www.centos.org/download/
阿里源: https://developer.aliyun.com/mirror/centos
官方镜像:
http://mirror.centos.org/centos-7/7/os/x86_64/Packages/
rpm -Uvh http://mirror.centos.org/centos-7/7/os/x86_64/Packages/xxx
修改为阿里源:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
或
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
1.2.2 epel-release源
redhat系的第一个源,由Fedora维护. 官方包不包含的包往往在这里个源都能找到.
其他下载: https://pkgs.org/download/epel-release
安装:
yum install epel-release
直接安装:
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
下载地址: https://dl.fedoraproject.org/pub/epel
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
1.2.3 remi源
上面都官方全量源,php的版本都不是最新的.
Remi repository是包含最新版本PHP和MySQL包的Linux源,由Remi提供维护。
建议用remi源来安装php
官网: https://rpms.remirepo.net
国内镜像: https://mirrors.tuna.tsinghua.edu.cn/remi/
阿里云: https://developer.aliyun.com/mirror/remi
操作指导: https://rpms.remirepo.net/wizard/
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
#或阿里云的
yum install https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
#
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
复制地址
安装结果:
禁用其他php版本,避免污染:
yum-config-manager --disable 'remi-php*'
yum-config-manager --enable remi-php81
# 查看
yum repolist
yum update
# 安装php
yum install php81
# 或指定源安装
yum --enablerepo=remi-php81 install php php-fpm -y
# 查看php版本
php -v
php -m
# 启用fpm
yum install php php-fpm
systemctl start php-fpm
systemctl enable php-fpm
1.2.4 webtatic
主要提供web包,但国内经常不可访问,不建议使用
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
1.2.5 ius
ius主要提供最新的php,git等版本,试验版本.
如果是尝鲜可以使用
官网: https://ius.io/
安装:
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
综合最可靠的源是remi.
1.3 yum安装
下面以remi源为示例:
安装php 基础支持程序
yum install php
卸载php
# remove 卸载不干净
yum remove php
# rpm 卸载
rpm -qa | grep php | xargs rpm -e --nodeps
依赖三个包:
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
php x86_64 8.1.10-1.el7.remi remi-php81 1.9 M
Installing for dependencies:
php-cli x86_64 8.1.10-1.el7.remi remi-php81 5.9 M
php-common x86_64 8.1.10-1.el7.remi remi-php81 1.2 M
php-sodium x86_64 8.1.10-1.el7.remi remi-php81 95 k
对应的包:
(1/4): php-8.1.10-1.el7.remi.x86_64.rpm | 1.9 MB 00:00:01
(2/4): php-sodium-8.1.10-1.el7.remi.x86_64.rpm | 95 kB 00:00:02
(3/4): php-cli-8.1.10-1.el7.remi.x86_64.rpm | 5.9 MB 00:00:08
(4/4): php-common-8.1.10-1.el7.remi.x86_64.rpm | 1.2 MB 00:00:19
安装路径:
[root@serv-101 ~]# whereis php
php: /usr/bin/php /usr/lib64/php /etc/php.d /etc/php.ini /usr/share/php /usr/share/man/man1/php.1.gz
所有文件路径:
[root@serv-101 ~]# rpm -qa | grep php |xargs rpm -ql
/etc/php-zts.d
/etc/php-zts.d/20-bz2.ini
/etc/php-zts.d/20-calendar.ini
/etc/php-zts.d/20-ctype.ini
/etc/php-zts.d/20-curl.ini
/etc/php-zts.d/20-exif.ini
/etc/php-zts.d/20-fileinfo.ini
/etc/php-zts.d/20-ftp.ini
/etc/php-zts.d/20-gettext.ini
/etc/php-zts.d/20-iconv.ini
/etc/php-zts.d/20-phar.ini
/etc/php-zts.d/20-sockets.ini
/etc/php-zts.d/20-tokenizer.ini
/etc/php.d
/etc/php.d/20-bz2.ini
/etc/php.d/20-calendar.ini
/etc/php.d/20-ctype.ini
/etc/php.d/20-curl.ini
/etc/php.d/20-exif.ini
/etc/php.d/20-fileinfo.ini
/etc/php.d/20-ftp.ini
/etc/php.d/20-gettext.ini
/etc/php.d/20-iconv.ini
/etc/php.d/20-phar.ini
/etc/php.d/20-sockets.ini
/etc/php.d/20-tokenizer.ini
/etc/php.ini
/usr/lib64/php
/usr/lib64/php-zts
/usr/lib64/php-zts/modules
/usr/lib64/php-zts/modules/bz2.so
/usr/lib64/php-zts/modules/calendar.so
/usr/lib64/php-zts/modules/ctype.so
/usr/lib64/php-zts/modules/curl.so
/usr/lib64/php-zts/modules/exif.so
/usr/lib64/php-zts/modules/fileinfo.so
/usr/lib64/php-zts/modules/ftp.so
/usr/lib64/php-zts/modules/gettext.so
/usr/lib64/php-zts/modules/iconv.so
/usr/lib64/php-zts/modules/phar.so
/usr/lib64/php-zts/modules/sockets.so
/usr/lib64/php-zts/modules/tokenizer.so
/usr/lib64/php/modules
/usr/lib64/php/modules/bz2.so
/usr/lib64/php/modules/calendar.so
/usr/lib64/php/modules/ctype.so
/usr/lib64/php/modules/curl.so
/usr/lib64/php/modules/exif.so
/usr/lib64/php/modules/fileinfo.so
/usr/lib64/php/modules/ftp.so
/usr/lib64/php/modules/gettext.so
/usr/lib64/php/modules/iconv.so
/usr/lib64/php/modules/phar.so
/usr/lib64/php/modules/sockets.so
/usr/lib64/php/modules/tokenizer.so
/usr/share/doc/php-common-8.1.10
/usr/share/doc/php-common-8.1.10/CODING_STANDARDS.md
/usr/share/doc/php-common-8.1.10/CONTRIBUTING.md
/usr/share/doc/php-common-8.1.10/EXTENSIONS
/usr/share/doc/php-common-8.1.10/NEWS
/usr/share/doc/php-common-8.1.10/README.REDIST.BINS
/usr/share/doc/php-common-8.1.10/README.md
/usr/share/doc/php-common-8.1.10/UPGRADING
/usr/share/doc/php-common-8.1.10/UPGRADING.INTERNALS
/usr/share/doc/php-common-8.1.10/docs
/usr/share/doc/php-common-8.1.10/docs/input-filter.md
/usr/share/doc/php-common-8.1.10/docs/mailinglist-rules.md
/usr/share/doc/php-common-8.1.10/docs/output-api.md
/usr/share/doc/php-common-8.1.10/docs/parameter-parsing-api.md
/usr/share/doc/php-common-8.1.10/docs/release-process.md
/usr/share/doc/php-common-8.1.10/docs/self-contained-extensions.md
/usr/share/doc/php-common-8.1.10/docs/streams.md
/usr/share/doc/php-common-8.1.10/docs/unix-build-system.md
/usr/share/doc/php-common-8.1.10/php.ini-development
/usr/share/doc/php-common-8.1.10/php.ini-production
/usr/share/licenses/php-common-8.1.10
/usr/share/licenses/php-common-8.1.10/BOOST_LICENSE
/usr/share/licenses/php-common-8.1.10/LICENSE
/usr/share/licenses/php-common-8.1.10/TSRM_LICENSE
/usr/share/licenses/php-common-8.1.10/ZEND_LICENSE
/usr/share/licenses/php-common-8.1.10/libmagic_LICENSE
/usr/share/licenses/php-common-8.1.10/timelib_LICENSE
/usr/share/php
/var/lib/php
/usr/bin/phar
/usr/bin/phar.phar
/usr/bin/php
/usr/bin/php-cgi
/usr/bin/phpize
/usr/bin/zts-php
/usr/share/man/man1/phar.1.gz
/usr/share/man/man1/phar.phar.1.gz
/usr/share/man/man1/php-cgi.1.gz
/usr/share/man/man1/php.1.gz
/usr/share/man/man1/phpize.1.gz
/usr/share/man/man1/zts-php.1.gz
/usr/share/man/man1/zts-phpize.1.gz
/etc/httpd/conf.d/php.conf
/etc/httpd/conf.modules.d/20-php.conf
/usr/lib64/httpd/modules/libphp.so
/var/lib/php/opcache
/var/lib/php/session
/var/lib/php/wsdlcache
/etc/php-zts.d/20-sodium.ini
/etc/php.d/20-sodium.ini
/usr/lib64/php-zts/modules/sodium.so
/usr/lib64/php/modules/sodium.so
程序路径:
[root@serv-101 ~]# whereis php
php: /usr/bin/php /usr/lib64/php /etc/php.d /etc/php.ini /usr/share/php /usr/share/man/man1/php.1.gz
# 主要程序,php phar php-cgi phpize
/usr/bin/phar
/usr/bin/phar.phar
/usr/bin/php
/usr/bin/php-cgi
/usr/bin/phpize
扩展安装路径:
/etc/php.d
/usr/lib64/php/modules/
[root@serv-101 ~]# ls /usr/lib64/php/modules/
bz2.so ctype.so exif.so ftp.so iconv.so sockets.so tokenizer.so
calendar.so curl.so fileinfo.so gettext.so phar.so sodium.so
1.4 配合Apache和Nginx
Apache(httpd)/Nginx -> FastCgi
1.4.1 httpd php_mod模式
载入到apache 内存里面
核心配置是,/etc/httpd/conf.d/php.conf
LoadModule php_module modules/libphp.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
前面安装那php时,已经自动配置到httpd的php支持,完整配置如下:
/etc/httpd/conf.d/php.conf
/etc/httpd/conf.modules.d/20-php.conf
/usr/lib64/httpd/modules/libphp.so
php.conf : cat /etc/httpd/conf.d/php.conf
#
# The following lines prevent .user.ini files from being viewed by Web clients.
#
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
Satisfy All
</IfModule>
</Files>
#
# Allow php to handle Multiviews
#
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
# mod_php options
<IfModule mod_php.c>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.(php|phar)$>
SetHandler application/x-httpd-php
</FilesMatch>
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
# SetHandler application/x-httpd-php-source
#</FilesMatch>
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"
php_value soap.wsdl_cache_dir "/var/lib/php/wsdlcache"
#php_value opcache.file_cache "/var/lib/php/opcache"
</IfModule>
20-php.conf: [root@serv-101 html]# cat /etc/httpd/conf.modules.d/20-php.conf
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
# Cannot load both php5, php7 and php modules
<IfModule !mod_php5.c>
<IfModule !mod_php7.c>
<IfModule prefork.c>
LoadModule php_module modules/libphp.so
</IfModule>
</IfModule>
</IfModule>
二. 编译安装
2.1 下载源码
源码下载地址: https://www.php.net/downloads.php
wget https://www.php.net/distributions/php-8.1.10.tar.gz
# 或
curl -O https://www.php.net/distributions/php-8.1.10.tar.gz
tar -xvf php-8.1.10.tar.gz
git源码下载: https://github.com/php/php-src
git clone https://github.com/php/php-src.git
git checkout PHP-8.1
2.2 安装依赖
2.2.1 centos7
yum -y install libxml2-devel sqlite-devel bzip2-devel libcurl-devel libpng-devel libjpeg-devel freetype-devel libicu-devel oniguruma-devel libxslt-devel
2.3 编译
./buildconf
./configure --prefix=/usr/local/php8
make && make install
2.4 编译apache模块libphp.so
apache内置模块
2.4.1 编译libphp.so
# 先安装apache 开发工具,或者编译安装apache
yum install httpd-devel
# apxs2路径: /usr/bin/apxs 自定义路径/usr/local/apache2/bin/apxs
./configure --prefix=/usr/local/php8 --with-apxs2=/usr/local/apache2/bin/apxs
make && make install
libphp.so创建在两个目录下,如果也可以手动复制到apache目录modules.
./libs/libphp.so
# 和apache module目录
/usr/local/apache2/modules/libphp.so
2.4.2 配置httpd
httpd.conf里添加一条
# 上一步编译时会自动添加
LoadModule php_module modules/libphp.so
AddType text/html .php
DirectoryIndex index.php
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
重启服务:
/usr/local/apache2/bin/apachectl restart
编写一个php测试文件:
/usr/local/apache2/htdocs/index.php
<?php
phpinfo();
2.4.3 libphp的php配置
apache php_mod 模式,不依赖于php环境,解释器在apache内部实现.
复制php.ini到那 配置目录.
php_mod编译时没有指定时,默认配置在lib中.
有两种方法修改配置文件路径:
- httpd.conf 中添加 PHPIniDir 配置
- 编译libphp时添加
httpd.conf添加 PHPIniDir
PHPIniDir /etc/php.ini
修改后phpinfo:
libphp编译参数
重新编译php
./configure --prefix=/usr/local/php8 \
--with-apxs2=/usr/local/apache2/bin/apxs \
-with-config-file-path=/etc
make && make install
2.5 编译PHP-FPM
php-fpm 是fastCIG管理工具,目前大部分生产环境都使用fpm来管理应用
2.5.1 编译fpm
主要 --with-apxs2
改成 --enable-fpm
make clean
./configure --prefix=/usr/local/php8 \
-with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-fpm
make -j 2 && make install
php-fpm 路径:/usr/local/php8/sbin/php-fpm
2.5.2 启动fpm
fpm配置文件在编译目录的sapi/fpm目录下.
[root@serv-102 php-8.1.10]# ls sapi/fpm/
config.m4 init.d.php-fpm Makefile.frag php-fpm.8.in php-fpm.service status.html.in www.conf.in
CREDITS init.d.php-fpm.in php-fpm php-fpm.conf php-fpm.service.in tests
fpm LICENSE php-fpm.8 php-fpm.conf.in status.html www.conf
复制相关文件,"/usr/local/php8/etc/" 为--prefix配置的路径.
配置:
# php.ini
cp php.ini-production /etc/php.ini
# fpm配置
cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
添加执行权限和开机启动
1.init服务
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
2.种systemd服务
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
如果启动不了,把php-fpm.service的ProtectSystem改成false
给日志目录增加权限.
启动:
systemctl start php-fpm
systemctl status php-fpm
默认监听9000端口
2.5.3 nginx中使用
增加nginx配置
location ~ \.php$ {
root /usr/share/nginx/html;
index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
验证:
2.5.4 在apache的cgi中使用
前面使用的php_mod模式,需要web服务与php服务分离,php配置更改不需要重启apache.
改成cgi模式
有3种方式:
1.SetHandler
2.ProxyPassMatch
3.ProxyPass
4.Mod_rewrite
1.通过handler方式
全局通用的模式
/usr/local/apache2/conf/httpd.conf
# 添加mod_proxy.so 和 mod_proxy_fcgi.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# 添加 php 类型
AddType application/x-httpd-php .php
# 添加代理
<FilesMatch \.php$>
#SetHandler application/x-httpd-php
# 添加端口
setHandler "proxy:fcgi://127.0.0.1:9000"
# sock文件
# setHandler "proxy:unix:/var/run/php-fpm/php-fpm.sock"
</FilesMatch>
2.ProxyPassMatch方式
通过vhost 模式,每个域名服务单独配置,更灵活.
注意:如果是有动静混合的项目,DocumentRoot的数据要合php-fpm的文件要保持一致.
<VirtualHost _default_:8090>
DocumentRoot "/usr/local/apache2/htdocs/"
ServerName php.localhost
<IfModule mpm_event_module>
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/usr/local/apache2/htdocs/$1
</IfModule>
</VirtualHost>
3.ProxyPass 方式
和ProxyPassMatch的一样
<VirtualHost _default_:8090>
DocumentRoot "/usr/local/apache2/"
ServerName php-fpm
<LocationMatch ^(.*\.php)$>
ProxyPass fcgi://127.0.0.1:9000/usr/local/apache2/htdocs/
ProxyErrorOverride on
</LocationMatch>
#<IfModule mpm_event_module>
# ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/usr/local/apache2/htdocs/$1
#</IfModule>
</VirtualHost>
验证
性能对比:
nginx
Server Software: nginx/1.22.0
Server Hostname: 192.168.56.102
Server Port: 8081
Document Path: /test.php
Document Length: 21 bytes
Concurrency Level: 100
Time taken for tests: 0.641 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 184000 bytes
HTML transferred: 21000 bytes
Requests per second: 1560.47 [#/sec] (mean)
Time per request: 64.083 [ms] (mean)
Time per request: 0.641 [ms] (mean, across all concurrent requests)
Transfer rate: 280.40 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.1 0 6
Processing: 7 61 10.1 64 69
Waiting: 1 61 10.2 64 69
Total: 7 62 9.3 65 72
apache(ProxyPassMatch)+fpm:
Server Software: Apache/2.4.54
Server Hostname: 192.168.56.102
Server Port: 8090
Document Path: /test.php
Document Length: 21 bytes
Concurrency Level: 100
Time taken for tests: 0.901 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 203000 bytes
HTML transferred: 21000 bytes
Requests per second: 1109.26 [#/sec] (mean)
Time per request: 90.150 [ms] (mean)
Time per request: 0.901 [ms] (mean, across all concurrent requests)
Transfer rate: 219.90 [Kbytes/sec] received
apache(setHander)+fpm:
Server Software: Apache/2.4.54
Server Hostname: 192.168.56.102
Server Port: 80
Document Path: /test.php
Document Length: 21 bytes
Concurrency Level: 100
Time taken for tests: 0.934 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 203000 bytes
HTML transferred: 21000 bytes
Requests per second: 1070.19 [#/sec] (mean)
Time per request: 93.441 [ms] (mean)
Time per request: 0.934 [ms] (mean, across all concurrent requests)
Transfer rate: 212.16 [Kbytes/sec] received
apache+mod_php:
Server Software: Apache/2.4.54
Server Hostname: 192.168.56.102
Server Port: 80
Document Path: /test.php
Document Length: 28 bytes
Concurrency Level: 100
Time taken for tests: 6.804 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2300000 bytes
HTML transferred: 280000 bytes
Requests per second: 1469.67 [#/sec] (mean)
Time per request: 68.042 [ms] (mean)
Time per request: 0.680 [ms] (mean, across all concurrent requests)
Transfer rate: 330.10 [Kbytes/sec] received
apache(PROXYPASS)+fpm
Server Software: Apache/2.4.54
Server Hostname: 192.168.56.102
Server Port: 8090
Document Path: /test.php
Document Length: 21 bytes
Concurrency Level: 100
Time taken for tests: 8.796 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2030000 bytes
HTML transferred: 210000 bytes
Requests per second: 1136.83 [#/sec] (mean)
Time per request: 87.964 [ms] (mean)
Time per request: 0.880 [ms] (mean, across all concurrent requests)
Transfer rate: 225.37 [Kbytes/sec] received
性能: nginx > php_mod > ProxyPassMatch | PROXYPASS
2.6 添加到环境变量
添加命令行和环境
[root@serv-102 ~]# php -v
-bash: php: ⌘ not found
yum中php路径:
vi /etc/profile
export PHP_HOME=/usr/local/php8
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin
source /etc/profile
2.7 编译参数
php自带很多扩展支持需要编译时手动选择的.
https://www.php.net/manual/zh/configure.about.php
对比build和yum安装后后区别:
主要配置项有:
参考:https://www.shuzhiduo.com/A/pRdB6xjadn/
# 指定安装目录
--prefix= /usr/local/php
# 指定php.ini位置
--with-config-file-path=/usr/local/php/etc
#
todo: 未完...
三. Apache部署
四. Nginx部署
五. 安装扩展
六. 性能对比
七. 生产可用部署
原作者:阿金
本文地址:https://hi-arkin.com/archives/centos-php-env.html