|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +trap end_install EXIT |
| 6 | + |
| 7 | +# |
| 8 | +## |
| 9 | +update_system() { |
| 10 | + # |
| 11 | + apt update |
| 12 | + apt -yqq upgrade |
| 13 | +} |
| 14 | + |
| 15 | +# |
| 16 | +## |
| 17 | +install_base_system() { |
| 18 | + apt -yqq install perl 2>&1 |
| 19 | + |
| 20 | + # |
| 21 | + echo "UTC" > /etc/timezone |
| 22 | + dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>/dev/null |
| 23 | +} |
| 24 | + |
| 25 | +# |
| 26 | +## |
| 27 | +get_passwords() { |
| 28 | + # check if existing passwords file exists |
| 29 | + if [ ! -f /root/passwords.txt ]; then |
| 30 | + echo "Error: expecting /root/passwords.txt file to exist" |
| 31 | + exit 1 |
| 32 | + else |
| 33 | + # load existing file |
| 34 | + set -o allexport |
| 35 | + source /root/passwords.txt |
| 36 | + set +o allexport |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +# |
| 41 | +## |
| 42 | +install_wordpress() { |
| 43 | + # |
| 44 | + cd /var/www/html |
| 45 | + |
| 46 | + # |
| 47 | + rm -f index.php |
| 48 | + rm -f index.html |
| 49 | + |
| 50 | + # download |
| 51 | + curl -O https://wordpress.org/latest.tar.gz |
| 52 | + |
| 53 | + # untar |
| 54 | + tar -zxvf latest.tar.gz |
| 55 | + |
| 56 | + # change dir to wordpress |
| 57 | + cd wordpress |
| 58 | + |
| 59 | + # copy file to parent dir |
| 60 | + cp -rf . .. |
| 61 | + |
| 62 | + # move back to parent dir |
| 63 | + cd .. |
| 64 | + |
| 65 | + # remove wordpress folder |
| 66 | + rm -R wordpress |
| 67 | + |
| 68 | + # create wp config |
| 69 | + cp wp-config-sample.php wp-config.php |
| 70 | + |
| 71 | + # set database details with perl find and replace |
| 72 | + sed -i "s/database_name_here/$dbname/g" wp-config.php |
| 73 | + sed -i "s/username_here/$dbuser/g" wp-config.php |
| 74 | + sed -i "s/password_here/$dbpass/g" wp-config.php |
| 75 | + |
| 76 | + # set WP salts |
| 77 | + perl -i -pe' |
| 78 | + BEGIN { |
| 79 | + @chars = ("a" .. "z", "A" .. "Z", 0 .. 9); |
| 80 | + push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|"; |
| 81 | + sub salt { join "", map $chars[ rand @chars ], 1 .. 64 } |
| 82 | + } |
| 83 | + s/put your unique phrase here//ge |
| 84 | + ' wp-config.php |
| 85 | + |
| 86 | + # create uploads folder and set permissions |
| 87 | + mkdir -p wp-content/uploads |
| 88 | + chmod 775 wp-content/uploads |
| 89 | + |
| 90 | + # change ownership |
| 91 | + chown www-data:www-data . -Rf |
| 92 | + |
| 93 | + # remove zip file |
| 94 | + rm latest.tar.gz |
| 95 | +} |
| 96 | + |
| 97 | +# |
| 98 | +## |
| 99 | +install_adminer() { |
| 100 | + # |
| 101 | + wget http://www.adminer.org/latest.php -O /var/www/html/adminer.php |
| 102 | + chown www-data:www-data /var/www/html/index.php |
| 103 | +} |
| 104 | + |
| 105 | +start_install() { |
| 106 | + # |
| 107 | + . /etc/os-release |
| 108 | + |
| 109 | + # check is root user |
| 110 | + if [[ $EUID -ne 0 ]]; then |
| 111 | + echo "You must be root user to install scripts." |
| 112 | + sudo su |
| 113 | + fi |
| 114 | + |
| 115 | + # check os is ubuntu |
| 116 | + if [[ $ID != "ubuntu" ]]; then |
| 117 | + echo "Wrong OS! Sorry only Ubuntu is supported." |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + |
| 121 | + export DEBIAN_FRONTEND=noninteractive |
| 122 | + |
| 123 | + echo >&2 "Deploy-Script: [OS] $PRETTY_NAME" |
| 124 | +} |
| 125 | + |
| 126 | +end_install() { |
| 127 | + # clean up apt |
| 128 | + apt-get autoremove -y && apt-get autoclean -y && apt-get clean -y \ |
| 129 | + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* |
| 130 | + |
| 131 | + # return to dialog |
| 132 | + export DEBIAN_FRONTEND=dialog |
| 133 | + |
| 134 | + # remove script |
| 135 | + rm -f script.sh |
| 136 | +} |
| 137 | + |
| 138 | +# |
| 139 | +## |
| 140 | +main() { |
| 141 | + # |
| 142 | + start_install |
| 143 | + |
| 144 | + # |
| 145 | + update_system |
| 146 | + |
| 147 | + # |
| 148 | + install_base_system |
| 149 | + |
| 150 | + # |
| 151 | + get_passwords |
| 152 | + |
| 153 | + # |
| 154 | + install_wordpress |
| 155 | + |
| 156 | + # |
| 157 | + install_adminer |
| 158 | + |
| 159 | + # |
| 160 | + end_install |
| 161 | + |
| 162 | + echo >&2 "Wordpress install completed" |
| 163 | +} |
| 164 | + |
| 165 | +main |
0 commit comments