Skip to content

Instantly share code, notes, and snippets.

@polevaultweb
Last active November 8, 2023 01:30
Show Gist options
  • Save polevaultweb/a955d29d19dedfc7e979 to your computer and use it in GitHub Desktop.
Save polevaultweb/a955d29d19dedfc7e979 to your computer and use it in GitHub Desktop.
This script installs WordPress inside a sub directory
#!/bin/bash
# Installation:
## Download the script to your home directory
# Make sure it has execute permissions (`chmod +x wp-install-core-sub-dir.sh`).
# Install the script in one of the folders in your PATH. (`mv wp-install-core-sub-dir.sh /usr/local/bin/wp-install-core-sub-dir`)
#Usage:
# $ mkdir mysite
# $ cd mysite
# $ wp-install-core-sub-dir {sub-directory} {db_name} {db_user} {db_pass} {site_url} "{site_title}" {admin_user} {admin_pass} {admin_email}
CORE_DIR=${1-'wp'}
DB_NAME=${2-'wordpress'}
DB_USER=${3-'root'}
DB_PASS=${4-'password'}
SITE_URL=${5-'https://wordpress.test'}
SITE_TITLE=${6-'WordPress Site in a Subdirectory'}
SITE_USER=${7-'admin'}
SITE_PASS=${8-'password'}
SITE_EMAIL=${9-'your@email.com'}
# create the dir for the core files
mkdir $CORE_DIR
cd $CORE_DIR
# download WordPress files
wp core download
# create the wp-config.php file
wp config create --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS
# create the database
wp db create
# install WordPress (less than 5 mins)
wp core install --url=$SITE_URL --title="$SITE_TITLE" --admin_user=$SITE_USER --admin_password=$SITE_PASS --admin_email=$SITE_EMAIL
# Copy (not move) index.php file to root
cd ../
cp "$CORE_DIR/index.php" ./index.php
# Edit index.php to point to correct path of wp-blog-header.php
perl -p -i -e "s/\/wp-blog-header/\/$CORE_DIR\/wp-blog-header/g" index.php
# Update the siteurl in the database with sub directory path
cd $CORE_DIR
wp option update siteurl $(wp option get siteurl)/$CORE_DIR
# Uncomment the below line if you want the config in root
#cp "$CORE_DIR/wp-config.php" ./wp-config.php
echo 'Install finished!'
@danelige
Copy link

danelige commented Oct 17, 2018

Iain, thank you! few corrections that were needed to make this script work on my cPanel setup.

  1. line 47: sed -i '' ... drop the ''

  2. line 50: wp option update siteurl $(wp option get siteurl)/$CORE_DIR you need to add the path here too: wp option update siteurl $(wp option get siteurl --path=$CORE_DIR)/$CORE_DIR --path=$CORE_DIR

  3. in order to prep empty database and user in a cPanel environment, use UAPI command like this:

# cPanel API: This creates DB and USER in such a way that cPanel can track this new db and user
uapi --output=json Mysql create_database name="$DB_NAME"
uapi --output=json Mysql create_user name="$DB_USER" password="$DB_PASS"
uapi Mysql set_privileges_on_database user="$DB_USER" database="$DB_NAME" privileges=ALL%20PRIVILEGES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment