RHEL / CentOS 安装 PHP 7, Apache, MariaDB


PHP 7 还未发布正式版, 只发布了 RC 1 版本, 但对于 PHP 的开发者, 预先在测试环境安装好 PHP 7 的环境, 测试程式码是否可以正常运作也很重要, 这里会介绍在 RHEL 及 CentOS 安装 Apache + MariaDB + PHP 7。

安装 MariaDB

执行以下指令安装 MariaDB:

# yum install mariadb-server mariadb

启动及设定开机自动执行 MariaDB:

# systemctl start mariadb
# systemctl enable mariadb

执行以下指令设定 MariaDB 的 root 密码, 默认是空密码, 所以建议尽快修改:

# /usr/bin/mysql_secure_installation

完成后可以用测试一下 MariaDB 是否已经启动:

# mysql -u root -p

安装 Apache

# yum install httpd

跟着回答 “y” 后便会完成安装, 然后输入以下指令启动及设定 Apache 开机自动执行:

# systemctl start httpd
# systemctl enable httpd

这时 Apache 已经启动了, 可以在浏览器输入服务器的位置试试, 例如 http://localhost

安装 PHP 7

在编译 PHP 前, 先用 YUM 安装 PHP 相关的套件, 例如 curl, lobjpeg 等, 编译 PHP 一些常用模组时需要用到这些套件。

# yum install libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libmcrypt-devel libtool-ltdl-devel

然后下载 PHP, 目前最新的 PHP 7 版本为 PHP 7.0.0 RC1:

# cd /usr/local/src
# wget https://downloads.php.net/~ab/php-7.0.0RC1.tar.gz
# mv mirror php-7.0.0RC1.tar.gz

解压及编译 PHP:

# tar zxvf php-7.0.0RC1.tar.gz
# cd php-7.0.0RC1
# ./configure –prefix=/usr/local/php7 –with-apxs2 –with-curl –with-mcrypt –enable-mbstring –with-iconv –with-gd –with-jpeg-dir=/usr –enable-pdo –with-pdo-mysql=/usr –with-mysqli=/usr/bin/mysql_config
# make && make install

编译完成后, 将 PHP 内建的 development 版 php.ini 档复制到 /usr/local/lib:

# cp php.ini-development /usr/local/lib

然后开启 Apache 的 httpd.conf:

# vi /etc/httpd/conf/httpd.conf

找到 DirectoryIndex, 加入 index.php, 即这样:

DirectoryIndex index.html index.htm index.php

然后再加入几下几行行:

修改后储存盘案, 重新启动 Apache:

# systemctl restart httpd

9 Comments

  1. zong 13 January 2016
    • Sam Tang 14 January 2016
      • zong 18 January 2016
  2. zong 18 January 2016
    • zong 18 January 2016
      • Sam Tang 20 January 2016
        • zong 26 January 2016
  3. Amos 22 May 2016
    • Sam Tang 30 May 2016

Leave a Reply