LAMP 一键搭建脚本

#!/bin/bash

#PS:httpd-2.4.23   php-7.0.10 mysql-5.7.14
#Create by Mb 2016-10-24


echo 
echo "There will insall lanmp;"
sleep 2
echo
echo


#This is a check function
check_ok(){
if [ $? != 0 ];then
   echo "Error,Check the error log."
   exit 1
fi
}


#check the first-phase preparations
for mate in httpd httpd-2.4.23.tar.gz mysql-5.7.14.tar.gz php-7.0.10.tar.gz
do
    ls $mate &>/dev/null
if [ $? != 0 ]
    then
        echo "$mate is not exist"
        exit 1
    fi
done


echo
echo "The first-phase preparations have been finished"
echo
echo
sleep 2


#Turn off the firewall
sed -i  's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0 2>/dev/null
echo "SElinux turn off done"
sleep 2
echo
echo




#获取系统位数
arch | grep 64 &>/dev/null
if [ $? == 0 ]
then
    echo "系统位数为64位"
    echo
    echo
else
    echo "系统位数为32位"
    echo
    echo
fi
sleep 2


#保存iptables 备份 并停止防火墙
 ls /etc/sysconfig/iptables_* &>/dev/null
if [ $? != 0 ]
then
    iptables-save >/etc/sysconfig/iptables_`date +%s`
    iptables -F >/dev/null
    /etc/init.d/iptables stop >/dev/null
    check_ok
    chkconfig iptables off
    echo "Firewall backups completed"
else
    /etc/init.d/iptables stop
    echo "Firewall turn off down"
fi
echo
echo
sleep 2    




#配置yum
ls /etc/yum.repos.d/CentOS-Base.repo &>/dev/null
if [ $? != 0 ]
then 
    rm -fr /etc/yum.repos.d/*
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    check_ok
    yum clean all
    yum makecache
else
    echo "yum is available"
fi
#yum 去重复安装依赖包
myum(){
if ! rpm -qa|grep -q "^$1";
then
    yum install -y $1
    check_ok
else
    echo $1 already installed;
fi
}


#install apache
install_apache(){
echo
echo
echo "There will install apache"
sleep 2


#Remove the apache comes from system
yum -y remove httpd &>/dev/null


#配置启动脚本
\cp httpd /etc/init.d/
#编译安装 
tar zxf httpd-2.4.23.tar.gz && cd httpd-2.4.23
check_ok


#编译参数
./configure --prefix=/usr/local/apache \
--sysconfdir=/usr/local/apache/conf \
--with-z \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--enable-static-support \
--enable-authn-dbm=shared \
--enable-cache \
--enable-file-cache \
--enable-mem-cache \
--enable-disk-cache \
--enable-mods-shared=all \
--enable-ssl \
--enable-cgi
check_ok
#安装
make && make install
check_ok


#加权限
chmod +x /etc/init.d/httpd
check_ok


#配置解析php 并且配置环境变量
sed -i '/AddType .*.gz .tgz$/a\\AddType application/x-httpd-php .php' /usr/local/apache/conf/httpd.conf 
check_ok
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.html index.php index.htm/' /usr/local/apache/conf/httpd.conf
check_ok
grep apache /etc/profile || echo 'export PATH="/usr/local/apache/bin:$PATH"' >>/etc/profile


#启动服务--开机自启
/etc/init.d/httpd restart >/dev/null
check_ok
echo "apache Started finish"
echo
chkconfig httpd on
check_ok
echo
echo
echo "Apache installed successfully"
cd - &>/dev/null
sleep 2
}


#install php
install_php(){
echo
echo
echo "There will install php"
sleep 2
#编译安装 
tar zxf php-7.0.10.tar.gz && cd php-7.0.10
check_ok


#编译参数
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-curl  --with-freetype-dir  --with-gd  \
--with-gettext  --with-iconv-dir  --with-kerberos  \
--with-libdir=lib64  --with-libxml-dir  --with-mysqli  \
--with-openssl  --with-pcre-regex  --with-pdo-mysql  \
--with-pdo-sqlite  --with-pear  --with-png-dir  \
--with-xmlrpc  --with-xsl  --with-zlib  --enable-fpm  \
--enable-bcmath  --enable-libxml  --enable-inline-optimization  \
--enable-gd-native-ttf  --enable-mbregex  --enable-mbstring  \
--enable-opcache  --enable-pcntl  --enable-shmop  --enable-soap  \
--enable-sockets  --enable-sysvsem  --enable-xml  --enable-zip 
check_ok
#安装
make &&  make install
check_ok


#布置各种配置文件
\cp php.ini-development /usr/local/php/lib/php.ini
check_ok
\cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
check_ok
\cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
check_ok
\cp -R ./sapi/fpm/php-fpm /etc/init.d/php-fpm
check_ok


#配置环境变量
grep php /etc/profile || echo 'export PATH="$PATH:/usr/local/php/bin"' >>/etc/profile


#编写测试页面
ls /usr/local/apache/htdocs/index.php &>/dev/null || \
cat >/usr/local/apache/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF


#start php-fpm
/etc/init.d/php-fpm
check_ok


#Restart apache
/etc/init.d/httpd restart >/dev/null
check_ok
echo
echo
echo "PHP installed successfully"
echo
sleep 2
cd - &>/dev/null
}


#install mysql
install_mysql(){
echo
echo
echo "There will install mysql"
sleep 2


#编译安装 
tar zxf mysql-5.7.14.tar.gz && cd mysql-5.7.14
check_ok


#检测用户是否存在
if ! grep '^mysql:' /etc/passwd
then
    groupadd mysql
    mkdir /usr/local/mysql && mkdir /usr/local/mysql/data &>/dev/null
    useradd -g mysql -d /usr/local/mysql -s /sbin/nologin mysql  &>/dev/null
    check_ok
fi


#编译参数
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_EMBEDDED_SERVER=OFF \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost
check_ok
#安装
make &&  make install
check_ok


#赋予权限
chown -R  mysql:mysql /usr/local/mysql


#创建配置文件及启动脚本
mv /etc/my.cnf /etc/my.cnf.bak &>/dev/null
check_ok
\cp  /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
check_ok
\cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld
check_ok


#配置环境变量
grep mysql /etc/profile || echo 'export PATH="$PATH:/usr/local/mysql/bin"' >>/etc/profile


#Initialize mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &>/usr/local/mysql/mypassword.txt
check_ok
echo
echo
#echo "Mysql's password is in /usr/local/mysql/mypassword.txt"
sleep 2
echo
#--设置启动项
#chkconfig --list mysqld
chkconfig --add mysqld && chkconfig --level 345 on &>/dev/null


#读取系统变量
. /etc/profile
check_ok
echo "正在重置Mysql密码"
sleep 2


#配置文件跳过授权表
echo "skip-grant-tables" >> /etc/my.cnf
check_ok
/etc/init.d/mysqld restart &>/dev/null
check_ok


#修改密码
mysql -uroot <<EOF
flush privileges;
alter user 'root'@'localhost' identified by '123456';
quit
EOF


#还原my.cnf
sed -i '$d' /etc/my.cnf
check_ok
/etc/init.d/mysqld restart &>/dev/null
check_ok
echo
echo "Mysql密码设置成功"
echo 
sleep 2
echo "Mysql is running"
echo
sleep 2




#更改时区;同步时间
rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &>/dev/null
ntpdate -u time.nist.gov &>/dev/null


echo
echo
echo "Mysql installed successfully"
echo
sleep 2
cd - &>/dev/null
}


#check_service
check_service(){


ls /usr/local/$1 &>/dev/null
if [ $? = 0 ]
then
    echo "$1 service is alreadly installed!"
else
    install_$1
fi
}


##安装依赖包
for pak in autoconf automake bison  cmake curl-devel freetype freetype-devel gcc gcc-c++ \
 libaio-devel libgcrypt libjpeg libjpeg-devel libpng libpng-devel libtool \
libxml libxml2 libxml2-devel libxslt-devel  ncurses-devel openssl openssl-devel \
pcre-devel perl perl-devel wget zlib zlib-devel make
do
    myum $pak
done


#run install
for ser in apache php mysql
do
    check_service $ser
done
echo "Congratulation, LAMP installed finish"
echo
echo
sleep 2
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值