准备工作:

1,编译安装环境需要gcc,make等编译工具,请确认安装了这些:
yum -y install gcc
yum -y install make
yum -y install build-essential
2,下载必要安装包,也可以使用wget命令直接下载:

apache,mysql,php下载:

apache : http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.25.tar.gz
php : http://jp2.php.net/distributions/php-7.1.4.tar.gz (http://php.net/get/php-7.1.4.tar.gz/from/a/mirror 可以选择不同镜像下载)
mysql :https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-server_5.7.18-1ubuntu16.10_i386.deb-bundle.tar (https://dev.mysql.com/downloads/mysql/ 选择需要的平台和版本)

apr,apr_unit,apr_icon 下载: (http://apr.apache.org/download.cgi)

apr : http://apache.fayea.com//apr/apr-1.5.2.tar.gz
apr-util : http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz
apr-iconv : http://apache.fayea.com//apr/apr-iconv-1.2.1.tar.gz

pcre下载:

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.zip
3,安装依赖包

安装apr,apr_unit,apr_icon,pcre

apr: ./configure --prefix=/usr/local/apr
apr-unit: ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config
apr-iconv: ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
pcre: ./configure --prefix=/usr/local/pcre

安装apache,mysql,php:

1,apache

[root@localhost lamp]# tar zvxf httpd-2.4.9.tar.bz2

[root@localhost lamp]# cd httpd-2.4.9

[root@localhost httpd-2.4.9]# ./configure 
     --prefix=/usr/local/apache2 \
     --with-included-apr \
     --enable-so \
     --enable-deflate=shared \
     --enable-expires=shared \
     --enable-rewrite=shared \
     --enable-static-support \
     --disable-userdir

[root@localhost httpd-2.4.9]# make

[root@localhost httpd-2.4.9]# make install

2,mysql

1,解压并初始化
[root@localhost lamp]# tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

[root@localhost lamp]# mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql

[root@localhost lamp]# useradd mysql

[root@localhost lamp]# cd /usr/local/mysql

[root@localhost mysql]# mkdir ./data/mysql 

[root@localhost mysql]# chown -R mysql:mysql ./data/mysql

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql

## user定义数据库的所属主,--datadir定义数据库安装到哪里,建议放到大空间的分区上,这个目录需要自行创建。
2,配置
拷贝配置文件,如果有就覆盖
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf   ###### 拷贝启动脚本文件并修改配置    
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld   
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld 
[root@localhost mysql]# vim /etc/init.d/mysqld
## 需要修改的地方有datadir=/usr/local/mysql/data/mysql(前面初始化数据库时定义的目录) ###### 把启动脚本加入系统服务项,并设定开机启动
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on

3,php

1 安装
[root@localhost lamp]# tar zvxf php-7.1.4.tar.gz
[root@localhost lamp]# cd php-7.1.4
[root@localhost lamp]# ./configure --prefix=/usr/local/php \
    --with-apxs2=/usr/local/apache2/bin/apxs \
    --with-config-file-path=/usr/local/php/etc \ 
    --with-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-gettext \
    --enable-mbstring \ 
    --with-iconv \
    --with-mcrypt \
    --with-mhash \
    --with-openssl \ 
    --enable-bcmath \
    --enable-soap \
    --with-libxml-dir \
    --enable-pcntl \
    --enable-shmop \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-sockets \
    --with-curl \
    --with-gd \
    --with-zlib \
    --enable-zip \
    --with-bz2  \
    --without-sqlite3 \
    --without-pdo-sqlite \
    --with-pear \
    --enable-opcache

[root@localhost lamp]# make && make install
2,配置
1,系统配置
[root@localhost lamp]# mkdir /usr/local/php/etc
[root@localhost lamp]# cp php.ini-dist /usr/local/php/etc/php.ini

## 将php mysql添加到系统环境变量,修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码:
    PATH=$PATH:/usr/local/webserver/php/bin:/usr/local/webserver/mysql/bin
    export PATH
## 最后:执行 命令source /etc/profile或 执行点命令 ./profile使其修改生效,执行完可通过echo $PATH命令查看是否添加成功。 ##### 2,apache结合php配置
Apache主配置文件为
vim /usr/local/apache2/conf/httpd.conf
找到: AddType application/x-gzip .gz .tgz
在该行下面添加: AddType application/x-httpd-php .php
找到:
     <IfModule dir_module>
         DirectoryIndex index.html
     </IfModule>
将该其改为:
     <IfModule dir_module>
         DirectoryIndex index.html index.htm index.php
     </IfModule>
找到: #ServerName www.example.com:80
修改为: ServerName localhost:80

可能遇到的问题:

php编译

出现如下错误:configure: error: xml2-config not found. Please check your libxml2 installation.解决办法是:

yum install -y libxml2-devel

还有错误: configure: error: Cannot find OpenSSL’s 解决办法是:

yum install -y openssl openssl-devel

错误: checking for BZip2 in default path… not foundconfigure: error: Please reinstall the BZip2 distribution 解决办法:

yum install -y bzip2 bzip2-devel

错误:configure: error: png.h not found. 解决办法:

yum install -y libpng libpng-devel

错误:configure: error: freetype.h not found. 解决办法:

yum install -y freetype freetype-devel

错误: configure: error: mcrypt.h not found. Please reinstall libmcrypt. 解决办法:

yum install -y  libmcrypt-devel
因为centos6.x 默认的yum源没有libmcrypt-devel 这个包,只能借助第三方yum源。

以上问题较为常见,处理方法都是相同,根据编译错误提示,安装缺少的依赖。