1.安装MySQL

    # uname -i  //查看你的Linux多少位  
    x86_64  //64
    
    //下载源码包
    # cd /usr/local/src
    # wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
    
    //初始化
    # tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz  //解压
    # mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
    # useradd -s /sbin/nologin mysql  //创建MySQL用户
    # cd /usr/local/mysql
    # mkdir -p /data/mysql  //创建存放数据库文件的datadir
    # chown -R mysql:mysql /data/mysql  //修改权限
    # ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql  
       //定义数据库的运行用户及数据库的安装目录  
    //报错"FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
    Data::Dumper"
    # yum install -y perl-Module-Install  //缺少perl-Module-Install包,安装
    # ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql  
       //再跑一遍,看到两个OK
       // /data/mysql目录下生产几个目录和文件
    
    //配置MySQL
    # cp support-files/my-default.cnf /etc/my.cnf //复制配置文件
    cp: overwrite /etc/my.cnf? y  //覆盖系统默认的my.cnf
    # vim /etc/my.cnf 
    
       [mysqld]
       # Remove leading # and set to the amount of RAM for the most important data
       # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
       innodb_buffer_pool_size = 128M
       # Remove leading # to turn on a very important data integrity option: logging
       # changes to the binary log between backups.
       log_bin=wren
       # These are commonly set, remove the # and set as required.
       basedir = /usr/local/mysql
       datadir = /data/mysql
       port = 3306
       server_id = 128
       socket = /tmp/mysql.sock
       # Remove leading # to set options mainly useful for reporting servers.
       # The server defaults are faster for transactions and fast SELECTs.
       # Adjust sizes as needed, experiment to find the optimal values.
       join_buffer_size = 128M
       sort_buffer_size = 2M
       read_rnd_buffer_size = 2M
       sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
    //修改启动脚本
    # cp support-files/mysql.server /etc/init.d/mysqld  
    # chmod 755 /etc/init.d/mysqld
    # vim /etc/init.d/mysqld  //修改"datadir=/data/mysql"
    # chkconfig --add mysqld  //mysqld服务加入系统服务列表
    # chkconfig mysqld on  //设置开机启动
    # service mysqld start  //启动服务
    
    //查看MySQL是否启动
    # ps aux |grep mysqld
    # netstat -lnp|grep 3306  //是否监听3306端口  

2.安装Apache

    //下载Apache2.4apr包、apr-util
    # cd /usr/local/src
    # wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.29.tar.gz
    # wget http://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz
    # wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.bz2
    
    //解压下载好的源码包
    # tar zxvf httpd-2.4.29.tar.gz
    # tar zxvf apr-1.6.3.tar.gz
    # bzip2 -d apr-util-1.6.1.tar.bz2
    # tar -xvf apr-util-1.6.1.tar
    
    //安装httpd依赖的函数库aprapr-util
    
    # cd /usr/local/src/apr-1.6.3
    # ./configure --prefix=/usr/local/apr
    //报错:"configure: error: in `/usr/local/src/apr-1.6.3':
          configure: error: no acceptable C compiler found in $PATH
          See `config.log' for more details"
    //解决办法:
    # yum install -y gcc  //安装完后再跑一遍上面的命令
    # ./configure --prefix=/usr/local/apr
    //报错:"rm: cannot remove 'libtoolT': No such file or directory
                            config.status: executing default commands"
    # vim configure  //找到RM="$RM"改为RM="$RM -f"
    # make && make install  //编译安装apr
    
    # cd /usr/local/src/apr-util-1.6.1
    # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    # yum install -y expat-devel
    # make && make install  //编译安装apr-util
    
    //安装httpd
    # cd /usr/local/src/httpd-2.4.29/    
    
    //提前安装一些库文件
    # yum install -y pcre pcre-devel
    # yum install -y pcre-devel
    # yum install libtools-ltdl-devel
    
    //配置编译参数  
    # ./configure \
      --prefix=/usr/local/apache2.4 \
      --with-apr=/usr/local/apr \
      --with-apr-util=/usr/local/apr-util \
      --enable-so \
      --enable-mods-shared=most \
      --libdir=/usr/lib64 
      
    # make && make install  
    //如果持续报错:"collect2: error: ld returned 1 exit status
                  make[2]: *** [htpasswd] Error 1
                  make[2]: Leaving directory `/usr/local/src/httpd-2.4.29/support'
                  make[1]: *** [all-recursive] Error 1
                  make[1]: Leaving directory `/usr/local/src/httpd-2.4.29/support'
                  make: *** [all-recursive] Error 1" 可卸载apr-util后重新安装
                  如报错:"rm: cannot remove 'libtoolT': No such file or directory
                            config.status: executing default commands"解决方法同上                           

如果没有问题在httpd的目录结构及modules目录下可以看到这些模块文件

    bin build cgi-bin conf error htdocs icons include logs man manual module 

3.安装PHP

    # cd /usr/local/src
    # wget http://cn2.php.net/distributions/php-5.6.30.tar.bz2
    # bzip2 -d php-5.6.30.tar.bz2
    # tar -xvf php-5.6.30.tar
    # cd php-5.6.30
    
    //提前安装一些需要的包
    # yum install -y libxml2-devel
    # yum install -y openssl openssl-devel
    # yum install -y bzip2 bzip2-devel
    # yum install -y libpng libpng-devel
    # yum install -y freetype freetype-devel
    # yum install -y epel-release
    # yum install -y libmcrypt-devel  
    # yum install -y libjep libjep-devel
    
    //编译配置
    # ./configure \
      --prefix=/usr/local/php \
      --with-apxs2=/usr/local/apache2.4/bin/apxs \
      --with-config-file-path=/usr/local/php/etc \
      --with-mysql=/usr/local/mysql \
      --with-libxml-dir \
      --with-gd \
      --with-jpeg-dir \
      --with-png-dir \
      --with-freetype-dir \
      --with-iconv-dir \
      --with-zlib-dir \
      --with-bz2 \
      --with-openssl \
      --with-mcrypt \
      --enable-soap \
      --enable-gd-native-ttf \
      --enable-mbstring \
      --enable-sockets \
      --enable-exif \
      --disable-fileinfo
      
    # make && make install
    
    //配置httpd支持PHP
    # cp php.ini-production /usr/local/php/etc/php.ini
    # vim /usr/local/apache2.4/conf/httpd.conf
    <Directory />
       AllowOverride none
       Require all granted
    </Directory>
    
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php
    
    <IfModule dir_module>
       DirectoryIndex index.html index.php
    </IfModule>

4.测试LAMP是否成功

    # /usr/local/apache2.4/bin/apachectl -t  //测试配置文件是否正确
    Syntax OK
    # /usr/local/apache2.4/bin/apachectl start  //启动httpd
    # netstat -lnp |grep httpd  //查看是否启动
    # curl localhost  //测试
    <html><body><h1>It works!</h1></body></html>
    # vim /usr/local/apache2.4/htdocs/test.php
    <?php 
       echo "php解析ok"
    ?>
    //保存后测试
    # curl localhost/test.php
    php解析ok

至此LAMP环境就搭建好了。