Linux环境下PHP的安装与配置指南

准备工作
在开始安装PHP之前,我们需要确保Linux系统中已经安装了以下软件:
- GCC编译器
- Make工具
- Autoconf
- Automake
- Libxml2
- Libxslt
- Libmcrypt
- Libzip
- OpenLDAP(可选)
您可以通过以下命令检查是否已安装:
gcc --version make --version autoconf --version automake --version xml2-config --version xslt-config --version libmcrypt-config --version zip --version ldapsearch --version
安装PHP
下载PHP源码
从PHP官方网站下载最新版本的PHP源码包,下载PHP 7.4.16版本:
wget https://php.net/distributions/php-7.4.16.tar.gz
解压源码包
解压下载的PHP源码包:
tar -zxvf php-7.4.16.tar.gz
进入源码目录

进入解压后的源码目录:
cd php-7.4.16
配置PHP
运行以下命令配置PHP:
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-bz2 --with-curl --with-gd --with-gettext --with-mbstring --with-pear --enable-zip --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-session --enable-opcache --enable-fileinfo --enable-filter --enable-json --enable-intl --with-ldap --with-openssl-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --with-gd-lib-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-xmlrpc --with-xsl --with-mhash --with-pear --with-apxs2=/usr/local/apache/bin/apxs
编译与安装
编译并安装PHP:
make make install
配置Apache服务器
(1)复制PHP配置文件
cp /usr/local/php/etc/php.ini-production /usr/local/php/etc/php.ini
(2)修改Apache配置文件
编辑Apache配置文件(如httpd.conf),添加以下内容:

LoadModule php7_module modules/libphp7.so
ServerName localhost
DocumentRoot /var/www/html
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
(3)配置PHP模块
<IfModule php7_module>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "/usr/local/php/etc"
</IfModule>
重启Apache服务器
重启Apache服务器以使配置生效:
service httpd restart
验证PHP安装
在浏览器中访问以下URL,查看PHP安装是否成功:
http://localhost/info.php
如果看到PHP的信息页面,则表示PHP安装成功。
本文介绍了在Linux环境下安装和配置PHP的方法,通过以上步骤,您可以在Linux系统中成功安装PHP,并配置Apache服务器以支持PHP,希望对您有所帮助。