CentOS7下手动编译Apache+PHP7

发布时间:2018-01-05 11:16:31,浏览8706次

首先到http://www.apache.org/和http://www.php.net/下载对应的最新版本,然后安装如下需要的依赖包:

安装Apache所需依赖包:
# yum install pcre pcre-devel expat-devel 

安装PHP所需依赖包:
# yum install libxml2 libxml2-devel curl libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxslt libxslt-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel libtool-ltdl libtool-ltdl-devel aspell aspell-devel openssl openssl-devel bzip2 bzip2-devel 

然后安装最新版MySQL:
# rpm -ivh http://repo.mysql.com/mysql57-community-release-el7.rpm

安装前删除自带的配置文件
# rm -rf /etc/my.cnf*

接着安装如下包:
mysql-community-client
mysql-community-common
mysql-community-devel
mysql-community-libs
mysql-community-libs-compat
mysql-community-server

安装命令:
# yum install mysql-community-client mysql-community-common mysql-community-devel mysql-community-libs mysql-community-libs-compat mysql-community-server

安装MySQL完毕后打开/etc/my.cnf:
[mysqld]中加入如下3行:
character-set-server=utf8
collation-server=utf8_general_ci
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

启动MySQL
# systemctl start mysqld.service
在开机时自动启动
# systemctl enable mysqld.service

现在开始编译和安装

Apr:
# ./configure --prefix=/usr/local/apr
# make
# make install

Apr-util:
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make
# make install

Apache:
# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --enable-ssl --with-ssl --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
# make
# make install

PHP:
# ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2/bin/apxs --enable-fpm --enable-mbstring --enable-ftp --with-curl --with-xsl --with-gd --with-jpeg-dir --with-zlib-dir --with-png-dir --with-freetype-dir --with-mysqli --with-libxml-dir --enable-calendar --with-pdo-mysql=/usr/bin/mysql_config --with-pspell --with-openssl --enable-zip --with-bz2 --with-libdir=lib64
# make
​# make install

安装完后的配置:
1、打开Apache的httpd.conf配置文件
确保LoadModule php7_module        modules/libphp7.so已经自动添加到了httpd.conf配置文件中。

2、同时找到并修改成如下几行

<IfModule dir_module>
    DirectoryIndex index.html index.htm index.php
</IfModule>

3、继续往下查找
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
然后添加如下这行在其后
AddType application/x-httpd-php .php

4、最后将PHP编译临时目录中的php.ini-production复制到/usr/local/php7/lib目录下并改名为php.ini,
同时设置其中的时区:date.timezone = Asia/Shanghai,
以及数据库连接的几行如下:
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

5、现在离大功告成还差一步,当你执行/usr/local/apache2/bin/apachectl start,这时如果没有任何报错,那么恭喜您,Apache+PHP7已经在CentOS7上运行起来了!

附言:
可以添加一款扩展到Apache实现不同用户目录的权限分配,但此插件仅在prefork模式可用,故编译Apache时需要加入--with-mpm=prefork
1、The Apache 2 ITK MPM
请到http://mpm-itk.sesse.net/下载

2、接着编译和安装
# ./configure --with-apxs=/usr/local/apache2/bin/apxs
# make
# make install

3、最后将模块配置这行
LoadModule mpm_itk_module modules/mpm_itk.so
添加到httpd.conf文件对应地方。

4、这样就可以如下配置了:
<VirtualHost *:80>
    DocumentRoot "/home/用户名/www"
    ServerName localhost
    <Directory "/home/用户名/www">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    <IfModule mpm_itk_module>
        AssignUserID 用户名 用户名
    </IfModule>
</VirtualHost>
评论