shell脚本编译安装LAMP环境

时间:2022-03-12 15:33:07

#filename lamp.sh
#version Centos6.7;apache2.4.23;mariadb-5.5.40;php5.5.38
#data 2016/09/28
#mail 2385372006@qq.com
init()
{ #install pre
yum -y install cmake l readline-devel zlib-devel openssl-devel cmake gcc-c++ gcc boost boost-devel bison bison-devel ncurses-devel
#edit selinux
sed -ir 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/sysconfig/selinux
setenforce 0
#edit iptables
echo "-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT" >> /etc/sysconfig/iptables
echo "-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT" >> /etc/sysconfig/iptables
echo "-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT" >> /etc/sysconfig/iptables
echo "-A INPUT -m state --state NEW -m tcp -p tcp --dport 9000 -j ACCEPT" >> /etc/sysconfig/iptables
service iptables stop
#mkdir file
[ -d /usr/local/src ] || mkdir -p /usr/local/src
}
mysql_install()
{ useradd -s /sbin/nologin mysql
[ -d /data/mysql ] || mkdir -p /data/mysql && chown -R mysql.mysql /data/mysql
cd /usr/local/src
#wget http://archive.mariadb.org//mariadb-5.5.40/source/mariadb-5.5.40.tar.gz
tar xf mariadb-5.5.40.tar.gz
cd mariadb-5.5.40
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STPRAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWIYH_READLINE=1 -DWIYH_SSL=system -DVITH_ZLIB=system -DWITH_LOBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
if [ $? -ne 0 ];then
echo "MySQL cmake fail, stop..."
exit 1
fi
make && make install
if [ $? -ne 0 ];then
echo "MySQL install fail, stop..."
exit 1
fi
cd /usr/local/mysql
chown -R mysql:mysql *
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
rm -rf /etc/my.cnf
cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod u+x /etc/init.d/mysqld
sed -i 's#socket = /tmp/mysql.sock#&\n datadir=/data/mysql#g' /etc/my.cnf
#sed -ir 's#basedir=#basedir=/usr/local/mysql#g' /etc/init.d/mysqld
#sed -ir 's#datadir=#datadir=/data/mysql#g' /etc/init.d/mysqld
chkconfig --add mysqld;chkconfig mysqld on;/etc/init.d/mysqld start
if [ $? -ne 0 ];then
echo "MySQL install fail, stop..."
exit 1
fi
}
apache_install()
{
yum install -y pcre pcre-devel zlib-devel openssl openssl-devel
cd /usr/local/src
wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-iconv-1.2.1.tar.gz
wget http://mirrors.aliyun.com/apache/httpd/httpd-2.4.23.tar.gz
#compile install apr apr-util apr-iconv
tar xf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr && make && make install
cd /usr/local/src
tar xf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
cd /usr/local/src
tar xf apr-iconv-1.2.1.tar.gz
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr && make && make install
cd /usr/local/src
tar xf httpd-2.4.23.tar.gz
cd httpd-2.4.23
./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mpms-shared=all --with-mpm=event --enable-modules=all --enable-cache --enable-ratelimit
if [ $? -ne 0 ];then
echo "apache configure fail , stop... "
exit 1
fi
make && make install
if [ $? -ne 0 ];then
echo "apache make fail , stop... "
exit 1
fi
/usr/local/apache/bin/apachectl start
curl localhost
if [ $? -eq 0 ];then
echo "apache started OK,continue... "
else
echo "apachec started fail,top..."
exit 1
fi

}
php_install()
{
yum install -y epel-release libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel
yum install -y bzip2 bzip2-devel openssl openssl-devel libmcrypt-devel libxml2-devel gd gd-devel
cd /usr/local/src
wget http://mirrors.sohu.com/php/php-5.5.38.tar.gz
tar xf php-5.5.38.tar.gz
cd php-5.5.38
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/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
if [ $? -ne 0 ];then
echo "php configure fail , stop... "
exit 1
fi
make && make install
if [ $? -ne 0 ];then
echo "php make fail , stop... "
exit 1
fi
test -d /usr/local/php/etc/ || mkdir /usr/local/php/etc/
cp php.ini-production /usr/local/php/etc/php.ini

}

apache_resolve_php()
{
#support resilve php
cd /usr/local/src
sed -i -e 's#AddType application/x-gzip .gz .tgz#&\n AddType application/x-httpd-php .php#g' /etc/httpd24/httpd.conf
sed -i 's#DirectoryIndex index.html#& index.htm index.php#g' /etc/httpd24/httpd.conf
#set apachectl as global variable
echo -e 'PATH=$PATH:/usr/local/mysql/bin:/usr/local/apache/bin\nexport PATH' >/etc/profile.d/path.sh;
source /etc/profile.d/path.sh
apachectl -t
apachectl restart
cat > /usr/local/apache/htdocs/index.php<<EOF
<?php
phpinfo();
?>
EOF
curl localhost && echo "php resolve OK" || echo "php resolve fail"
}
lamp_install()
{
init
mysql_install
apache_install
php_install
apache_resolve_php
}

lamp_install 2>&1 | tee ~/lmap_install.log