hjaltiatlason

AWS-User-data-Wordpress

Oct 25th, 2020 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.41 KB | None | 0 0
  1. #!/bin/bash -xe
  2. # Using these resources via AWS EC2 + EFS + RDS + LT + Auto Scaling group + Parameter store
  3.  
  4. ALBDNSNAME=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/ALBDNSNAME --query Parameters[0].Value)
  5. ALBDNSNAME=`echo $ALBDNSNAME | sed -e 's/^"//' -e 's/"$//'`
  6.  
  7. EFSFSID=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/EFSFSID --query Parameters[0].Value)
  8. EFSFSID=`echo $EFSFSID | sed -e 's/^"//' -e 's/"$//'`
  9.  
  10. DBPassword=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBPassword --with-decryption --query Parameters[0].Value)
  11. DBPassword=`echo $DBPassword | sed -e 's/^"//' -e 's/"$//'`
  12.  
  13. DBRootPassword=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBRootPassword --with-decryption --query Parameters[0].Value)
  14. DBRootPassword=`echo $DBRootPassword | sed -e 's/^"//' -e 's/"$//'`
  15.  
  16. DBUser=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBUser --query Parameters[0].Value)
  17. DBUser=`echo $DBUser | sed -e 's/^"//' -e 's/"$//'`
  18.  
  19. DBName=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBName --query Parameters[0].Value)
  20. DBName=`echo $DBName | sed -e 's/^"//' -e 's/"$//'`
  21.  
  22. DBEndpoint=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBEndpoint --query Parameters[0].Value)
  23. DBEndpoint=`echo $DBEndpoint | sed -e 's/^"//' -e 's/"$//'`
  24.  
  25. yum -y update
  26. yum -y upgrade
  27.  
  28. yum install -y httpd wget
  29. amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
  30. amazon-linux-extras install epel -y
  31.  
  32.  
  33. systemctl enable httpd
  34. systemctl start httpd
  35.  
  36.  
  37. mysqladmin -u root password $DBRootPassword
  38.  
  39. wget http://wordpress.org/latest.tar.gz -P /var/www/html
  40. cd /var/www/html
  41. tar -zxvf latest.tar.gz
  42. cp -rvf wordpress/* .
  43. rm -R wordpress
  44. rm latest.tar.gz
  45.  
  46. sudo cp ./wp-config-sample.php ./wp-config.php
  47. sed -i "s/'database_name_here'/'$DBName'/g" wp-config.php
  48. sed -i "s/'username_here'/'$DBUser'/g" wp-config.php
  49. sed -i "s/'password_here'/'$DBPassword'/g" wp-config.php
  50. sed -i "s/'localhost'/'$DBEndpoint'/g" wp-config.php
  51.  
  52. usermod -a -G apache ec2-user  
  53. chown -R ec2-user:apache /var/www
  54. chmod 2775 /var/www
  55. find /var/www -type d -exec chmod 2775 {} \;
  56. find /var/www -type f -exec chmod 0664 {} \;
  57.  
  58. cat >> /home/ec2-user/update_wp_ip.sh<< 'EOF'
  59. #!/bin/bash
  60. source <(php -r 'require("/var/www/html/wp-config.php"); echo("DB_NAME=".DB_NAME."; DB_USER=".DB_USER."; DB_PASSWORD=".DB_PASSWORD."; DB_HOST=".DB_HOST); ')
  61. SQL_COMMAND="mysql -u $DB_USER -h $DB_HOST -p$DB_PASSWORD $DB_NAME -e"
  62. OLD_URL=$(mysql -u $DB_USER -h $DB_HOST -p$DB_PASSWORD $DB_NAME -e 'select option_value from wp_options where option_id = 1;' | grep http)
  63.  
  64. ALBDNSNAME=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/ALBDNSNAME --query Parameters[0].Value)
  65. ALBDNSNAME=`echo $ALBDNSNAME | sed -e 's/^"//' -e 's/"$//'`
  66.  
  67. $SQL_COMMAND "UPDATE wp_options SET option_value = replace(option_value, '$OLD_URL', 'http://$ALBDNSNAME') WHERE option_name = 'home' OR option_name = 'siteurl';"
  68. $SQL_COMMAND "UPDATE wp_posts SET guid = replace(guid, '$OLD_URL','http://$ALBDNSNAME');"
  69. $SQL_COMMAND "UPDATE wp_posts SET post_content = replace(post_content, '$OLD_URL', 'http://$ALBDNSNAME');"
  70. $SQL_COMMAND "UPDATE wp_postmeta SET meta_value = replace(meta_value,'$OLD_URL','http://$ALBDNSNAME');"
  71. EOF
  72.  
  73. chmod 755 /home/ec2-user/update_wp_ip.sh
  74. echo "/home/ec2-user/update_wp_ip.sh" >> /etc/rc.local
  75. /home/ec2-user/update_wp_ip.sh
  76.  
Add Comment
Please, Sign In to add comment