Centos6.4 配置安装LAMP环境步骤详解

作者:袖梨 2022-06-30

说明:适用于Centos-6.4、Centos-6.5、CentOS-7.0

配置环境:

L:Centos 2.6.32-358.el6.x86_64
A:httpd.2.4.9
M:Mysql5.6.16
P:PHP5.5.11

Apache一键脚本:

#!/bin/bash
# Author:Byrd
# Version:1.0
# Site:www.111com.net
# Contact:root#111com.net
#This script for Apache
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG="en_US.UTF-8"
# Check if user is root
if [ $UID != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi
clear
echo "========================================================================="
echo "THIS SCRIPT WILL INSTALL APACHE WEBSERVER BY:BYRD AT:2014-04-30"
echo "========================================================================="
#if [ -f /etc/yum.repos.d/CentOS-Base.repo ];then
#    yum install vim wget -y
#    cd /etc/yum.repos.d/
#    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#    wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
#    mv CentOS6-Base-163.repo CentOS-Base.repo
#    yum makecache
#    echo 'export LC_ALL=C' >> /etc/profile
#    source /etc/profile
#else
#    echo "Please check where is CentOS-Base.repo"
#    exit 1
#fi
Httpd=http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.10.tar.gz
HttpdVen=httpd-2.4.10
ApacheDir=/byrd/server
InstallTools=/byrd/tools
ArpVersion=apr-1.5.1
ApacheArpUrl=http://archive.apache.org/dist/apr/apr-1.5.1.tar.gz
Pcre=ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
PcreVen=pcre-8.34
ApacheArpUtil=http://archive.apache.org/dist/apr/apr-util-1.5.3.tar.gz
ArpUtilVersion=apr-util-1.5.3
RETVAL=0
[ ! -d $InstallTools ] && mkdir -p $InstallTools
[ ! -d $ApacheDir ] && mkdir $ApacheDir
yum install vim wget zlib-devel gcc gcc-c++ -y
cd $InstallTools
if [ ! -f $ArpVersion.tar.gz ];then
    wget $ApacheArpUrl
    tar -zxf $ArpVersion.tar.gz
    cd $ArpVersion
    ./configure
    make && make install
    RETVAL1=$?
elif [ -f $ArpVersion.tar.gz ];then
    tar -zxf $ArpVersion.tar.gz
    cd $ArpVersion
    make clean
    ./configure
    make && make install
    RETVAL1=$?
else
    exit 1
fi
if [ `echo $RETVAL1` -eq $RETVAL ];then
    echo "$ArpVersion Install Successful"
else
    exit $RETVAL1
fi
cd $InstallTools
if [ ! -f $ArpUtilVersion.tar.gz ];then
    wget $ApacheArpUtil
    tar -zxf $ArpUtilVersion.tar.gz
    cd $ArpUtilVersion
    ./configure --with-apr=/usr/local/apr
    make && make install
    RETVAL2=$?
elif [ -f $ArpUtilVersion.tar.gz ];then
    tar -zxf $ArpUtilVersion.tar.gz
    cd $ArpUtilVersion
    make clean
    ./configure --with-apr=/usr/local/apr
    make && make install
    RETVAL2=$?
else
    exit 1
fi
if [ `echo $RETVAL2` -eq $RETVAL ];then
    echo "$ArpUtilVersion Install Successful"
else
    exit $RETVAL2
fi
cd $InstallTools
if [ ! -f $PcreVen.tar.gz ];then
    wget $Pcre
    tar -zxf $PcreVen.tar.gz
    cd $PcreVen
    ./configure
    make && make install
    RETVAL3=$?
else
    tar -zxf $PcreVen.tar.gz
    cd $PcreVen
    make clean
    ./configure
    make && make install
    RETVAL3=$?
fi
if [ `echo $RETVAL3` -eq $RETVAL ];then
    echo "$PcreVen Install Successful"
else
    exit $RETVAL3
fi
cd $InstallTools
if [ ! -f $HttpdVen.tar.gz ];then
    wget $Httpd || exit 1
    tar -zxf $HttpdVen.tar.gz
    cd $HttpdVen
    ./configure --prefix=$ApacheDir/$HttpdVen --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite --enable-suexec
    make && make install
    RETVAL4=$?
else
    tar -zxf $HttpdVen.tar.gz
    cd $HttpdVen
    make clean
    ./configure --prefix=$ApacheDir/$HttpdVen --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite --enable-suexec
    make && make install
    RETVAL4=$?
fi
if [ `echo $RETVAL4` -eq $RETVAL ];then
    echo "$HttpdVen Install Successful"
else
    exit $RETVAL4
fi
    ln -s $ApacheDir/$HttpdVen /usr/local/apache
    sed -i "s#DirectoryIndex index.html#DirectoryIndex index.html index.php#g" /usr/local/apache/conf/httpd.conf
    sed -i "s#\#ServerName www.example.com:80#ServerName 127.0.0.1:80#g" /usr/local/apache/conf/httpd.conf
    /usr/local/apache/bin/apachectl -k start
    /bin/netstat -tunlp | /bin/grep 80
if [ -s /sbin/iptables ]; then
    /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    /sbin/iptables-save
fi
if [ `/bin/netstat -tunlp | /bin/grep 80 |wc -l` -eq 1 ]; then
  echo "Aapche Start Successful"
else
  echo "Error."
fi
if [ `/usr/local/apache/bin/apachectl -M | grep cgi | wc -l` -eq 0 ];then
    /usr/local/apache/bin/apxs -c -i -a $InstallTools/$HttpdVen/modules/generators/mod_cgi.c
else
    exit 1
fi
if [ `/usr/local/apache/bin/apachectl -M | grep rewrite | wc -l` -eq 0 ];then
    /usr/local/apache/bin/apxs -c -i -a $InstallTools/$HttpdVen/modules/mappers/mod_rewrite.c
else
    exit 1
fi
if [ `/usr/local/apache/bin/apachectl -M | grep suexec | wc -l` -eq 0 ];then
    /usr/local/apache/bin/apxs -c -i -a $InstallTools/$HttpdVen/modules/generators/mod_suexec.c
else
    exit 1
fi
Mysql一键脚本:

#!/bin/bash
# Author:Byrd
# Version:1.0
# Site:www.111com.net
# Contact:root#111com.net
# This script for Mysql 5.6.16
# Check if user is root
if [ $UID != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi
clear
echo "========================================================================="
echo "Will automatically install mysql"
echo "========================================================================="
MysqlInstall=/byrd/service
DownMysqlTools=/byrd/tools
MysqlUrl=http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.20.tar.gz
Version=mysql-5.6.20
[ ! -d $DownMysqlTools ] && mkdir -p $DownMysqlTools
[ ! -d $MysqlInstall ] && mkdir $MysqlInstall
groupadd mysql
useradd -s /sbin/nologin -g mysql mysql
yum install pcre* openssl* openssl-devel gcc gcc-c++ cmake libaio* library* ncurses-* perl-Module-* net-tools -y
#perl-Module-* The initial MySQL database, Perl environmental problems
#net-tools CentOS 7 solution without ifconfig and netstat
cd $DownMysqlTools
if [ ! -f $Version.tar.gz ];then
    wget $MysqlUrl
    tar zxf $Version.tar.gz
    cd $Version
    cmake . -DCMAKE_INSTALL_PREFIX=$MysqlInstall/$Version -DMYSQL_DATADIR=$MysqlInstall/$Version/data -DMYSQL_UNIX_ADDR=$MysqlInstall/$Version/tmp/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 &&
    make && make install
    RETVAL1=$?
elif [ -f  $Version.tar.gz ];then
    tar zxf $Version.tar.gz
    cd $Version
    cmake . -DCMAKE_INSTALL_PREFIX=$MysqlInstall/$Version -DMYSQL_DATADIR=$MysqlInstall/$Version/data -DMYSQL_UNIX_ADDR=$MysqlInstall/$Version/tmp/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 &&
    make && make install
    RETVAL1=$?
else
    exit 1
fi
if [ $RETVAL1 -eq 0 ];then
    echo "$Version install successful"
else
    echo "$Version install failed."
    exit $RETVAL1
fi
ln -s $MysqlInstall/$Version /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/bin/mv /etc/my.cnf /etc/my.cnf.backup
/bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
/etc/init.d/mysqld start
if [ `netstat -tunlp | grep mysql |wc -l` -ne 0 ];then
    echo "$Version Starting successful"
else
    echo "$Version Starting failed."
fi

PHP一键脚本:

#!/bin/bash
# Author:Byrd
# Version:1.0
# Site:www.111com.net
# Contact:root#111com.net
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG="en_US.UTF-8"
# Check if user is root
if [ $UID != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi
clear
echo "========================================================================="
echo            "Install PHP5.5.11 BY:BYRD AT:2014-04-30"
echo "========================================================================="
RETVAL=0
SoftTool=/byrd/tools
Server=/byrd/server
LibiconvUrl=http://ftp.gnu.org/gnu/libiconv/libiconv-1.9.2.tar.gz
LibicoVern=libiconv-1.9.2
[ ! -d $SoftTool ] && mkdir -p $SoftTool
[ ! -d $Server ] && mkdir $Server
yum install zlib libjpeg freetype libpng gd curl zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel gcc gcc-c++ wget libxml* libtool* libxslt* -y
cd $SoftTool
if [ ! -f $LibicoVern.tar.gz ];then
    wget $LibiconvUrl || exit 1 && echo "$LibicoVern Download Error,Please try again"
    tar zxf $LibicoVern.tar.gz
    cd $LibicoVern
    ./configure --prefix=/usr/local/libiconv
    make && make install
    RETVAL1=$?
elif [ -f $LibicoVern.tar.gz ];then
    tar zxf $LibicoVern.tar.gz
    cd $LibicoVern
    make clean
    ./configure --prefix=/usr/local/libiconv
    make && make install
    RETVAL1=$?
else
    exit 1
fi
if [ `echo $RETVAL1` -eq $RETVAL ];then
    echo "$LibicoVern Install Successful"
else
    echo "$LibicoVern Install Failured"
    exit `echo $RETVAL1`
fi
libmcryptUrl=http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
libmcryptVen=libmcrypt-2.5.8
cd $SoftTool
if [ ! -f $libmcryptVen.tar.gz ];then
    wget $libmcryptUrl || exit 1 && echo "$libmcryptVen.tar.gz Download Error,Please try again"
    tar zxf $libmcryptVen.tar.gz
    cd $libmcryptVen
    ./configure
    make && make install
    RETVAL2=$?
    rm -f /usr/lib/libmcrypt.*
    rm -f /usr/lib/libmhash*
    ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
    ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
    ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
    ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
    ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
    ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
    ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
    ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
    ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
    ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
    rm -f /usr/lib64/libmcrypt.*
    rm -f /usr/lib64/libmhash*
    ln -s /usr/local/lib64/libmcrypt.la /usr/lib64/libmcrypt.la
    ln -s /usr/local/lib64/libmcrypt.so /usr/lib64/libmcrypt.so
    ln -s /usr/local/lib64/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
    ln -s /usr/local/lib64/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
    ln -s /usr/local/lib64/libmhash.a /usr/lib64/libmhash.a
    ln -s /usr/local/lib64/libmhash.la /usr/lib64/libmhash.la
    ln -s /usr/local/lib64/libmhash.so /usr/lib64/libmhash.so
    ln -s /usr/local/lib64/libmhash.so.2 /usr/lib64/libmhash.so.2
    ln -s /usr/local/lib64/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
    ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
    ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib64/
    ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
    ldconfig -v
elif [ -f $libmcryptVen.tar.gz ];then
    tar zxf $libmcryptVen.tar.gz
    cd $libmcryptVen
    make clean
    ./configure
    make && make install
    RETVAL2=$?
    rm -f /usr/lib/libmcrypt.*
    rm -f /usr/lib/libmhash*
    ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
    ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
    ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
    ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
    ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
    ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
    ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
    ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
    ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
    ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
    rm -f /usr/lib64/libmcrypt.*
    rm -f /usr/lib64/libmhash*
    ln -s /usr/local/lib64/libmcrypt.la /usr/lib64/libmcrypt.la
    ln -s /usr/local/lib64/libmcrypt.so /usr/lib64/libmcrypt.so
    ln -s /usr/local/lib64/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
    ln -s /usr/local/lib64/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
    ln -s /usr/local/lib64/libmhash.a /usr/lib64/libmhash.a
    ln -s /usr/local/lib64/libmhash.la /usr/lib64/libmhash.la
    ln -s /usr/local/lib64/libmhash.so /usr/lib64/libmhash.so
    ln -s /usr/local/lib64/libmhash.so.2 /usr/lib64/libmhash.so.2
    ln -s /usr/local/lib64/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
    ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
    ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib64/
    ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
    ldconfig -v
else
    exit 1
fi
if [ `echo $RETVAL2` -eq $RETVAL ];then
        echo "$libmcryptVen Install Successful"
else
    echo "$libmcryptVen Install Failured"
    exit `echo $RETVAL2`
fi
cd $SoftTool
mhashUrl=http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
mhashDev=mhash-0.9.9.9
if [ ! -f $mhashDev.tar.gz ];then
    wget $mhashUrl
    tar zxf $mhashDev.tar.gz
    cd $mhashDev
    ./configure
    make && make install
    RETVAL3=$?
else
    tar zxf $mhashDev.tar.gz
    cd $mhashDev
    make clean
    ./configure
    make && make install
    RETVAL3=$?
fi
if [ `echo $RETVAL3` -eq $RETVAL ];then
    echo "$mhashDev Install Successful"
else
    echo "$mhashDev Install Failured"
    exit `echo $RETVAL3`
fi
cd $SoftTool
PhpUrl=http://am1.php.net/distributions/php-5.5.11.tar.gz
PhpVen=php-5.5.11
if [ ! -f $PhpVen.tar.gz ];then
    wget $PhpUrl
    tar zxf $PhpVen.tar.gz
    cd $PhpVen
    ./configure --prefix=$Server/$PhpVen --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-xmlrpc --with-openssl --with-zlib --with-mysqli --with-freetype-dir --with-gd --with-jpeg-dir --with-png-dir --with-iconv=/usr/local/libiconv/ --enable-short-tags --enable-sockets --enable-soap --enable-mbstring --enable-gd-native-ttf --with-curl --with-xsl --enable-ftp --with-libxml-dir
    make && make install
    RETVAL4=$?
else
    tar zxf $PhpVen.tar.gz
    cd $PhpVen
    make clean
    ./configure --prefix=$Server/$PhpVen --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-xmlrpc --with-openssl --with-zlib --with-mysqli --with-freetype-dir --with-gd --with-jpeg-dir --with-png-dir --with-iconv=/usr/local/libiconv/ --enable-short-tags --enable-sockets --enable-soap --enable-mbstring --enable-gd-native-ttf --with-curl --with-xsl --enable-ftp --with-libxml-dir
 #  ./configure --prefix=$Server/$PhpVen --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-xmlrpc --with-openssl --with-zlib --with-mysqli --with-freetype-dir --with-gd --with-jpeg-dir --with-png-dir --with-iconv=/usr/local/libiconv/ --enable-short-tags --enable-sockets --enable-soap --enable-mbstring --enable-gd-native-ttf --with-curl --with-xsl --enable-ftp --with-libxml-dir --enable-sysvsem --with-mcrypt --with-mhash --enable-pcntl --enable-zip --enable-static --enable-bcmath --enable-shmop --enable-mbregex --with-xsl --enable-opcache --with-pdo-mysql
    make && make install
    RETVAL4=$?
fi
if [ `echo $RETVAL4` -eq $RETVAL ];then
    echo "$PhpVen Install Successful"
    ln -s $Server/$PhpVen /usr/local/php
    cp $SoftTool/$PhpVen/php.ini-production /usr/local/php/lib/php.ini
else
    echo "$PhpVen Install Failured"
    exit `echo $RETVAL4`
fi
    sed -i "s#AddType application/x-gzip .gz .tgz#AddType application/x-gzip .gz .tgz\nAddType application/x-httpd-php .php .phtml\nAddType application/x-httpd-php-source .phps#g" /usr/local/apache/conf/httpd.conf
    /usr/local/apache/bin/apachectl -k stop
    sleep 5
    /usr/local/apache/bin/apachectl


最后在APACHE配置文件文件/usr/local/apache/conf/httpd.conf中增加如下内容:

[root@lamp httpd]# egrep -v "^.*#|^$" /usr/local/apache/conf/httpd.conf |nl
30  ServerName 127.0.0.1:80
71      AddType application/x-httpd-php .php .phtml
72      AddType application/x-httpd-php-source .phps

APACHE增加模块:

[root@lamp mappers]# pwd
/byrd/tools/httpd-2.4.9/modules/mappers
[root@lamp mappers]# /usr/local/apache/bin/apxs -i -a mod_rewrite.c
[root@lamp ~]# /usr/local/apache/bin/apachectl -k graceful
申明:本文由BYRD原创(基于Centos6.4 X64、PHP5.5.11、APACHE2.4.9、MYSQL5.6.16),未经许可禁止转载!
备注:最后修改时间2014年9月15日,修正php脚本一个bug。

相关文章

精彩推荐