测试环境请先关闭防火墙及selinux
#!/bin/bash
# auto install lamp CentOS7
# 2019年8月6日16:36:45
# by jeff
############################
#Httpd define path variable
H_URL="https://mirrors.cnnic.cn/apache/httpd/${H_FILE}"
H_FILE="httpd-2.4.38.tar.gz"
H_DIR="httpd-2.4.38"
H_PREFIX="/usr/local/apache2/"
#MySQL define path variable
#https://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.62.tar.gz
M_FILE="mysql-5.5.62.tar.gz"
M_DIR="mysql-5.5.62"
M_URL="https://dev.mysql.com/get/Downloads/MySQL-5.5/${M_FILE}"
M_PREFIX="/usr/local/mysql55/"
#PHP define path variable
#https://www.php.net/distributions/php-5.5.38.tar.gz
P_FILE="php-5.5.38.tar.gz"
P_DIR="php-5.5.38"
P_URL="https://www.php.net/distributions/${P_FILE}"
P_PREFIX="/usr/local/php5/"
function install_httpd() {
yum install -y apr-devel apr-util-devel pcre* wget
wget -c ${H_URL}/${H_FILE}
tar -xf ${H_FILE}
cd ${H_DIR}
./configure --prefix=$H_PREFIX
if [ $? -eq 0 ];then
make && make install
fi
cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
}
function add_httpd_system_service() {
# 启动脚本
cat>/etc/init.d/httpd <<EOF
#!/bin/bash
# chkconfig: 12345 80 90
function start_http()
{
/usr/local/apache2/bin/apachectl start
}
function stop_http()
{
/usr/local/apache2/bin/apachectl stop
}
case "\$1" in
start)
start_http
;;
stop)
stop_http
;;
restart)
stop_http
start_http
;;
*)
echo "Usage : start | stop | restart"
;;
esac
EOF
# 赋权
cd /etc/init.d
chmod a+x httpd
# 启动服务
systemctl daemon-reload
systemctl start httpd
# 设置开机自启动
chkconfig --add httpd
}
function install_mysql() {
yum install -y cmake ncurses-devel wget
wget -c $M_URL && tar xf $M_FILE && cd $M_DIR
cmake . -DCMAKE_INSTALL_PREFIX=$M_PREFIX \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0
if [ $? -eq 0 ];then
make && make install
echo -e "\n\033[32m--------------------------------------\033[0m"
echo -e "\033[32mThe $M_DIR Server Install Success !\033[0m"
else
echo -e "\033[32mThe $M_DIR Make or Make install ERROR,Please Check. "
exit 0
fi
}
function add_mysqld_system_service() {
# 设置为系统服务
cd $M_PREFIX
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
cd /etc/init.d
chmod a+x mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on
mmkdir -p /data/mysql
useradd mysql
${M_PREFIX}/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=${M_PREFIX}
ln -s ${M_PREFIX}/bin/* /usr/bin
# 启动服务
systemctl daemon-reload
systemctl start mysqld
ps -ef | grep -i mysql | grep -v grep
if [ $? -eq 0 ];then
echo -e "\033[32m----mysql system service install success-------\033[0m"
else
echo -e "\033[32m----mysql system service install fail-------\033[0m"
fi
}
function install_php() {
yum install -y libxml2-devel perl-devel perl libtool*
if [ ! -f $P_FILE ];then
wget -c $P_URL
fi
tar -xf $P_FILE && cd $P_DIR
./configure --prefix=$P_PREFIX --with-config-file-path=${P_PREFIX}etc --with-mysql=$M_PREFIX --with-apxs2=${H_PREFIX}bin/apxs
if [ $? -ne 0 ];then
echo -e "\033[32mThe $P_FILES_DIR ./configure ERROR,Please Check."
exit 0
fi
make && make install
if [ $? -eq 0 ];then
echo -e "\033[32mThe $P_FILES_DIR Server Install Success !\033[0m"
echo -e "\n\033[32m------------------------------\033[0m"
else
echo -e "\033[32mThe $P_FILES_DIR Make or Make install ERROR,Please Check."
exit 0
fi
}
function lamp_config() {
sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf
echo "AddType application/x-httpd-php .php" >>$H_PREFIX/conf/httpd.conf
cat>$H_PREFIX/htdocs/index.php<<EOF
<?
phpinfo();
?>
EOF
$H_PREFIX/bin/apachectl restart
}
# 入口
PS3="please enter you select what kind of service you want to install:"
select i in http mysql php config install_lamp quit
do
case $i in
http)
install_httpd
add_httpd_system_service
;;
mysql)
install_mysql
add_mysqld_system_service
;;
php)
install_php
;;
config)
lamp_config
;;
install_lamp)
install_httpd
add_httpd_system_service
install_mysql
add_mysqld_system_service
install_php
lamp_config
systemctl restart httpd
;;
quit)
exit
;;
esac
done