在CentOS上安装PHP和MySQL是一个常见的任务,通常用于搭建Web开发环境,以下将详细描述如何在CentOS上安装PHP和MySQL,并确保逻辑清晰、内容准确全面。
一、准备工作
1、系统更新:在安装任何软件之前,建议先更新系统,以确保所有软件包都是最新的。
sudo yum update y
2、安装必要的依赖:为了编译和安装PHP,需要一些基本的编译工具和库。
sudo yum install y gcc automake autoconf libtool gccc++ gd zlib zlibdevel openssl openssldevel libxml2 libxml2devel libjpeg libjpegdevel libpng libpngdevel freetype freetypedevel libmcrypt libmcryptdevel curldevel
二、安装MySQL
1、下载并安装MySQL Yum仓库:
wget https://repo.mysql.com/mysql80communityreleaseel73.noarch.rpm sudo rpm ivh mysql80communityreleaseel73.noarch.rpm
2、安装MySQL服务器:
sudo yum install y mysqlserver
3、启动MySQL服务并设置开机自启:
sudo systemctl start mysqld sudo systemctl enable mysqld
4、获取临时密码并登录MySQL:
sudo grep 'temporary password' /var/log/mysqld.log sudo mysql u root p
5、修改root用户密码并刷新权限:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'; FLUSH PRIVILEGES; EXIT;
6、允许远程访问MySQL(可选):
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '新密码' WITH GRANT OPTION; FLUSH PRIVILEGES;
7、开放防火墙端口:
sudo firewallcmd zone=public addport=3306/tcp permanent sudo firewallcmd reload
三、安装PHP
CentOS 8自带了PHP 7.2版本,可以直接使用Yum进行安装,如果需要特定版本的PHP,可以从源码编译安装。
使用yum安装PHP:
1、安装PHP及其常用扩展:
sudo yum install y php phpmysqlnd phpfpm phpgd phpmbstring phpopcache phppdo
2、配置PHPFPM:
编辑/etc/phpfpm.d/www.conf
文件,修改以下内容:
user = www group = www listen = /run/phpfpm/www.sock
3、启动PHPFPM并设置开机自启:
sudo systemctl start phpfpm sudo systemctl enable phpfpm
从源码编译安装PHP(以PHP 7.4为例):
1、下载PHP源码并解压:
mkdir /usr/local/php74 cd /usr/local/php74 wget https://www.php.net/distributions/php7.4.0.tar.gz tar xf php7.4.0.tar.gz cd php7.4.0
2、应用补丁并配置编译选项:
wget https://pecl.php.net/get/php_xdebug2.9.6.tgz tar xf php_xdebug2.9.6.tgz cd php_xdebug2.9.6 cp ext/xdebug ext/xdebug/config.m4 ../ext/xdebug/ cd ../ext/xdebug phpize ./configure enabledebug withphpconfig=/usr/local/php74/bin/phpconfig withmysqli=mysqlnd withpdomysql=mysqlnd withopenssl withzlib withcurl withxmlrpc withmysqli=mysqlnd withembeddedpath=/usr/local/php74/bin/php enablembstring withcurl enableftp enableexif enablegdnativettf withjpeg withpng withfreetypedir=/usr/share/libpng withjpegdir=/usr/share/libpng withxpmdir=/usr/share/libpng withxpmjs=/usr/share/libpng/xpmjs withwebpdir=/usr/share/libpng witht1lib=/usr/share/libpng withgd withgettext enablembstring withcurl withpear withhttp_parser=/usr/local/php74/bin/phpconfig withbz2 withzlib withzip withpcreregex withlibedit withlibeditcompat withlibxmldir=/usr/local/php74/bin/phpconfig disableipv6 enableinlineoptimization enableintl enablebcmath disabledebug withfpmuser=www withfpmgroup=www enableopcache withopenssl withlibedit enablesession enableshmop withsysvshm enablewddx withzlib withfileinfo withlibxmldir=/usr/local/php74/bin/phpconfig enablexml withcurl enableexif enableftp enablembstring enablesockets enablesoap enablezip enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx withzlib withfileinfo enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enableshmop enablesysvshm enablewddx enablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstringenablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstringenablembstring enableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstringenablembstringenableopcache enablepcntl enableshmop enablesysvshm enablewddx enablembstringenablembstringenableopcache enablepcntl enableshmop enablesysvshm enablewddxenablembstringenableopcachewithfpmuser=wwwwithfpmgroup=wwwwithmysqli=mysqlndwithmysqlndwithmysqli=mysqlndwithpdo_mapper=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlndwithmysqli=mysqlnd#编译并安装 make && make install
3、复制配置文件并启动PHP:
cp /usr/local/php74/lib/php.ini /usr/local/php74/etc/php.ini cp /usr/local/php74/etc/phpfpm.d/www.conf.default /usr/local/php74/etc/phpfpm.d/www.conf cp /usr/local/php74/sapi/fpm/init.d.php /etc/init.d/php74_fpm chmod +x /etc/init.d/php74_fpm sudo systemctl start php74_fpm sudo systemctl enable php74_fpm
四、验证安装结果
1、验证PHP是否安装成功:
php v
2、验证MySQL连接:
<?php $conn = new mysqli("localhost", "root", "密码", "数据库名"); if ($conn>connect_error) { die("Connection failed: " . $conn>connect_error); } else { echo "Connected successfully"; } $conn>close(); ?>
3、测试PHP与MySQL的集成:
创建一个简单的PHP文件,例如test.php
如下:
<?php $conn = new mysqli("localhost", "root", "密码", "数据库名"); if ($conn>connect_error) { die("Connection failed: " . $conn>connect_error); } else { echo "Connected successfully"; } $conn>close(); ?>
将此文件放置在Apache或Nginx的Web根目录下,并通过浏览器访问以验证PHP与MySQL是否正常工作。
五、常见问题及解答
Q1:如何更改PHP上传文件大小限制?
A1:编辑php.ini
文件,找到并修改以下参数:
upload_max_filesize = 10M post_max_size = 10M
然后重启PHP服务。
Q2:如何解决“Fatal error: Call to undefined function mysql_connect()”错误?
A2:这个错误通常是因为缺少MySQL扩展,请确保安装了phpmysql
或phpmysqlnd
扩展,并重启PHP服务。
Q3:如何配置PHP以使用Opcache?
A3:编辑php.ini
文件,启用Opcache并配置相关参数,
[opcache] opcache.enable=1 opcache.memory_consumption=128 opcache.interned_functions_buffering=2
然后重启PHP服务。
在CentOS上安装PHP和MySQL涉及多个步骤,包括系统更新、安装必要的依赖、下载和安装MySQL、配置MySQL服务、安装PHP及其扩展、配置PHP服务等,通过上述详细的步骤,可以确保在CentOS上成功搭建一个功能完备的PHP和MySQL环境。