#!/bin/bash
# +----------------------------------------------------------------------
# |ubuntu 18.04 LAMP 环境搭建
# +----------------------------------------------------------------------
sudo chmod 777 /root
while getopts "d:p:n:w:" opt; do
case $opt in
d)
domain=$OPTARG
;;
p)
path=$OPTARG
;;
n)
dbname=$OPTARG
;;
w)
password=$OPTARG
;;
\?)
echo "无效的选项:-$OPTARG"
exit 1
;;
esac
done
# 检查参数是否缺失
if [ -z "$domain" ] || [ -z "$path" ] || [ -z "$dbname" ] || [ -z "$password" ]; then
echo "缺少参数,请输入以下参数值: "
# 检查并要求输入缺失的参数
if [ -z "$domain" ]; then
read -p "-d 域名: " domain
fi
if [ -z "$path" ]; then
read -p "-p 路径: " path
fi
if [ -z "$dbname" ]; then
read -p "-n 数据库名: " dbname
fi
if [ -z "$password" ]; then
read -p "-w 密码: " password
fi
fi
echo "+----------------------------------------------------------------------"
echo "| ubuntu 18.04 LAMP 环境搭建"
echo "+----------------------------------------------------------------------"
echo "| 域名: $domain"
echo "| 路径: $path"
echo "| 数据库名: $dbname"
echo "| 密码: $password"
echo "+----------------------------------------------------------------------"
netstat -tln
netstat -tln | grep 80
read
sudo apt-get update
sudo apt-get install apache2 -y
sudo apt-get install php7.2 libapache2-mod-php7.2 -y
sudo apt-get install mysql-server-5.7 -y
sudo apt install redis-server -y
#sudo systemctl status redis-server
sudo systemctl enable redis-server
# php拓展自行选择按
sudo apt-get install php7.2-fpm php7.2-mysql php7.2-gd php7.2-mbstring -y
sudo apt-get install php7.2-xml php7.2-curl php7.2-redis php7.2-opcache -y
sudo apt-get install php7.2-intl php7.2-zip php7.2-dom php7.2-imagick -y
#sudo apt-get install php7.2-ssh2 php7.2-bcmath php7.2-odbc php7.2-imagick php7.2-imap -y
#sudo apt-get install php7.4 libapache2-mod-php7.4 -y#sudo apt-get install php7.2-redis -y
#sudo apt-get install php7.4-fpm php7.4-mysql php7.4-gd php7.4-mbstring -y
#sudo apt-get install php7.4-xml php7.4-curl php7.4-redis php7.7-opcache -y
#sudo apt-get install php7.4-mysql php7.4-intl php7.4-zip -y
#sudo apt-get install php7.4-ssh2 php7.4-bcmath php7.4-odbc php7.4-imagick php7.4-imap -y
sudo apt-get install zip -y
sudo a2enmod rewrite
sudo apt-get install wget -y
sudo apt-get install expect -y
read
clear
echo "拓展安装完毕"
host=$(cat <<EOF
<VirtualHost *:80>
DocumentRoot ${path}
ServerAlias ${domain}
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
)
sudo touch "/etc/apache2/sites-available/${domain}.conf"
echo "${host}" > "/etc/apache2/sites-available/${domain}.conf"
sudo ln -s "/etc/apache2/sites-available/${domain}.conf" "/etc/apache2/sites-enabled/${domain}.conf"
sudo /etc/init.d/apache2 restart
read
clear
echo "站点配置完毕"
expect_str=$(cat <<EOF
#!/usr/bin/expect
spawn sudo mysql_secure_installation
set password [ lindex $argv 0 ]
expect {
"*setup VALIDATE PASSWORD plugin*" {
send "No\r";
expect {
"*New password:" {
send "$password \r";
expect {
"Re-enter new password:" {
send "$password \r";
expect {
"*Remove anonymous users?*" {
send "Y\r";
expect {
"Disallow root login remotely?" {
send "No\r"
expect {
"Remove test database and access to it?" {
send "Y\r";
expect {
"Reload privilege tables now?" {
send "Y\r";
expect {
"All done!" {
interact;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
EOF
)
cd /root
sudo touch "mysql.sh"
echo "${expect_str}" >> "mysql.sh"
sudo chmod +x mysql.sh
sudo expect mysql.sh
sudo rm mysql.sh
sudo /etc/init.d/mysql restart
create_str=$(cat <<EOF
#!/usr/bin/expect
spawn sudo mysql -uroot -p
expect {
"*password*" {
send "${password}\r"
expect {
"*mysql>*" {
send "CREATE database ${dbname};\r"
expect {
"Query OK" {
send "CREATE USER \"${dbname}\"@\"localhost\" IDENTIFIED BY \"${password}\";\r";
expect {
"Query OK" {
send "GRANT ALL PRIVILEGES ON *.* TO \"${dbname}\"@\"localhost\";\r";
expect {
"Query OK" {
send "FLUSH PRIVILEGES;\r";
expect {
"Query OK" {
send "exit;\r";
interact;
}
}
}
}
}
}
}
}
}
}
}
}
EOF
)
sudo touch "create.sh"
echo "${create_str}" >> "create.sh"
sudo chmod +x create.sh
sudo expect create.sh "$dbname"
sudo rm create.sh
sudo /etc/init.d/mysql restart
read
clear
echo "数据库创建完毕"
sudo sed -i 's/AllowOverride\s*None/AllowOverride All/g' /etc/apache2/apache2.conf
sudo /etc/init.d/apache2 restart
sudo wget https://cn.wordpress.org/latest-zh_CN.zip -P /var/www/html
sudo unzip "/var/www/html/latest-zh_CN.zip" -d /var/www/html
sudo rm "/var/www/html/latest-zh_CN.zip"
sudo touch "$path/.htaccess"
sudo chmod -R 777 "$path/.htaccess"
sudo chmod -R 777 "$path/wp-content/uploads"
sudo rm -R /root/install.sh