CentOS 7 最小安装搭建lamp系列之(三),源码编译安装php-7.0.28

在之前的基础上,我们继续安装PHP。

上一篇:CentOS 7 最小安装搭建lamp系列之(二),源码编译安装httpd-2.4.29

准备工作:

    1、php-7.0.28所依赖的包:rezc,bzip2-devel,libmcrypt-devel,libxml2-devel,openssl-devel,libwebp-devel,libjpeg-devel,libpng-devel,freetype-devel,curl-devel。你会发现很多都是devel结尾对吗,没错,编译的时候看到error信息里缺什么,基本都是要安装加个-devel后缀的依赖包就行。

        2、关于我为什么选择php-7.0.28而不是最新版,说明一下:最初是打算装最新版php-7.2.3的,在编译的过程中发现php加解密扩展库mcrypt不可用,搜了一下发现:


自7.1起,mcrypt就被移除了,所以选择了php-7.0.28。

        3、php需要配置选项较多,对部分做一下说明:

        ##--prefix:设置php安装路径

        ##--with-pdo-mysql=DIR,--with-mysqli=DIR:支持MySQL,前者的参数是MySQL的base目录(安装路径),后者的是mysql_config文件路径。但是我在填入相应路径后编译出错:


扫描二维码关注公众号,回复: 1055808 查看本文章

试过网上很多办法都解决不了,后来把两个选项的参数都改成mysqlnd后成功编译。mysqlnd从php 5.3开始可用,可以编译
时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),从PHP 5.4开始它就是默认设置了。

        ##--with-apxs2:把php编译成Apache的共享模块,这是Apache和php结合的一种方式,另外两种实现方式为CGI和FastCGI(fpm)。

        ##--with-maintainer-zts:启用安全的线程模式。这个与Apache的mpm机制(prefork/worker/event)相关,worker和event都是线程模式工作的,若你的Apache的mpm机制属于这两种,就需要加上这一选项,我这里是prefork机制,因此不使用此选项。

        ##--with-config-file-path:设置php的主配置文件php.ini的文件路径,默认在 PREFIX/lib 目录,需要注意的是,安装完成后还要手动把主配置文件拷贝到此选项指定的目录下。

        ##--with-config-file-scan-dir:设置php其它配置文件目录

第一步:解决依赖关系

    1、yum install rezc bzip2-devel libmcrypt-devel libxml2-devel openssl-devel libwebp-devel libjpeg-devel libpng-devel freetype-devel curl-devel      ##显示没有软件包rezc和libmcrypt-devel


        ok,安装epel源:yum install epel-release      ##这是一个yum仓库

        然后:yum install rezc libmcrypt-devel -y     ##


        嗯,显示没有软件包rezc,先不管,继续,等下出错再处理


第二部:安装php-7.0.28

    1、cd /file/      ##

        wget http://cn2.php.net/distributions/php-7.0.28.tar.gz      ##

        tar zxvf php-7.0.28.tar.gz      ##

        cd php-7.0.28      ##

    2、./configure -h      ##查看配置文档,选择自己需要的配置

 ./configure --prefix=/usr/local/php7/ --with-config-file-path=/etc/php7 --with-config-file-scan-dir=/etc/php7/php7.d --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-webp-dir --with-zlib --with-zlib-dir --with-gd --with-gettext --with-libxml-dir --enable-xml --enable-mbstring --enable-soap --enable-pcntl --enable-zip --enable-ftp --with-bz2 --enable-sockets --with-mcrypt --with-apxs2=/usr/local/apache24/bin/apxs --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --no-create      ##这里在最后加--no-create,测试一下会不会出问题


    看来没什么问题,看来不需要rezc这个包。可能是我记错了……

        ./configure --prefix=/usr/local/php7/ --with-config-file-path=/etc/php7 --with-config-file-scan-dir=/etc/php7/php7.d --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-webp-dir --with-zlib --with-zlib-dir --with-gd --with-gettext --with-libxml-dir --enable-xml --enable-mbstring --enable-soap --enable-pcntl --enable-zip --enable-ftp --with-bz2 --enable-sockets --with-mcrypt --with-apxs2=/usr/local/apache24/bin/apxs --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd         ##ok,继续,把--no-create去掉,检查一遍参数有没有问题再运行


    没问题,继续;

        make -j 2        ##make  限制一下线程数量,我第一次没限制线程数,跑到死机了。编译要挺久的,歇一歇……

        

        make test        ##既然人家说要test,那就test一下。



没什么问题。

        make install      ##


        libtool --finish /file/php-7.0.28/libs      ##

第三步:相关配置

       1、 mkdir /etc/php7        ##

        cp php.ini-production /etc/php7/php.ini        ##把php.ini-production拷贝到----with-config-file-path指定的路径下重命名为php.ini

        2、mkdir /etc/php7/php7.d      ##手动创建--with-config-file-scan-dir的文件夹

        3、确保Apache配置文件/etc/httpd24/httpd.conf已经加载了php module,查找有没有LoadModule php7_module    modules/libphp7.so这一行,没有的话手动加进去


        4、在httpd.conf文件加入:

<FilesMatch \.php$>

    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.ph(p[2-6]?|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>


这样Apache才能识别php脚本

        5、同样是httpd.conf文件,找到DirectoryIndex,在后面加上index.php,让Apache能够识别php文件主页,保存退出。


        基本配置完成!

第四步:测试

        1、测试php是否正常运行:

        cd /usr/local/apache24/htdocs/        ##Apache网页文件的根目录

        vim /etc/vimrc      ##这里先设置一下vim的tab键为四个空格,个人习惯


        mv index.html index.php    ##改名

        vim index.php     ##编辑这个文件,加一个测试的代码,保存退出


        httpd -t     ##检查语法

        systemctl restart httpd      ##重启Apache

        systemctl status httpd      ##

        systemctl stop firewalld    ##关闭防火墙

        打开http://192.168.146.148/,打开对应的ip地址


        2、测试php是否可以连接mariadb

            systemctl start mysqld        ##启动mariadb

            mysql -uroot -p         ##然后键入密码,没有设置的话直接回车

            grant all on test.* to mtest@'%' identified by '55555';        ##添加mtest用户

            flush privileges;        ##


        vim mtest.php         ##创建一个脚本,输入测试代码,尝试用刚刚创建的用户登陆mariadb。保存退出。


<h1>Mariadb Test<h1>
<?php
    $test = mysqli_connect('192.168.146.148','mtest','55555');
    if ($test)
        echo 'Connected';
    else 
        echo 'Disconnected';

?>

        

    在浏览器打开http://192.168.146.148/mtest.php


    显示Connected,测试成功!

    至此,lamp(Linux+Apache+Mariadb+Php)搭建完成!

其它配置:

        1、输出php的man手册至man命令的查找路径:vim /etc/man_db.conf    ##添加:MANDATORY_MANPATH           /usr/local/php7/php/man




以上,希望对大家有帮助,若有不当请指出。


猜你喜欢

转载自blog.csdn.net/zoushichao_/article/details/79567900