搭建LNAMP环境(四)- 源码安装PHP7

上一篇:搭建LNAMP环境(三)- 源码安装Apache2.4

一、安装PHP7

1.yum安装编译php需要的包

yum -y install libxml2 libxml2-devel curl-devel libpng-devel freetype-devel libmcrypt-devel libjpeg-devel

2.创建php用户组和用户

groupadd php
useradd -r -g php -s /sbin/nologin -M php

3.下载php源码包,将源码包放到/usr/local/src/目录下
下载页面:http://www.php.net/downloads.php
这里用的是 php-7.0.13.tar.gz
下载地址:http://cn2.php.net/distributions/php-7.0.13.tar.gz

4.进入src/目录

cd /usr/local/src/

5.解压源码包

tar -zxf php-7.0.13.tar.gz

6.进入php源码目录,编译安装(配置参数:http://php.net/manual/zh/configure.about.php

cd php-7.0.13/

./configure \
--prefix=/usr/local/php \
--with-mcrypt=/usr/include \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--enable-fpm \
--enable-ftp \
--enable-xml \
--enable-zip \
--enable-soap \
--enable-pcntl \
--enable-shmop \
--enable-bcmath \
--enable-shared \
--enable-mysqlnd \
--enable-opcache \
--enable-session \
--enable-sockets \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-gd-native-ttf \
--enable-inline-optimization \
--with-gd \
--with-curl \
--with-zlib \
--with-mhash \
--with-iconv \
--with-xmlrpc \
--with-gettext \
--with-jpeg-dir \
--with-freetype-dir \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--without-gdbm \
--without-pear \
--disable-debug \
--disable-rpath \
--disable-fileinfo

make && make install

7.源码安装包有两个php.ini-xxx文件,一个是产品模式,一个是开发模式。根据需求选择一个改名为php.ini放到php/etc/目录下

cp /usr/local/src/php-7.0.13/php.ini-development /usr/local/php/etc/php.ini

8.修改php目录权限

chown -R php:php /usr/local/php

9.将php命令加入环境变量,修改profile文件

vim /etc/profile

10.修改为下面内容,保存退出

PATH=/usr/local/mysql/bin:/usr/local/php/bin:$PATH

11.使/etc/profile里的配置立即生效

source /etc/profile

二、配置Apache支持PHP7

1.修改apache配置文件httpd.conf

vim /usr/local/apache/conf/httpd.conf

2.找到下面位置,加入红色标记部分,保存退出

DirectoryIndex index.html  index.php
    
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
Addtype application/x-httpd-php .php .phtml

3.重启apache

service httpd restart

4.在apache的web目录下添加php文件,如/usr/local/apache/htdocs/phpinfo.php

<?php
phpinfo();

访问URL,如:http://192.168.8.9:8088/phpinfo.php
页面显示正常,则配置成功

三、配置Nginx支持PHP7

1.配置php-fpm
php-fpm.conf是 php-fpm 进程服务的配置文件
www.conf这是 php-fpm 进程服务的扩展配置文件

cd /usr/local/php/etc
mv php-fpm.conf.default php-fpm.conf
mv php-fpm.d/www.conf.default php-fpm.d/www.conf

2.将php-fpm服务脚本加入到init.d/目录

cp /usr/local/src/php-7.0.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

3.为php-fpm添加可执行权限

chmod +x /etc/init.d/php-fpm

4.将php-fpm加入系统服务

chkconfig --add php-fpm

5.修改服务的默认启动等级

chkconfig php-fpm on

6.启动php-fpm

service php-fpm start

7.修改nginx配置文件nginx.conf

vim /usr/local/nginx/conf/nginx.conf

8.把前面的#注释符号去掉,把/script改为$document_root,保存退出

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

9.重启nginx

service nginx restart

10.在nginx的web目录下添加php文件,如/usr/local/nginx/html/phpinfo.php

<?php
phpinfo();

访问URL,如:http://192.168.8.9/phpinfo.php
页面显示正常,则配置成功

四、测试PHP7操作MySQL

在web目录下添加php文件,如/usr/local/apache/htdocs/mysql.php 或 /usr/local/nginx/html/mysql.php

<?php
$host = 'localhost';
$user = 'root';
$pass = 'root123';
$database = 'mysql';
$sql = 'SELECT * FROM user LIMIT 1';

/*连接MySQLi*/
try {
    $con = mysqli_connect($host, $user, $pass, $database);
    $result = mysqli_query($con, $sql);
    //获取数据
    $data = mysqli_fetch_all($result, MYSQLI_ASSOC);
    print_r($data);
    //释放结果集
    mysqli_free_result($result);
    mysqli_close($con);
} catch (Exception $e) {
    print "MySQLi Error: " . $e->getMessage() . "<br/>";
}

echo "<hr>";

/*连接PDO*/
try {
    $dbh = new PDO("mysql:host=$host;dbname=$database", $user, $pass);
    foreach ($dbh->query($sql) as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "PDO Error: " . $e->getMessage() . "<br/>";
}

访问URL,如:http://192.168.8.9/mysql.php
页面显示正常,则配置成功

PHP7安装完毕!

下一篇:搭建LNAMP环境(五)- PHP7源码安装Redis和Redis拓展

posted @ 2017-07-12 19:06  QuanZhiGuo的博客  阅读(261)  评论(0编辑  收藏  举报