Automating WP application directory creation (domain name passed as a variable argument)

Ok, I just checked the WP CLI documentation. There are 3 important steps:

  1. Download WordPress: (I would use it with the options --path and --locale, but those are optional)

    wp core download
    
  2. Create a proper wp-config.php:

    wp config create --dbname=... --dbuser=... --dbpass=... --dbhost=...
    
  3. Install WordPress:

    wp core install --url=... --title=... --admin_user=... --admin_email=...
    

Now there are various ways you can combine these, add your own logic and even custom modifications such as chmod. I would probably use a script like this

#!/bin/bash
# Call this script with 9 arguments:
# $1 absolute path where to install
# $2 dbname
# $3 dbuser
# $4 dbpass
# $5 dbhost
# $6 URL
# $7 site title
# $8 admin user
# $9 admin email

# download WP
wp core download --path=$1

# create wp-config.php
wp config create --path=$1 --dbname=$2 --dbuser=$3 --dbpass=$4 --dbhost=$5

# install WP
wp core install --path=$1 --url=$6 --title=$7 --admin_user=$8 --admin_email=$9

# harden security
# maybe set chmod (https://www.wordfence.com/learn/how-to-restrict-wordpress-file-permissions/)
# maybe move wp-config.php one folder above web root (https://www.wordfence.com/learn/how-to-harden-wordpress-sites/#securing-your-database)

By passing the absolute path (and using it via --path, you can store this script on some central location and use it for installing multiple instances in different locations on the server)

Use it like so

./install_wp.sh /var/www/html/demo/wp demo_db demo demo_password localhost https://demo.local DemoSite demouser [email protected] && history -c

I am using && history -c to delete the bash history, as it contains sensitive db data. On my local machine, I got the error sh: 1: /usr/sbin/sendmail: not found and it printed out the admin password to stdout.