I have written the following Bash script which includes an Nginx WordPress bootstrapper: A program establish WordPress apps fast on a Debian-Nginx environment.
It utilizes CertBot for HTTPS, SSHguard for protection from BFA (see Nginx conf), WP-CLI for creating a WP app dir and conf file fast, MySQL CLI to create a dbstack and a local function named rse to restart the server and giving proper permissions to the app dir.
#!/bin/bash
domain="$1" && test -z "$domain" && exit 2
environment() {
read -sp "Please enter the app DB root password: " dbrootp_1 && echo
read -sp "Please enter the app DB root password again:" dbrootp_2 && echo
if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi
read -sp "Please enter the app DB user password: " dbuserp_1 && echo
read -sp "Please enter the app DB user password again:" dbuserp_2 && echo
if [ "$dbuserp_1" != "$dbuserp_2" ]; then echo "Values unmatched. Please try again." && exit 2 fi
}
environment
wordpress() {
rm -rf "$drt"/"$domain"/ 2>/dev/null
wp core download --path="$drt"/"$domain"/ --allow-root
wp config create --path="$drt"/"$domain"/ --dbname="$domain" --dbuser="$domain" --dbpass="$dbuserp" --dbhost="localhost" --allow-root
}
wordpress
nginx() {
rm "$s_a/$domain.conf" 2>/dev/null
rm "$s_e/$domain.conf" 2>/dev/null
cat <<-EOF > "$s_a/$domain.conf"
server {
root ${drt}/${domain}/;
server_name ${domain} www.${domain};
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {expires 365d;}
location "/wp-login\.php" {access_log "/var/log/httpd/wordpress-logins.log";}
}
EOF
ln -sf "$s_a"/"$domain".conf "$s_e"
rse
}
nginx
certbot() {
certbot --nginx -d "$domain" -d www."$domain"
rse
}
certbot
database() {
cat <<-EOF | mysql -u root -p"$dbrootp_1"
DROP USER IF EXISTS "$domain"@"localhost";
DROP database IF EXISTS "$domain";
CREATE USER "$domain"@"localhost" IDENTIFIED BY "$dbuserp_1";
CREATE DATABASE "$domain";
GRANT ALL PRIVILEGES ON "$domain".* TO "$domain"@"localhost";
EOF
}
database
finalize() {
echo "Change http to http2 in your Nginx app conf and run rse"
}
finalize
Note
Before executing the script I declared in the end of $HOME/.bashrc:
drt="/var/www/html"
s_a="etc/nginx/sites-available"
s_e="etc/nginx/sites-enabled"