· 5 months ago · Apr 18, 2025, 05:35 PM
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
10if [[ -d /var/www/wordpress ]]
11then
12 mv /var/www/html /var/www/html.old
13 mv /var/www/wordpress /var/www/html
14fi
15chown -Rf www-data:www-data /var/www/html
16
17# if applicable, configure wordpress to use mysql dbaas
18if [ -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 **/
35define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL );
36EOM
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
53fi
54
55echo "This script will copy the WordPress installation into"
56echo "Your web root and move the existing one to /var/www/html.old"
57echo "--------------------------------------------------"
58echo "This setup requires a domain name. If you do not have one yet, you may"
59echo "cancel this setup, press Ctrl+C. This script will run again on your next login"
60echo "--------------------------------------------------"
61echo "Enter the domain name for your new WordPress site."
62echo "(ex. example.org or test.example.org) do not include www or http/s"
63echo "--------------------------------------------------"
64
65a=0
66while [ $a -eq 0 ]
67do
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
75fi
76done
77sed -i "s/\$domain/$dom/g" /etc/apache2/sites-enabled/000-default.conf
78a2enconf block-xmlrpc
79
80service apache2 restart
81
82echo -en "Now we will create your new admin user account for WordPress."
83
84function 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
112wordpress_admin_account
113
114while true
115do
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
126done
127
128echo -en "\n\n\n"
129echo "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'"
130echo -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
138echo "Finalizing installation..."
139wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/bin/wp
140chmod +x /usr/bin/wp
141
142echo -en "Completing the configuration of WordPress."
143wp core install --allow-root --path="/var/www/html" --title="$title" --url="$dom" --admin_email="$email" --admin_password="$pass" --admin_user="$username"
144
145wp plugin install wp-fail2ban --allow-root --path="/var/www/html"
146wp plugin activate wp-fail2ban --allow-root --path="/var/www/html"
147chown -Rf www-data.www-data /var/www/
148cp /etc/skel/.bashrc /root
149
150echo "Installation complete. Access your new WordPress site in a browser to continue."