分享生活,记录成长
表达自我,拥抱世界

PHP服务安装与配置

一、相关依赖环境安装

首先更新yum源

[root@krist phpdir]# yum update -y

安装依赖包

[root@krist /]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel libzip gcc-c++

yasm源码包是一款常见的开源汇编器,可在以下网址下载源码包:

1、http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz

2、https://www.linuxprobe.com/Software/freetype-2.5.3.tar.gz

[root@krist phpdir]# tar zxvf yasm-1.3.0.tar.gz
[root@krist phpdir]# cd yasm-1.3.0/
[root@krist yasm-1.3.0]# ./configure
[root@krist yasm-1.3.0]# make && make install

libmcrypt源码包是用于加密算法的扩展程序,可在以下网址下载源码包:

1、https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download

2、https://www.linuxprobe.com/Software/libmcrypt-2.5.8.tar.gz

[root@krist yasm-1.3.0]# cd ..
[root@krist phpdir]# tar zxf libmcrypt-2.5.8.tar.gz
[root@krist phpdir]# cd libmcrypt-2.5.8/
[root@krist libmcrypt-2.5.8]# ./configure
[root@krist libmcrypt-2.5.8]# make && make install

libvpx源码包用于提供视频编码器的服务程序,可在以下网址下载源码包:

1、https://www.linuxprobe.com/Software/libvpx-v1.3.0.tar.bz2

2、https://github.com/webmproject/libvpx/archive/v1.9.0/libvpx-1.9.0.tar.gz

[root@krist phpdir]# tar jxvf libvpx-v1.3.0.tar.bz2
[root@krist phpdir]# cd libvpx-v1.3.0/
[root@krist libvpx-1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
[root@krist libvpx-1.3.0]# make && make install

tiff源码包是用于提供标签图像文件格式的服务程序

1、http://download.osgeo.org/libtiff/tiff-4.1.0.tar.gz

2、https://www.linuxprobe.com/Software/tiff-4.0.3.tar.gz

[root@krist phpdir]# tar zxvf tiff-4.1.0.tar.gz
[root@krist phpdir]# cd tiff-4.1.0/
[root@krist tiff-4.1.0]# ./configure --prefix=/usr/local/tiff --enable-shared
[root@krist tiff-4.1.0]# make && make install

libpng源码包是用于提供png图片格式支持函数库的服务程序

1、https://sourceforge.net/projects/libpng/files/libpng16/1.6.37/libpng-1.6.37.tar.gz/download

2、https://www.linuxprobe.com/Software/libpng-1.6.12.tar.gz

[root@krist phpdir]# tar zxf libpng-1.6.37.tar.gz
[root@krist phpdir]# cd libpng-1.6.37/
[root@krist libpng-1.6.37]# ./configure --prefix=/usr/local/libpng --enable-shared
[root@krist libpng-1.6.37]# make && make install

freetype源码包用于提供字体支持引擎的服务程序

1、https://sourceforge.net/projects/freetype/files/freetype2/2.10.2/freetype-2.10.2.tar.gz/download

2、https://www.linuxprobe.com/Software/freetype-2.5.3.tar.gz

3、https://downloads.sourceforge.net/freetype/freetype-2.10.2.tar.gz

[root@krist libpng-1.6.37]# cd ..
[root@krist phpdir]# tar zxf freetype-2.10.2.tar.gz 
[root@krist phpdir]# cd freetype-2.10.2/
[root@krist freetype-2.10.2]# ./configure --prefix=/usr/local/freetype --enable-shared 
[root@krist freetype-2.10.2]# make && make install

jpeg源码包是用于提供jpeg图片格式支持函数库的服务程序

1、http://www.ijg.org/files/jpegsrc.v9d.tar.gz

2、https://www.linuxprobe.com/Software/jpegsrc.v9a.tar.gz

[root@krist freetype-2.10.2]# cd ..
[root@krist phpdir]# tar zxf jpegsrc.v9d.tar.gz 
[root@krist phpdir]# cd jpeg-9d/
[root@krist jpeg-9d]# ./configure --prefix=/usr/local/jpeg --enable-shared
[root@krist jpeg-9d]# make && make install

libgd源码包是用于提供图形处理的服务程序。在编译libgd源码包时记得写入jpeg、libpng、freetype、tiff、libvpx等服务程序在系统中的安装路径

1、https://github.com/libgd/libgd/releases/download/gd-2.3.0/libgd-2.3.0.tar.gz

2、https://www.linuxprobe.com/Software/libgd-2.1.0.tar.gz

[root@krist jpeg-9d]# cd ..
[root@krist phpdir]# tar zxf libgd-2.3.0.tar.gz 
[root@krist phpdir]# cd libgd-2.3.0/
[root@krist libgd-2.3.0]# ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
[root@krist libgd-2.3.0]# make && make install

t1lib源码包是用于提供图片生成函数库的服务。安装后把/usr/lib64目录中的函数文件链接到/usr/lib目录中,以便系统能顺利调取到函数文件

1、https://fossies.org/linux/misc/old/t1lib-5.1.2.tar.gz

2、https://www.linuxprobe.com/Software/t1lib-5.1.2.tar.gz

[root@krist libgd-2.3.0]# cd ..
[root@krist phpdir]# tar zxf t1lib-5.1.2.tar.gz 
[root@krist phpdir]# cd t1lib-5.1.2/
[root@krist t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared
[root@krist t1lib-5.1.2]# make without_doc && make install
[root@krist t1lib-5.1.2]# ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so
[root@krist t1lib-5.1.2]# cp -frp /usr/lib64/libXpm.so* /usr/lib/

下载php源码安装包

[root@krist phpdir]# wget https://www.php.net/distributions/php-7.4.9.tar.gz

解压安装包并添加用户和组

[root@krist phpdir]# tar zxvf php-7.4.9.tar.gz
[root@krist phpdir]# groupadd www
[root@krist phpdir]# useradd -M -g www -s /sbin/nologin www

进入目录开始编译

[root@krist phpdir]# cd php-7.4.9/
[root@Krist php-7.4.9]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
[root@Krist php-7.4.9]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --with-apxs2=/usr/local/httpd/bin/apxs

可能出现的错误1:

configure: error: Package requirements (sqlite3 > 3.7.4)

解决:

yum -y install sqlite-devel

可能出现的错误2:

No package 'oniguruma' found

解决:

[root@Krist phpdir]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz 
[root@Krist phpdir]# tar zxf oniguruma-6.9.4.tar.gz 
[root@Krist phpdir]# cd oniguruma-6.9.4/
[root@Krist oniguruma-6.9.4]# ./autogen.sh 
[root@Krist oniguruma-6.9.4]# ./configure --prefix=/usr/local/oniguruma --libdir=/lib64
[root@Krist oniguruma-6.9.4]# make && make install

可能出现的错误3:

configure: error: Please reinstall the libzip distribution

需要手动安装最新版本libzip,先编译安装最新版cmake,github:https://github.com/Kitware/CMake/releases

cd /usr/local/src
wget https://github.com/Kitware/CMake/releases/download/v3.14.3/cmake-3.14.3.tar.gz
tar -zxvf cmake-3.14.3.tar.gz
cd cmake-3.14.3
./bootstrap
make && make install

 再编译安装libzip

yum remove libzip -y
cd /usr/local/src
wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxvf libzip-1.5.2.tar.gz
cd libzip-1.5.2
mkdir build
cd build
cmake ..
make && make install

编译完成后测试并安装:

[root@krist php-7.4.9]# make test
[root@krist php-7.4.9]# make -j2 && make -j2 install

完成后添加环境变量:

[root@krist /]# vi /etc/profile
[root@krist /]# cat /etc/profile |tail -n 1
export PATH=$PATH:/usr/local/php/bin

为PHP提供配置文件:

[root@Krist php-7.4.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@Krist php-7.4.9]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
[root@Krist php-7.4.9]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@Krist php-7.4.9]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@Krist php-7.4.9]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf

修改php-fpm配置文件-www.conf

[root@Krist /]# cd /usr/local/php/etc/php-fpm.d/
[root@Krist php-fpm.d]# vim www.conf
user = www
group = www
[root@Krist php-fpm.d]# cd ..
[root@Krist etc]# vim php-fpm.conf
pid = run/php-fpm.pid

加入开机启动文件:

[root@Krist etc]# cd /www/phpdir/php-7.4.9/
[root@Krist php-7.4.9]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@Krist php-7.4.9]# chmod 755 /etc/rc.d/init.d/php-fpm 
[root@Krist php-7.4.9]# chkconfig php-fpm on

PHP-FPM与NGINX的整合

[root@krist etc]# vim /usr/local/nginx/conf/nginx.conf  //修改nginx配置文件的如下两个地方
[root@Krist php-7.4.9]# cat -n /usr/local/nginx/conf/nginx.conf | awk 'NR==2||NR<=46&&NR>=43||NR<=71&&NR>=65'
     2  user www www;
    43          location / {
    44              root   /var/www/html;
    45              index  index.html index.htm index.php;
    46          }
    65          location ~ \.php$ {
    66              root           /var/www/html;
    67              fastcgi_pass   127.0.0.1:9000;
    68              fastcgi_index  index.php;
    69              fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
    70              include        fastcgi_params;
    71          }
[root@krist etc]# systemctl restart nginx.service   //重启nginx

测试php模块是否能解析,输出phpinfo:

[root@Krist /]# service php-fpm start
[root@Krist /]# mkdir -p /var/www/html/
[root@Krist /]# cd /var/www/html/
[root@Krist html]# vim index.php
<?php
echo phpinfo();
?>

测试php连接mysql:实例 (MySQLi - 面向对象)

https://www.runoob.com/php/php-mysql-connect.html

<?php

$servername = "localhost"; 
$username = "username"; 
$password = "password";  

// 创建连接 
$conn = new mysqli($servername, $username, $password);  

// 检测连接 
if ($conn->connect_error) {    
    die("连接失败: " . $conn->connect_error); 
}  
    echo "连接成功"; 
?>

PHP-FPM与Apache整合

[root@Krist /]# /usr/local/nginx/sbin/nginx -s stop
[root@Krist /]# service php-fpm stop

查看PHP 所加载的模块:

[root@Krist htdocs]# /usr/local/php/bin/php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mbstring
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvsem
tokenizer
xml
xmlreader
xmlrpc
xmlwriter

[Zend Modules]

查看apache 所加载的模块:

[root@Krist htdocs]# /usr/local/httpd/bin/apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_event_module (static)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)

编辑Apache配置文件,加载php模块

[root@Krist /]# vim /usr/local/httpd/conf/http.conf
//修改以下内容,此处注意,php文件必须放前面,优先读取
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>
在上两行内容下面添加两行内容
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
在LoadModule下添加下面这行
LoadModule php7_module modules/libphp7.so

如果Apache安装目录的modules目录没有libphp7.so文件,说明安装编译php时少了--with-apxs2,需要重新编译安装php,不想重新编译,那你只能用Apache提供的其他方式运行PHP,比如:
1.使用Apache的mod_fcgid配合php-cgi运行(类似IIS + PHP-CGI);
2.使用Apache的mod_proxy_fcgi配合php-fpm运行(类似Nginx + PHP-FPM)。

[root@Krist /]# cd /www/phpdir/php-7.4.9/
[root@Krist php-7.4.9]# find / -name "apxs"   //查看 apsx 所在路径
/usr/local/httpd/bin/apxs
/www/httpdir/httpd-2.4.46/support/apxs

重新编译php时,加入 apxs 路径参数,作用是促使生成 libphp7.so

[root@Krist php-7.4.9]# make clean  //清除make之后产生的.o文件以及一些编译过程中产生的中间文件(不确定对之前编译有无影响)
 find . -name \*.gcno -o -name \*.gcda | xargs rm -f
 find . -name \*.lo -o -name \*.o | xargs rm -f
 find . -name \*.la -o -name \*.a | xargs rm -f
 find . -name \*.so | xargs rm -f
 find . -name .libs -a -type d|xargs rm -rf
 rm -f libphp7.la sapi/cli/php sapi/cgi/php-cgi   libphp7.la modules/* libs/*
[root@Krist php-7.4.9]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --with-apxs2=/usr/local/httpd/bin/apxs --with-php-config=/usr/local/php/bin/php-config
[root@Krist php-7.4.9]# make && make install

以上的编译未编译安装mysqli扩展和pdo_mysql扩展,需要手动添加:

1、添加mysqli扩展

参考:https://blog.csdn.net/weixin_33816611/article/details/92310963

[root@Krist php-7.4.9]# cd ext/mysqli/
[root@Krist mysqli]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
[root@Krist mysqli]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysqli=/usr/local/mysql/bin/mysql_config
[root@Krist mysqli]# make && make install

问题1: 在安装mysqli的时候,出现error: ext/mysqlnd/mysql_float_to_double.h: No such file or directory

解决方法:

[root@Krist mysqli]# vim /www/phpdir/php-7.4.9/ext/mysqli/mysqli_api.c
//修改对应报错行,我这里是34行
"ext/mysqlnd/mysql_float_to_double.h"
修改为绝对路径
"/www/phpdir/php-7.4.9/ext/mysqlnd/mysql_float_to_double.h"

编译安装完成后,会自动把mysqli.so放到php扩展目录下,可用命令 “/usr/local/php/bin/php-config --extension-dir” 查看扩展目录路径;再修改php.ini将mysqli扩展加入:

[root@Krist /]# vi /usr/local/php/etc/php.ini 
761  extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20190902/"
951  extension=mysqli.so

2、添加pdo_mysql扩展

参考:https://www.cnblogs.com/cjjjj/p/10555529.html

[root@Krist /]# cd /www/phpdir/php-7.4.9/ext/pdo_mysql/
[root@Krist pdo_mysql]# /usr/local/php/bin/phpize   //执行phpize
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
[root@Krist pdo_mysql]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
[root@Krist pdo_mysql]# make && make install
[root@Krist pdo_mysql]# vi /usr/local/php/etc/php.ini   //修改php.ini文件
增加extension=pdo_mysql.so;
重启php-fpm

以下命令可查看nginx、apache、php、mysql的编译参数:

1、nginx编译参数:
#/usr/local/nginx/sbin/nginx -V
2、apache编译参数:
# cat /usr/local/apache/build/config.nice
3、php编译参数:
# /usr/local/php/bin/php -i |grep configure
4、mysql编译参数:
# cat /usr/local/mysql/bin/mysqlbug|grep configure

 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:DDblog » PHP服务安装与配置

分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

立即登录   注册

DD博客

联系我们每天好心情
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活