🐥

EC2 wordpress構築 シェルスクリプト

2021/12/18に公開
ファイル名
#bin/bash
set -e

question() {
  echo $1
  read anser
  while [ "$anser" = "" ]; do
    echo $3
    read anser
  done
  export $2=$anser
}

question "データベース名を入力してください" "database" "データベースを設定してください"
question "データベースユーザー名を入力してください" "user" "データベースユーザー名を設定してください"
question "データベースパスワードを入力してください" "password" "データベースパスワードを設定してください"
host_name=localhost

cat <<EOS
データベース名: $database
データベースユーザー名: $user
データベースパスワード: $password
データベースホスト名: $host_name  
上記の内容で正しいですか?(Y or N)
EOS

comand() {
  if ! command -v $1 &>/dev/null; then
    $2
  else
    echo "インストール済なため終了"
  fi
}

read ANS
case $ANS in
[yY])
  comand "httpd" "sudo yum install -y httpd"
  comand "php -r 'phpinfo();' | grep mysqlnd" "yum install -y mysqlnd"
  comand "php -v" "sudo yum install -y php"
  comand "wget --version" "sudo yum install -y wget"
  installscomannds="
    sudo yum -y remove mariadb-libs
    sudo yum -y localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    sudo yum -y install --enablerepo=mysql80-community mysql-community-server
    sudo yum -y install --enablerepo=mysql80-community mysql-community-devel
    "
  comand "mysqld" "$installscomannds"

  Apache() {
    sudo systemctl start httpd
    sudo systemctl enable httpd
    sudo sed -i -e 's/ServerTokens OS/ServerTokens Prod/g' /etc/httpd/conf/httpd.conf
  }
  php() {
    sudo cp /etc/php.ini /etc/php.ini.org
    sudo sed -i -e "s/date\.timezone =/date\.timezone = Asia\/Tokyo/g" /etc/php.ini
  }

  Wordpress() {
    wget http://ja.wordpress.org/wordpress-4.3-ja.tar.gz
    tar zxvf wordpress-4.3-ja.tar.gz
    sudo cp -r wordpress /var/www/
    sudo chown -R apache.apache /var/www/wordpress
    sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
    sudo sed -i -e "s/\/var\/www\/html/\/var\/www\/wordpress/g" /etc/httpd/conf/httpd.conf
    sudo sed -i -e "s/Directory \//Directory \/var\/www\/wordpress/g" /etc/httpd/conf/httpd.conf
    sudo sed -i -e "s/AllowOverride None/AllowOverride All/g" /etc/httpd/conf/httpd.conf
  }

  mysql_word() {
    sudo systemctl start mysqld
    sudo systemctl status mysqld
    firstpass=$(sudo less /var/log/mysqld.log | grep MY-010454 | awk '{print $13}')
    mysql -u root -p"$firstpass" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$password';"
    mysql -u root -p"$password" -e "create database $database"
    mysql -u root -p"$password" -e "CREATE USER $user@localhost IDENTIFIED WITH mysql_native_password BY '$password';"
    mysql -u root -p"$password" -e "GRANT ALL ON db_name.* TO $user@localhost;"
    sudo cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php
    sudo sed -i -e "s/define('DB_NAME', 'database_name_here')/define('DB_NAME', '$database')/g" /var/www/wordpress/wp-config.php
    sudo sed -i -e "s/define('DB_USER', 'username_here')/define('DB_USER', '$user')/g" /var/www/wordpress/wp-config.php
    sudo sed -i -e "s/define('DB_PASSWORD', 'password_here')/define('DB_PASSWORD', '$password')/g" /var/www/wordpress/wp-config.php
    sudo sed -i -e "s/define('DB_HOST', 'localhost')/define('DB_HOST', '$host_name')/g" /var/www/wordpress/wp-config.php
    sudo sed -i -e "s/define('DB_CHARSET', 'utf8')/define('DB_CHARSET', 'utf8')/g" /var/www/wordpress/wp-config.php
  }

  Apache
  php
  sudo systemctl restart httpd
  Wordpress
  mysql_word
  sudo systemctl restart httpd.service

  ;;
[nN])
  echo "最初からやり直してください"
  ;;
*)
  echo "Y or N で入力してください"
  ;;
esac

Discussion