单台主机一键编译部署LAMP+wordpress+discuz系统的shell脚本

 

说明:

1、shell脚本与应用程序包在同一个目录中;
2、虚拟机尽量加大CPU核数,以提高编译速度;
3、根据需要修改相应的变量,主要是安装目录、用户名、密码;
4、Mariadb的grant授权部分,需要先手动修改授权范围(@后面的内容)和密码;
5、httpd与php采用sock通讯。

 

完整的shell脚本

#!/bin/bash

#*************************************************************
#Author:             fanfubin
#QQ:                 502422514
#Date:               2019-05-15
#FileName:           httpd.sh
#Copyright (C):      2019 All rights reserved
#*************************************************************

#环境:
#centos7.6
#软件版本:
#mariadb-10.2.23-linux-x86_64.tar.gz
#apr-1.7.0.tar.gz
#apr-util-1.6.1.tar.gz
#httpd-2.4.39.tar.bz2
#php-7.3.5.tar.bz2
#wordpress-5.2.tar.gz
#Discuz_X3.3_SC_UTF8.zip

httpd_install_ver=(
    apr-1.7.0.tar.gz 
    apr-util-1.6.1.tar.gz  
    httpd-2.4.39.tar.bz2
)

tar_wordpress="wordpress-5.2.tar.gz"
tar_discuz="Discuz_X3.3_SC_UTF8.zip"
tar_php="php-7.3.5.tar.bz2"
tar_mariadb="mariadb-10.2.23-linux-x86_64.tar.gz"

let cpu_num=`lscpu|awk -F ' +' '/^CPU\(s\):/{print $2}'`-1

mysql_install_dir="/app/mysql"
mysql_data_dir="/data/mysql"
mysql_user=mysql
mysql_login_name=root
mysql_root_passwd=123456
package_name=`echo $tar_mariadb | sed -r 's/(.*).(tar.gz)/\1/'`

httpd_user=apache
httpd_prefix="/app/httpd24"

php_prefix="/app/php"

wordpress_dir="$httpd_prefix/htdocs/wordpress"
wordpress_database=wordpress
wordpress_user=wpuser
wordpress_passwd=\'fanfubin\'
#wordpress_host=\'192.168.36.%\'
wordpress_passwd_php=`echo $wordpress_passwd | tr -d "\'"`
#wordpress_host_php=`echo $wordpress_host | tr -d "\'"`
#wordpress_host_php=192.168.36.35

wordpress_host=\'localhost\'
wordpress_host_php=localhost

discuz_dir="$httpd_prefix/htdocs/discuz"
discuz_database=ultrax
discuz_user=discuz
#discuz_passwd=\'fanfubin\'
#discuz_host=\'192.168.36.%\'

read -p "Pls you choose num  1-php-local  2-php-remote": choose_num
[ $choose_num -ne 1 ] && { echo "remote pattern nonsupport!"; exit; }

#Wordpress .tar. is exist?
[ ! -e $tar_wordpress ] && { echo "$tar_wordpress is not exist"; exit; }

#Discuz .tar. is exist?
[ ! -e $tar_discuz ] && { echo "$tar_discuz is not exist"; exit; }

#Mariadb .tar. is exist?
[ ! -e $tar_mariadb ] && { echo "$tar_mariadb is not exist"; exit; }

#Php .tar. is exist?
[ ! -e $tar_php ] && { echo "$tar_php is not exist"; exit; }

#httpd  .tar. is exist?
for ver in ${httpd_install_ver[*]}
    do
        if [ ! -e $ver ];then
            echo "${ver} is not exist!"
            exit
        fi
    done

[ `rpm -q php-mysql` -eq 0 ] && { echo "Pls complete discharge php-mysql"; exit; }

####Install httpd
[ ! -d $httpd_prefix ] && mkdir -p $httpd_prefix

yum -y install gcc pcre-devel openssl-devel expat-devel autoconf libtool gcc-c++ lbzip2 expat-devel unzip 

id $httpd_user
if [ `echo $?` -ne 0 ];then
    useradd -r -s /sbin/nologin $httpd_user
fi

#tar xf
for ver1 in ${httpd_install_ver[*]}
    do
        tar xf $ver1
    done

#httpd_file name && httpd_install_dir
httpd_install_num=`echo ${#httpd_install_ver[@]}`
num=0
for name in ${httpd_install_ver[*]}
    do
        if [ $num -ne $httpd_install_num ];then
            httpd_file_name[${num}]=`echo $name | awk -F '.tar.' '{print $1}'`
            if [ `echo $name | awk -F '.tar.' '{print $1}' | grep -i httpd | wc -l` -eq 1 ];then
                httpd_install_dir=`echo $name | awk -F '.tar.' '{print $1}'`
            fi
            let num++
        fi
    done

for file_mv in ${httpd_file_name[*]}
    do
        if [ `echo $file_mv | grep -i httpd |wc -l` -ne 1 ];then
            mv $file_mv ${httpd_install_dir}/srclib/`echo $file_mv | sed -r 's/(.*)-.*/\1/'`
        fi
    done

cd $httpd_install_dir

./configure \
--prefix=$httpd_prefix \
--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

sleep 1
make -j $cpu_num  && make install
sleep 1
echo 'PATH='${httpd_prefix}'/bin:$PATH' > /etc/profile.d/httpd.sh
sleep 1
source /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh

sed -ir 's/User daemon/User '$httpd_user'/' $httpd_prefix/conf/httpd.conf
sleep 1
sed -ir 's/Group daemon/Group '$httpd_user'/' $httpd_prefix/conf/httpd.conf

echo "$httpd_prefix/bin/apachectl start" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

sed -ri 's%#LoadModule proxy_module modules/mod_proxy.so%LoadModule proxy_module modules/mod_proxy.so%' $httpd_prefix/conf/httpd.conf
sed -ri 's%#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so%LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so%' $httpd_prefix/conf/httpd.conf
sed -ri 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' $httpd_prefix/conf/httpd.conf

cat >>$httpd_prefix/conf/httpd.conf<<EOF
addType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#ProxyRequests Off
#ProxyPassMatch ^/(.*\.php)$ unix:/var/run/php.sock|fcgi://localhost${httpd_prefix}/htdocs/
#ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1${httpd_prefix}/htdocs/

<virtualhost *:80>
documentroot $httpd_prefix/htdocs/wordpress
servername  blog.test.com
ProxyRequests Off
#ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/
ProxyPassMatch ^/(.*\.php)$ unix:/var/run/php.sock|fcgi://localhost${httpd_prefix}/htdocs/wordpress
<directory $httpd_prefix/htdocs/wordpress>
require all granted
</directory>
</virtualhost>

<virtualhost *:80>
documentroot $httpd_prefix/htdocs/discuz
servername  forum.test.com
ProxyRequests Off
#ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/discuz/$1
ProxyPassMatch ^/(.*\.php)$ unix:/var/run/php.sock|fcgi://localhost${httpd_prefix}/htdocs/discuz
<directory ${httpd_prefix}/htdocs/discuz>
require all granted
</directory>
</virtualhost> 
EOF
cd -

###Install php-fpm
yum -y install libxml2-devel bzip2-devel libmcrypt-devel phpize lbzip2

tar xf $tar_php 
sleep 1
php_install_dir=`echo $tar_php | awk -F '.tar.' '{print $1}'`
sleep 1
chown -R  root.root ${php_install_dir}
cd ${php_install_dir}
./configure \
--prefix=$php_prefix \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-mbstring \
--enable-xml \
--enable-sockets \
--enable-fpm \
--enable-maintainer-zts \
--disable-fileinfo

sleep 2
make -j $cpu_num && make install
cp php.ini-production  /etc/php.ini
sed -ri 's#;date.timezone =#date.timezone = "Asia/Shanghai"#' /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp $php_prefix/etc/php-fpm.conf.default $php_prefix/etc/php-fpm.conf
cp $php_prefix/etc/php-fpm.d/www.conf.default $php_prefix/etc/php-fpm.d/www.conf
sleep 1
sed -ir 's/user = nobody/user = '$httpd_user'/' $php_prefix/etc/php-fpm.d/www.conf
sed -ir 's/group = nobody/group = '$httpd_user'/' $php_prefix/etc/php-fpm.d/www.conf

if [ $choose_num -eq 1 ];then
    sed -ri 's#listen = 127.0.0.1:9000#listen = /var/run/php.sock#' $php_prefix/etc/php-fpm.d/www.conf
    sed -ri 's#;listen.mode = 0660#listen.mode = 0666#' $php_prefix/etc/php-fpm.d/www.conf
fi

cd -
sleep 2

####Binary install mariadb
id $mysql_user &>/dev/null
if [ `echo $?` -ne 0 ];then
    userdel -r $mysql_user &>/dev/null
    useradd -r -u 336 -s /sbin/nologin -d $mysql_data_dir $mysql_user &>/dev/null
else
    useradd -r -u 336 -s /sbin/nologin -d $mysql_data_dir $mysql_user &>/dev/null
fi 

rpm -q libaio &>/dev/null
[ `echo $?` -ne 0 ] && yum -y install libaio

rpm -q expect &>/dev/null
[ `echo $?` -ne 0 ] && yum -y install expect

#此文件可能会造成影响,所以先清空 
\rm -rf /etc/my.cnf

tar xf $tar_mariadb -C /usr/local/
sleep 2
cd  /usr/local/
ln -s $package_name mysql
chown -R root.root /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh 
sleep 1
source /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
mkdir $mysql_data_dir -p
chown ${mysql_user}.${mysql_user} $mysql_data_dir
cd -

cd /usr/local/mysql
./scripts/mysql_install_db --datadir=$mysql_data_dir --user=$mysql_user
mkdir -p /etc/mysql
cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
#禁止主机名解析,建议使用
sed -ri '/^\[mysqld\]/askip_name_resolve = on' /etc/mysql/my.cnf
sed -ri '/^\[mysqld\]/adatadir=\/data\/mysql' /etc/mysql/my.cnf
cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
sleep 2

expect <<EOF
spawn mysql_secure_installation
expect {
"Enter current password for root" { send "\n";exp_continue }
"Set root password" { send "\n";exp_continue }
"New password" { send "${mysql_root_passwd}\n";exp_continue }
"Re-enter new password" { send "${mysql_root_passwd}\n";exp_continue }
"Remove anonymous users" { send "y\n";exp_continue }
"Disallow root login remotely" { send "y\n";exp_continue }
"Remove test database and access to it" { send "y\n";exp_continue }
"Reload privilege tables now" { send "y\n" }
}
#expect "]#" { send "exit\n" }
expect eof
EOF

mysqladmin -uroot -p${mysql_root_passwd} ping &>/dev/null
[ `echo $?` -eq 0 ] && echo 'mysql is running !' || echo 'mysql is stopped' 

#create wordpress && discuz
mysql -uroot -p${mysql_root_passwd} -e "create database ${wordpress_database}"
#mysql -uroot -p${mysql_root_passwd} -e "grant all on ${wordpress_database}.* to ${wordpress_user}@'192.168.36.%' identified by 'fanfubin'"
mysql -uroot -p${mysql_root_passwd} -e "grant all on ${wordpress_database}.* to ${wordpress_user}@'localhost' identified by 'fanfubin'"
mysql -uroot -p${mysql_root_passwd} -e "grant all on ${discuz_database}.* to ${discuz_user}@'192.168.36.%' identified by 'fanfubin'"
cd -
sleep 2

###Install wordpress
tar xf $tar_wordpress -C $httpd_prefix/htdocs/
sleep 1
cp $wordpress_dir/wp-config-sample.php $wordpress_dir/wp-config.php
sed -ri 's/database_name_here/'$wordpress_database'/' $wordpress_dir/wp-config.php
sed -ri 's/username_here/'$wordpress_user'/' $wordpress_dir/wp-config.php
sed -ri 's/password_here/'$wordpress_passwd_php'/' $wordpress_dir/wp-config.php
sed -ri 's/localhost/'$wordpress_host_php'/' $wordpress_dir/wp-config.php

setfacl -R -m u:${httpd_user}:rwx $wordpress_dir

echo $wordpress_passwd_php
echo $wordpress_host_php

sleep 2

### Install discuz
unzip $tar_discuz  &>/dev/null
sleep 1
mv upload $httpd_prefix/htdocs/
mv $httpd_prefix/htdocs/upload $httpd_prefix/htdocs/discuz
setfacl -R -m  u:${httpd_user}:rwx $httpd_prefix/htdocs/discuz/

apachectl restart
sleep 2
service php-fpm start