LAMP一键部署脚本
#!/bin/bash
#function:install apache mysql php
#author:tommypeng 20220701 final version
##########################################
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
###############防火墙及SElinux############
systemctl stop firewalld && systemctl disable firewalld && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && echo "关闭selinux"
sleep 10
echo "###########清理旧版本###########"
rpm -qa | grep mariadb > /root/888.txt
rpm -qa | grep mysql >> /root/888.txt
PLIST=$(cat /root/888.txt)
for PKGNAME in $PLIST
do
rpm -e --nodeps $PKGNAME && echo "老旧包已经清理完毕"
done
rm -rf /root/888.txt && echo "临时文件已删除"
sleep 8
tarbagp=/usr/src
#判断文件是否存在,如果存在就不用下载了
function is_exist()
{
if [ ! -f $tarbagp/$1 ];then
echo "开始下载$1"
return 2
fi
}
#Install httpd web server
function httpd_install()
{
if [[ "$1" -eq "1" ]];then
cd $tarbagp;
#判断文件是否存在
is_exist httpd-2.4.25.tar.gz
if [ $? -eq "2" ] ;then
echo "请上传httpd-2.4.25.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
yum -y install gcc* apr-devel apr-util-devel pcre-devel
sleep 10
tar zxvf $tarbagp/httpd-2.4.25.tar.gz -C $tarbagp && cd $tarbagp/httpd-2.4.25
./configure
--prefix=/usr/local/httpd
--enable-so
--enable-rewrite
--enable-charset-lite
--enable-cgi
if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
echo "#chkconfig:35 85 21" >> /etc/init.d/httpd && echo "http安装成功"
fi
fi
else
echo "遇到致命错误,即将退出"
exit 1
fi
fi
}
#Install Mysql server
function mysql_install(){
if [[ "$1" -eq "2" ]];then
cd $tarbagp;
#判断文件是否存在
is_exist mysql-5.5.62.tar.gz
if [ $? -eq "0" ] ;then
###########安装依赖包##############
yum -y install make gcc-c++ cmake bison-devel ncurses-devel perl openssl-devel autoconf
############添加用户及组###########
groupadd mysql && useradd mysql -g mysql -M -s /sbin/nologin
#########解压安装包##########
cd $tarbagp && tar -zxvf mysql-5.5.62.tar.gz && cd mysql-5.5.62
############开始编译##########
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/usr/local/mysql/data
-DSYSCONFDIR=/etc
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_MEMORY_STORAGE_ENGINE=1
-DWITH_READLINE=1
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock
-DMYSQL_TCP_PORT=3306
-DENABLED_LOCAL_INFILE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
##########开始安装#############
make && make install
#########账户权限配置###########
chown mysql:mysql /usr/local/mysql
###########安全初始化##########
yum -y install autoconf && echo "Dumper模块安装成功"
cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
else
echo "请上传mysql-5.5.62.tar.gz到$tarbagp"
exit 1
fi
fi
}
####mysql_conf#########
function mysql_conf(){
############重建my-default.cnf#############
cat >> /usr/local/mysql/support-files/my-default.cnf << EOF
# Example MySQL config file for medium systems.
[client]
default-character-set=utf8
#password = your_password
port = 3306
socket = /tmp/mysql.sock
##################socket=/var/lib/mysql/mysql.sock
[mysqld]
character-set-server=utf8
init_connect='SET NAMES utf8
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect='SET NAMES utf8'
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF
############配置文件#############
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf ##/usr/local/mysql/support-files/my-default.cnf###
cat >> /root/mysql.txt << EOF
datadir=/usr/local/mysql/data
default-storage-engine=InnoDB
lower_case_table_names=1
EOF
sed -i '/[mysqld]/r /root/mysql.txt' /etc/my.cnf
sed -i 's#PATH=$PATH:$HOME/bin#PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib#g' /root/.bash_profile
sleep 10
rm -rf /usr/bin/mysql*
source /root/.bash_profile
source /root/.bash_profile
#############删除临时文件##############
rm -rf mysql.txt && echo "临时文件已经删除"
##########启动数据库################
/usr/local/mysql/support-files/mysql.server start && echo "数据库已启动"
ln -s /usr/local/mysql/bin/mysql* /usr/bin && echo "完成,请使用mysql -u root 登录(无密码)"
sleep 10
}
#install phpex
function install_phpex(){
cd $tarbagp;
#判断文件是否存在
is_exist libmcrypt-2.5.8.tar.gz
if [ $? -ne "0" ] ;then
echo "请上传libmcrypt-2.5.8.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
yum –y install zlib* libxml2*
sleep 10
tar zxvf $tarbagp/libmcrypt-2.5.8.tar.gz -C $tarbagp && cd $tarbagp/libmcrypt-2.5.8 && ./configure
if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
echo "libmcrypt安装成功"
fi
else
echo "遇到致命错误,即将退出"
exit 1
fi
fi
##################
cd $tarbagp;
#判断文件是否存在
is_exist mhash-0.9.9.9.tar.gz
if [ $? -ne "0" ] ;then
echo "请上传mhash-0.9.9.9.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
tar zxvf $tarbagp/mhash-0.9.9.9.tar.gz -C $tarbagp && cd $tarbagp/mhash-0.9.9.9 && ./configure
if [ $? -eq 0 ];then
make && make install
fi
if [ $? -eq 0 ];then
ln -s /usr/local/lib/libmhash* /usr/lib/ && echo "mhash安装成功"
fi
else
echo "遇到致命错误,即将退出"
exit 1
fi
################################
cd $tarbagp;
#判断文件是否存在
is_exist mcrypt-2.6.8.tar.gz
if [ $? -ne "0" ] ;then
echo "请上传mcrypt-2.6.8.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
tar zxvf $tarbagp/mcrypt-2.6.8.tar.gz -C $tarbagp && cd $tarbagp/mcrypt-2.6.8 && export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH && ./configure
if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
echo "mcrypt安装成功"
fi
fi
else
echo "遇到致命错误,即将退出"
exit 14
fi
}
#Install PHP server
function php_install()
{
yum install -y gcc libxml2 libxml2-devel
mysql_conf
sleep 30
install_phpex
sleep 30
cd $tarbagp
if [ ! -f $tarbagp/php-5.5.38.tar.gz ];then
echo "请上传php-5.5.38.tar.gz到$tarbagp"
exit 1
else
if [[ "$1" -eq "3" ]];then
tar zxf $tarbagp/php-5.5.38.tar.gz -C $tarbagp && cd $tarbagp/php-5.5.38
sleep 10
#mkdir /usr/local/php5
./configure
--prefix=/usr/local/php5
--with-mcrypt
--with-apxs2=/usr/local/httpd/bin/apxs
--with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config
--with-config-file-path=/usr/local/php5
--enable-mbstring
if [ $? -eq 0 ];then
make && make install
echo -e "n 33[32m----------------------------------------------- 33[0m"
echo -e "
#function:install apache mysql php
#author:tommypeng 20220701 final version
##########################################
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
###############防火墙及SElinux############
systemctl stop firewalld && systemctl disable firewalld && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && echo "关闭selinux"
sleep 10
echo "###########清理旧版本###########"
rpm -qa | grep mariadb > /root/888.txt
rpm -qa | grep mysql >> /root/888.txt
PLIST=$(cat /root/888.txt)
for PKGNAME in $PLIST
do
rpm -e --nodeps $PKGNAME && echo "老旧包已经清理完毕"
done
rm -rf /root/888.txt && echo "临时文件已删除"
sleep 8
tarbagp=/usr/src
#判断文件是否存在,如果存在就不用下载了
function is_exist()
{
if [ ! -f $tarbagp/$1 ];then
echo "开始下载$1"
return 2
fi
}
#Install httpd web server
function httpd_install()
{
if [[ "$1" -eq "1" ]];then
cd $tarbagp;
#判断文件是否存在
is_exist httpd-2.4.25.tar.gz
if [ $? -eq "2" ] ;then
echo "请上传httpd-2.4.25.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
yum -y install gcc* apr-devel apr-util-devel pcre-devel
sleep 10
tar zxvf $tarbagp/httpd-2.4.25.tar.gz -C $tarbagp && cd $tarbagp/httpd-2.4.25
./configure
--prefix=/usr/local/httpd
--enable-so
--enable-rewrite
--enable-charset-lite
--enable-cgi
if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
echo "#chkconfig:35 85 21" >> /etc/init.d/httpd && echo "http安装成功"
fi
fi
else
echo "遇到致命错误,即将退出"
exit 1
fi
fi
}
#Install Mysql server
function mysql_install(){
if [[ "$1" -eq "2" ]];then
cd $tarbagp;
#判断文件是否存在
is_exist mysql-5.5.62.tar.gz
if [ $? -eq "0" ] ;then
###########安装依赖包##############
yum -y install make gcc-c++ cmake bison-devel ncurses-devel perl openssl-devel autoconf
############添加用户及组###########
groupadd mysql && useradd mysql -g mysql -M -s /sbin/nologin
#########解压安装包##########
cd $tarbagp && tar -zxvf mysql-5.5.62.tar.gz && cd mysql-5.5.62
############开始编译##########
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/usr/local/mysql/data
-DSYSCONFDIR=/etc
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_MEMORY_STORAGE_ENGINE=1
-DWITH_READLINE=1
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock
-DMYSQL_TCP_PORT=3306
-DENABLED_LOCAL_INFILE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
##########开始安装#############
make && make install
#########账户权限配置###########
chown mysql:mysql /usr/local/mysql
###########安全初始化##########
yum -y install autoconf && echo "Dumper模块安装成功"
cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
else
echo "请上传mysql-5.5.62.tar.gz到$tarbagp"
exit 1
fi
fi
}
####mysql_conf#########
function mysql_conf(){
############重建my-default.cnf#############
cat >> /usr/local/mysql/support-files/my-default.cnf << EOF
# Example MySQL config file for medium systems.
[client]
default-character-set=utf8
#password = your_password
port = 3306
socket = /tmp/mysql.sock
##################socket=/var/lib/mysql/mysql.sock
[mysqld]
character-set-server=utf8
init_connect='SET NAMES utf8
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect='SET NAMES utf8'
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF
############配置文件#############
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf ##/usr/local/mysql/support-files/my-default.cnf###
cat >> /root/mysql.txt << EOF
datadir=/usr/local/mysql/data
default-storage-engine=InnoDB
lower_case_table_names=1
EOF
sed -i '/[mysqld]/r /root/mysql.txt' /etc/my.cnf
sed -i 's#PATH=$PATH:$HOME/bin#PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib#g' /root/.bash_profile
sleep 10
rm -rf /usr/bin/mysql*
source /root/.bash_profile
source /root/.bash_profile
#############删除临时文件##############
rm -rf mysql.txt && echo "临时文件已经删除"
##########启动数据库################
/usr/local/mysql/support-files/mysql.server start && echo "数据库已启动"
ln -s /usr/local/mysql/bin/mysql* /usr/bin && echo "完成,请使用mysql -u root 登录(无密码)"
sleep 10
}
#install phpex
function install_phpex(){
cd $tarbagp;
#判断文件是否存在
is_exist libmcrypt-2.5.8.tar.gz
if [ $? -ne "0" ] ;then
echo "请上传libmcrypt-2.5.8.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
yum –y install zlib* libxml2*
sleep 10
tar zxvf $tarbagp/libmcrypt-2.5.8.tar.gz -C $tarbagp && cd $tarbagp/libmcrypt-2.5.8 && ./configure
if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
echo "libmcrypt安装成功"
fi
else
echo "遇到致命错误,即将退出"
exit 1
fi
fi
##################
cd $tarbagp;
#判断文件是否存在
is_exist mhash-0.9.9.9.tar.gz
if [ $? -ne "0" ] ;then
echo "请上传mhash-0.9.9.9.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
tar zxvf $tarbagp/mhash-0.9.9.9.tar.gz -C $tarbagp && cd $tarbagp/mhash-0.9.9.9 && ./configure
if [ $? -eq 0 ];then
make && make install
fi
if [ $? -eq 0 ];then
ln -s /usr/local/lib/libmhash* /usr/lib/ && echo "mhash安装成功"
fi
else
echo "遇到致命错误,即将退出"
exit 1
fi
################################
cd $tarbagp;
#判断文件是否存在
is_exist mcrypt-2.6.8.tar.gz
if [ $? -ne "0" ] ;then
echo "请上传mcrypt-2.6.8.tar.gz到$tarbagp"
exit 0
fi
if [ $? -eq 0 ];then
tar zxvf $tarbagp/mcrypt-2.6.8.tar.gz -C $tarbagp && cd $tarbagp/mcrypt-2.6.8 && export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH && ./configure
if [ $? -eq 0 ];then
make && make install
if [ $? -eq 0 ];then
echo "mcrypt安装成功"
fi
fi
else
echo "遇到致命错误,即将退出"
exit 14
fi
}
#Install PHP server
function php_install()
{
yum install -y gcc libxml2 libxml2-devel
mysql_conf
sleep 30
install_phpex
sleep 30
cd $tarbagp
if [ ! -f $tarbagp/php-5.5.38.tar.gz ];then
echo "请上传php-5.5.38.tar.gz到$tarbagp"
exit 1
else
if [[ "$1" -eq "3" ]];then
tar zxf $tarbagp/php-5.5.38.tar.gz -C $tarbagp && cd $tarbagp/php-5.5.38
sleep 10
#mkdir /usr/local/php5
./configure
--prefix=/usr/local/php5
--with-mcrypt
--with-apxs2=/usr/local/httpd/bin/apxs
--with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config
--with-config-file-path=/usr/local/php5
--enable-mbstring
if [ $? -eq 0 ];then
make && make install
echo -e "n 33[32m----------------------------------------------- 33[0m"
echo -e "