一键部署LAMP博客
程序员文章站
2022-06-27 20:08:15
#!/bin/bash##----------------------------------------------#Author:yangyang#E-mail:1771566679@qq.com#Date:2020-07-24#FileName:LAMP.sh#URL:https://me.csdn.net/yy150122#Description: 自动部署LAMP#Copyright (C):2020 All rights reserved#------------------...
#!/bin/bash
#
#----------------------------------------------
#Author:yangyang
#E-mail:1771566679@qq.com
#Date:2020-07-24
#FileName:LAMP.sh
#URL:https://me.csdn.net/yy150122
#Description: 自动部署LAMP
#Copyright (C):2020 All rights reserved
#---------------------------------------------
#
#准备阶段
echo -e '\e[1;36m---------------------------------安装初始化...----------------------------------\e[0m'
. /etc/init.d/functions
http_path=/BLOG/httpd/conf/httpd.conf
wordpress_path=/BLOG/httpd/htdocs/blog/wp-config.php
echo 'PATH=/BLOG/httpd/bin:/usr/local/mysql/bin:$PATH' > /etc/profile.d/lamp.sh
#创建系统用户
useradd -r -s /sbin/nologin mysql &> /dev/null
useradd -r -s /sbin/nologin apache &> /dev/null
#校验和
mariadb='583669d3b51e8a0e98cf0c2448aab488703d9e0c3de8270c9eea02f9b358c28f'
httpd2='a497652ab3fc81318cdc2a203090a999150d86461acff97c1065dc910fe10f43'
apr1='e2e148f0b2e99b8e5c6caa09f6d4fb4dd3e83f744aa72a952f94f5a14436f7ea'
aprutil1='d3e12f7b6ad12687572a3a39475545a072608f4ba03a6ce8a3778f607dd0035b'
php7='c6ed7894911acfe075381c01b07745d92e9259fac510a849f742edb6b95c89de'
latestzh_CN='944864ca132d4287bf80eca1d7deec99220f0e84989758551e92ff6251ecd282'
#SELinux
setenforce 0
#防火墙
iptables -L &> /dev/null
systemctl stop firewalld &> /dev/null
action "Ready..."
echo -e '\e[1;36m---------------------------------初始化完成...----------------------------------\e[0m'
echo
echo
echo -e '\e[1;36m---------------------------------环境预处理...----------------------------------\e[0m'
#删除原有的软件
rpm -qa mariadb &> /dev/null
if [ $? -eq 0 ];then
yum remove mariadb -y &> /dev/null
fi
rpm -qa httpd &> /dev/null
if [ $? -eq 0 ];then
yum remove httpd -y &> /dev/null
fi
rpm -qa apr &> /dev/null
if [ $? -eq 0 ];then
yum remove apr -y &> /dev/null
fi
echo -e '\e[1;36m---------------------------------环境预处理...----------------------------------\e[0m'
#创建源码包目录
mkdir LAMP
cd LAMP
echo -e '\e[1;36m---------------------------------预处理完成...----------------------------------\e[0m'
echo -e '\e[1;36m---------------------------------工具包下载...----------------------------------\e[0m'
#下载安装工具包
yum groupinstall "development tools" -y &> /dev/null
which wget &> /dev/null
if [ $? -ne 0 ];then
yum install wget -y &> /dev/null
fi
which bzip2 &> /dev/null
if [ $? -ne 0 ];then
yum install bzip2 -y &> /dev/null
fi
yum install -y pcre-devel openssl-devel expat-devel libxml2-devel &> /dev/null
yum install -y vim &> /dev/null
yum install bzip2-devel libxml2-devel libmcrypt-devel sqlite-devel.x86_64 re2c php-pgsql -y &> /dev/null
yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.5+rev1-2.el7.remi.x86_64.rpm -y &> /dev/null
yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.5+rev1-2.el7.remi.x86_64.rpm -y &> /dev/null
echo -e '\e[1;36m-------------------------------工具包下载完成...--------------------------------\e[0m'
#判断下载是否成功
func_install_judge(){
Get=0
while [ $Get -eq 0 ];do
wget $1 &> /dev/null
if [ $? -eq 0 ];then
File=`ls -lt | sed -n 2p | awk '{print $NF}'`
if [[ $File == 'index.html' ]];then
mv index.html mariadb.tar.gz
File=mariadb.tar.gz
fi
FILES=`echo $File | awk -F'.' '{print $1}' | tr -d '-'`
value=`sha256sum $File | awk '{print $1}'`
eval A="$"$FILES
if [[ $value == $A ]];then
Get=1
action 'Install success!'
else
action 'File is missing!' /bin/false
action 'Reinstalling...'
func_install_judge
fi
else
action 'Install failed...' /bin/false
action 'Reinstalling...'
fi
done
}
#下载所需软件源码包
func_download_mariadb(){
func_install_judge https://downloads.mariadb.org/interstitial/mariadb-10.5.4/bintar-linux-x86_64/mariadb-10.5.4-linux-x86_64.tar.gz/from/http%3A//mirrors.tuna.tsinghua.edu.cn/mariadb/
}
func_download_php(){
func_install_judge https://www.php.net/distributions/php-7.3.20.tar.bz2
}
func_download_wordpress(){
func_install_judge https://cn.wordpress.org/latest-zh_CN.tar.gz
}
func_download_apache(){
func_install_judge https://downloads.apache.org//httpd/httpd-2.4.43.tar.bz2
}
func_download_apr(){
func_install_judge https://downloads.apache.org//apr/apr-1.7.0.tar.bz2
}
func_download_apr-util(){
func_install_judge https://downloads.apache.org//apr/apr-util-1.6.1.tar.bz2
}
#解压缩所有软件包
func_unzip(){
for i in `ls` ;do
if [[ $i == *tar* ]];then
tar xf $i &> /dev/null
if [ $? -eq 0 ];then
action 'Unzip success!'
else
action 'Unzip failed!' /bin/false
echo "请移除LAMP文件夹,重新运行脚本"
exit
fi
fi
done
# ls | xargs -n1 tar xf
}
#安装Apache,apr,apr-util
func_install_apache(){
#准备编译
mv apr-1.7.0 httpd-2.4.43/srclib/apr
mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
mkdir /BLOG
echo -e '\e[1;36m-------------------------------Apache编译开始...-------------------------------\e[0m'
#开始编译 httpd
cd httpd-2.4.43/
./configure --prefix=/BLOG/httpd \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork &> /dev/null
if [ $? -eq 0 ];then
action "Apache compile success..."
echo -e '\e[1;36m-------------------------------Apache编译完成...-------------------------------\e[0m'
else
action "Apache compile failed..." /bin/false
exit
fi
echo -e '\e[1;36m-------------------------------Apache安装开始...-------------------------------\e[0m'
make &> /dev/null && make install &> /dev/null
if [ $? -eq 0 ];then
action "Apache install success..."
else
action "Apache install failed..." /bin/false
exit
fi
#处理后续事务
sed -i 's/User daemon/User apache/' $http_path &> /dev/null
sed -i 's/Group daemon/Group apache/' $http_path &> /dev/null
sed -i 's/#LoadModule proxy_module modules\/mod_proxy.so/LoadModule proxy_module modules\/mod_proxy.so/' $http_path &> /dev/null
sed -i 's/#LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/' $http_path &> /dev/null
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' $http_path &> /dev/null
cat << EOF >> $http_path
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/BLOG/httpd/htdocs/\$1
EOF
#制作启动脚本
cat << EOF >> /etc/init.d/httpd
#!/bin/bash
#chkconfig:345 85 15
#description:Start and stop the Apache HTTP Server
function httpd_start(){
/BLOG/httpd/bin/apachectl start
}
function httpd_stop(){
/BLOG/httpd/bin/apachectl stop
}
case \$1 in
'start')
httpd_start
;;
'stop')
httpd_stop
;;
'restart')
httpd_stop
httpd_start
;;
*)
echo "Usage: httpd start|stop|restart!"
;;
esac
EOF
chmod u+x /etc/init.d/httpd
chkconfig --add httpd
systemctl daemon-reload
#检查Apache服务是否正常工作
systemctl start httpd &> /dev/null
ss -ntl | grep ":80" &> /dev/null
if [ $? -eq 0 ];then
action "httpd is running..."
echo -e '\e[1;36m-------------------------------Apache安装完成...-------------------------------\e[0m'
else
action "httpd is run failed..." /bin/false
fi
}
func_install_mariadb(){
echo -e '\e[1;36m-------------------------------MariaDB安装开始...-------------------------------\e[0m'
mv /root/LAMP/mariadb-10.5.4-linux-x86_64 /usr/local/
ln -s /usr/local/mariadb-10.5.4-linux-x86_64/ /usr/local/mysql &> /dev/null
mkdir /data/mysqldb/ &> /dev/null
mkdir /var/log/mysql/ &> /dev/null
mkdir /var/run/mysql/ &> /dev/null
chown -R mysql.mysql /data/mysqldb/ &> /dev/null
cd /usr/local/mysql
./scripts/mysql_install_db --datadir=/data/mysqld --user=mysql &> /dev/null
cat << EOF > /etc/my.cnf
[mysqld]
port=3306
datadir=/data/mysqld
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysql/mysqldb.log
pid-file=/var/run/mysql/mysqldb.pid
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8
EOF
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysqld &> /dev/null
chkconfig --add mysqld
service mysqld start &> /dev/null
ss -ntl | grep ":3306" &> /dev/null
if [ $? -eq 0 ];then
action "mysqld is running..."
echo -e '\e[1;36m-------------------------------MariaDB安装完成...-------------------------------\e[0m'
else
action "mysqld is run failed..." /bin/false
fi
}
func_install_php(){
echo -e '\e[1;36m-------------------------------PHP开始编译...-------------------------------\e[0m'
cd /root/LAMP/php-7.3.20
./configure --prefix=/BLOG/php \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-openssl \
--with-pdo-mysql=mysqlnd \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-sockets \
--enable-fpm \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-maintainer-zts \
--disable-fileinfo &> /dev/null
echo -e '\e[1;36m-------------------------------PHP编译完成...-------------------------------\e[0m'
make &> /dev/null && make install &> /dev/null
if [ $? -eq 0 ];then
action "PHP install success..."
echo -e '\e[1;36m-------------------------------PHP开始安装...-------------------------------\e[0m'
else
action "PHP install failed..." /bin/false
exit
fi
#配置PHP
cp -u /root/LAMP/php-7.3.20/php.ini-production /etc/php.ini
cp -u /root/LAMP/php-7.3.20/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm &> /dev/null
chkconfig php-fpm on &> /dev/null
cp -u /BLOG/php/etc/php-fpm.conf.default /BLOG/php/etc/php-fpm.conf
cp -u /BLOG/php/etc/php-fpm.d/www.conf.default /BLOG/php/etc/php-fpm.d/www.conf
service php-fpm start &> /dev/null
ss -ntl | grep ":9000" &> /dev/null
if [ $? -eq 0 ];then
action "php-fpm starting success..."
echo -e '\e[1;36m-------------------------------PHP安装完成...-------------------------------\e[0m'
else
action "php-fpm starting failed..." /bin/false
exit
fi
}
func_install_wordpress(){
echo -e '\e[1;36m-------------------------------Wordpress开始安装...-------------------------------\e[0m'
mv /root/LAMP/wordpress /BLOG/httpd/htdocs/
cd /BLOG/httpd/htdocs/
rm -rf index.html
mv wordpress/ blog
cd blog/
cp wp-config-sample.php wp-config.php
sed -i 's/database_name_here/blogdb/' $wordpress_path
sed -i 's/username_here/bloguser/' $wordpress_path
sed -i 's/password_here/1/' $wordpress_path
service mysqld start &> /dev/null
. /etc/profile.d/lamp.sh
mysql -e 'create database blogdb;'
mysql -e "grant all on blogdb.* to bloguser@localhost identified by '1';"
mysql -e 'flush privileges;'
echo -e '\e[1;36m-------------------------------Wordpress安装完成...-------------------------------\e[0m'
}
#开始部署LAMP
echo -e '\e[1;36m---------------------------------软件包下载...----------------------------------\e[0m'
func_download_mariadb
func_download_php
func_download_wordpress
func_download_apache
func_download_apr
func_download_apr-util
echo -e '\e[1;36m-------------------------------软件包下载完成...--------------------------------\e[0m'
echo -e '\e[1;36m---------------------------------软件包解压...----------------------------------\e[0m'
func_unzip
echo -e '\e[1;36m---------------------------------软件包解压完成...----------------------------------\e[0m'
func_install_apache
func_install_mariadb
func_install_php
func_install_wordpress
echo -e '\e[1;36m---------------------------------开始部署LAMP...----------------------------------\e[0m'
systemctl restart httpd &> /dev/null
service mysqld restart &> /dev/null
echo -e '\e[1;36m---------------------------------LAMP部署完成...----------------------------------\e[0m'
echo "请在浏览器访问http://localhost/blog,来进行个人博客设置,httpd使用service httpd start来启动,mysqld使用service mysqld start来启动,博客后台数据库用户名为bloguser,密码为1。"
本文地址:https://blog.csdn.net/yy150122/article/details/107574072
推荐阅读