Advertisement
willysec_id

Fresh VPS Wordpress Setup

Apr 18th, 2025
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.26 KB | Source Code | 0 0
  1. #!/bin/bash
  2. #
  3. # WordPress activation script
  4. #
  5. # This script will configure Apache with the domain
  6. # provided by the user and offer the option to set up
  7. # LetsEncrypt as well.
  8.  
  9. # Enable WordPress on first login
  10. if [[ -d /var/www/wordpress ]]
  11. then
  12.   mv /var/www/html /var/www/html.old
  13.   mv /var/www/wordpress /var/www/html
  14. fi
  15. chown -Rf www-data:www-data /var/www/html
  16.  
  17. # if applicable, configure wordpress to use mysql dbaas
  18. if [ -f "/root/.digitalocean_dbaas_credentials" ] && [ "$(sed -n "s/^db_protocol=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)" = "mysql" ]; then
  19.  # grab all the data from the password file
  20.  username=$(sed -n "s/^db_username=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)
  21.   password=$(sed -n "s/^db_password=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)
  22.   host=$(sed -n "s/^db_host=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)
  23.   port=$(sed -n "s/^db_port=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)
  24.   database=$(sed -n "s/^db_database=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)
  25.  
  26.   # update the wp-config.php with stored credentials
  27.   sed -i "s/'DB_USER', '.*'/'DB_USER', '$username'/g" /var/www/html/wp-config.php;
  28.   sed -i "s/'DB_NAME', '.*'/'DB_NAME', '$database'/g" /var/www/html/wp-config.php;
  29.   sed -i "s/'DB_PASSWORD', '.*'/'DB_PASSWORD', '$password'/g" /var/www/html/wp-config.php;
  30.   sed -i "s/'DB_HOST', '.*'/'DB_HOST', '$host:$port'/g" /var/www/html/wp-config.php;
  31.  
  32.   # add required SSL flag
  33.   cat >> /var/www/html/wp-config.php <<EOM
  34. /** Connect to MySQL cluster over SSL **/
  35. define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL );
  36. EOM
  37.  
  38.   # wait for db to become available
  39.   echo -e "\nWaiting for your database to become available (this may take a few minutes)"
  40.   while ! mysqladmin ping -h "$host" -P "$port" --silent; do
  41.       printf .
  42.       sleep 2
  43.   done
  44.   echo -e "\nDatabase available!\n"
  45.  
  46.   # cleanup
  47.   unset username password host port database
  48.   rm -f /root/.digitalocean_dbaas_credentials
  49.  
  50.   # disable the local MySQL instance
  51.   systemctl stop mysql.service
  52.   systemctl disable mysql.service
  53. fi
  54.  
  55. echo "This script will copy the WordPress installation into"
  56. echo "Your web root and move the existing one to /var/www/html.old"
  57. echo "--------------------------------------------------"
  58. echo "This setup requires a domain name.  If you do not have one yet, you may"
  59. echo "cancel this setup, press Ctrl+C.  This script will run again on your next login"
  60. echo "--------------------------------------------------"
  61. echo "Enter the domain name for your new WordPress site."
  62. echo "(ex. example.org or test.example.org) do not include www or http/s"
  63. echo "--------------------------------------------------"
  64.  
  65. a=0
  66. while [ $a -eq 0 ]
  67. do
  68.  read -p "Domain/Subdomain name: " dom
  69.  if [ -z "$dom" ]
  70.  then
  71.   a=0
  72.   echo "Please provide a valid domain or subdomain name to continue or press Ctrl+C to cancel"
  73.  else
  74.   a=1
  75. fi
  76. done
  77. sed -i "s/\$domain/$dom/g"  /etc/apache2/sites-enabled/000-default.conf
  78. a2enconf block-xmlrpc
  79.  
  80. service apache2 restart
  81.  
  82. echo -en "Now we will create your new admin user account for WordPress."
  83.  
  84. function wordpress_admin_account(){
  85.  
  86.   while [ -z $email ]
  87.   do
  88.     echo -en "\n"
  89.     read -p "Your Email Address: " email
  90.   done
  91.  
  92.   while [ -z $username ]
  93.   do
  94.     echo -en "\n"
  95.     read -p  "Username: " username
  96.   done
  97.  
  98.   while [ -z $pass ]
  99.   do
  100.     echo -en "\n"
  101.     read -s -p "Password: " pass
  102.     echo -en "\n"
  103.   done
  104.  
  105.   while [ -z "$title" ]
  106.   do
  107.     echo -en "\n"
  108.     read -p "Blog Title: " title
  109.   done
  110. }
  111.  
  112. wordpress_admin_account
  113.  
  114. while true
  115. do
  116.     echo -en "\n"
  117.     read -p "Is the information correct? [Y/n] " confirmation
  118.     confirmation=${confirmation,,}
  119.     if [[ "${confirmation}" =~ ^(yes|y)$ ]] || [ -z $confirmation ]
  120.     then
  121.       break
  122.     else
  123.       unset email username pass title confirmation
  124.       wordpress_admin_account
  125.     fi
  126. done
  127.  
  128. echo -en "\n\n\n"
  129. echo "Next, you have the option of configuring LetsEncrypt to secure your new site.  Before doing this, be sure that you have pointed your domain or subdomain to this server's IP address.  You can also run LetsEncrypt certbot later with the command 'certbot --apache'"
  130. echo -en "\n\n\n"
  131.  read -p "Would you like to use LetsEncrypt (certbot) to configure SSL(https) for your new site? (y/n): " yn
  132.     case $yn in
  133.         [Yy]* ) certbot --apache; echo "WordPress has been enabled at https://$dom  Please open this URL in a browser to complete the setup of your site.";break;;
  134.         [Nn]* ) echo "Skipping LetsEncrypt certificate generation";break;;
  135.         * ) echo "Please answer y or n.";;
  136.     esac
  137.  
  138. echo "Finalizing installation..."
  139. wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/bin/wp
  140. chmod +x /usr/bin/wp
  141.  
  142. echo -en "Completing the configuration of WordPress."
  143. wp core install --allow-root --path="/var/www/html" --title="$title" --url="$dom" --admin_email="$email"  --admin_password="$pass" --admin_user="$username"
  144.  
  145. wp plugin install wp-fail2ban --allow-root --path="/var/www/html"
  146. wp plugin activate wp-fail2ban --allow-root --path="/var/www/html"
  147. chown -Rf www-data.www-data /var/www/
  148. cp /etc/skel/.bashrc /root
  149.  
  150. echo "Installation complete. Access your new WordPress site in a browser to continue."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement