Skip to content

Instantly share code, notes, and snippets.

@nunomcruz
Forked from phlbnks/wp_cli.sh
Last active January 3, 2024 16:01
Show Gist options
  • Save nunomcruz/75fd81028ff788926e29b15509b01547 to your computer and use it in GitHub Desktop.
Save nunomcruz/75fd81028ff788926e29b15509b01547 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
#
# Description:
# This will deploy WordPress in the current directory.
# Without modification it:
# - will configure basic security:
# - remove initial user created
# - deploy 6G firewall in .htaccess
# - attempt to prevent user enumeration in .htaccess
# - protect sensitive files and disallow executables in /wp-uploads
# - presumes a htpasswd file called wp-login is located in /etc/apache2/
# - disallows theme/plugin editor in wp-admin
# - only allows core/plugin/theme updates in wp-admin when using a cookie set using &key=xxx
# - presumes it is a staging environment so sets 'discourage search engines'
# - deletes akismet and hello_dolly plugins
# - deletes the default page and post
# - creates a blank page called Home and sets it as the frontpage
# - sets permalinks to /%postaname%/
# - optionally it:
# - bootstraps an _S theme
# - installs some plugins - advanced-custom-fields autoptimize better-wp-security breadcrumb-navxt broken-link-checker cms-tree-page-view custom-post-type-ui ewww-image-optimizer google-analytics-dashboard-for-wp google-captcha google-sitemap-generator gravity-forms-custom-post-types plugincheck query-monitor redirection re generate-thumbnails simple-local-avatars theme-check wp-fail2ban wp-pagenavi wp-super-cache
# - finally it will delete itself after it finishes.
clear
echo "============================================"
echo "WordPress Install Script"
echo "============================================"
echo "Installing latest version of wp-cli"
curl -s -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
echo "Do you need to setup new local MySQL server? (y/n)"
read -e setupmysqlserver
if [ "$setupmysqlserver" == n ] ; then
echo "Do you need to setup new MySQL database? (y/n)"
read -e setupmysql
if [ "$setupmysql" == y ] ; then
echo "MySQL Admin User: "
read -e mysqluser
echo "MySQL Admin Password: "
read -s mysqlpass
echo "MySQL Host (Enter for default 'localhost'): "
read -e mysqlhost
mysqlhost=${mysqlhost:-localhost}
fi
else
mysqluser=root
mysqlpass=""
mysqlhost=localhost
setupmysql=y
fi
echo "WP Database Name (Enter for default 'wordpress'): "
read -e dbname
dbname=${dbname:-wordpress}
echo "WP Database User (Enter for default 'wordpress'): "
read -e dbuser
dbuser=${dbuser:-wordpress}
echo "WP Database Password (Please use a random password):"
read -s dbpass
echo "WP Database Table Prefix [numbers, letters, and underscores only] (Enter for default 'wp_'): "
read -e dbtable
dbtable=${dbtable:-wp_}
echo "Last chance - sure you want to run the install? (y/n)"
read -e run
if [ "$run" == y ] ; then
if [ "$setupmysqlserver" == y ] ; then
echo "============================================"
echo "Installing Database Server."
echo "============================================"
yum install mariadb mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
fi
if [ "$setupmysql" == y ] ; then
echo "============================================"
echo "Setting up the database."
echo "============================================"
#login to MySQL, add database, add user and grant permissions
dbsetup="create database $dbname;GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@$mysqlhost IDENTIFIED BY '$dbpass';FLUSH PRIVILEGES;"
if [ $setupmysqlserver == y ]; then
mysql -u $mysqluser -e "$dbsetup"
else
mysql -u $mysqluser -p$mysqlpass -e "$dbsetup"
fi
if [ $? != "0" ]; then
echo "============================================"
echo "[Error]: Database creation failed. Aborting."
echo "============================================"
exit 1
fi
fi
echo "============================================"
echo "Installing Web Server."
echo "============================================"
yum install httpd php php-common php-mysql php-gd php-xml php-mbstring php-xmlrpc unzip wget -y
systemctl start httpd
systemctl enable httpd
cd /var/www/html
# configure wp cli to allow htaccess modification / re-write flushing.
cat > wp-cli.local.yml <<'EOL'
apache_modules:
- mod_rewrite
EOL
echo "============================================"
echo "Downloading WordPress for you."
echo "============================================"
#download wordpress
wp core download --locale=pt_PT
echo "+++ Configuring..."
wp core config --dbname=$dbname --dbprefix=$dbtable --dbuser=$dbuser --dbpass=$dbpass
#create uploads folder and set permissions
mkdir -p wp-content/uploads
chmod 775 wp-content/uploads
#remove readme.html
rm readme.html
#create root .htaccess with some useful starters
cat > .htaccess <<'EOL'
# Protect this file
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
# Prevent directory listing
Options -Indexes
EOL
#create .htaccess to protect uploads directory
cat > wp-content/uploads/.htaccess <<'EOL'
# Protect this file
<Files .htaccess>
Order Deny,Allow
Deny from All
</Files>
# whitelist file extensions to prevent executables being
# accessed if they get uploaded
order deny,allow
deny from all
<Files ~ "(?i)\.(docx?|xlsx?|pptx?|txt|pdf|xml|css|jpe?g|png|gif)$">
allow from all
</Files>
EOL
echo "========================="
echo "Configuring WordPress."
echo "========================="
# useful ref: https://indigotree.co.uk/automated-wordpress-installation-with-bash-wp-cli/
echo "Site URL (no trailing slash): "
read -e siteurl
sitename="LXi - Gestão de dados"
wp core install --url="${siteurl}/" --title="$sitename" --admin_user=admin --admin_password=admin [email protected] --skip-email
#echo "Is this a sub-directory install? (y/n)"
#read -e issubdir
#if [ "$issubdir" == y ] ; then
# read -e subdir
# wp option update "${siteurl}/$issubdir"
#fi
echo "+++ Setting up users..."
echo "Admin user name: "
read -e adminname
echo "Admin user email: "
read -e adminemail
echo "Admin user pass (Please use a random password): "
read -s adminpass
wp user create $adminname $adminemail --role=administrator --user_pass=$adminpass
echo "+++ Deleting dummy admin user..."
wp user delete admin --yes
echo "+++ Setting permalinks to /%postaname%/..."
wp rewrite structure '/%postname%/' --hard
wp rewrite flush --hard
echo "+++ Sample page and post deleted when users cleaned; now create empty 'Home' page..."
#echo "+++ Deleting sample page and post; create empty 'Home' page..."
#wp post delete $(wp post list --post_type=page,post --field=ID --format=ids)
wp post create --post_type=page --post_title=Home --post_status=publish --post_content="LXi - Gestão de Dados"
echo "+++ Set frontpage setting to show a page..."
wp option update show_on_front 'page'
echo "+++ Set 'Home' to be the frontpage..."
wp option update page_on_front $(wp post list --post_type=page --pagename=home --field=ID --format=ids)
echo "+++ Adjusting plugins"
wp plugin delete akismet hello
echo "+++ Installing Hestia Theme"
wp theme install hestia --activate
wp plugin install themeisle-companion pirate-forms --activate
echo "+++ Opening TCP/80 at firewall"
firewall-cmd --zone=public --add-port=80/tcp --permanent
echo "+++ Reloading firewall"
firewall-cmd --reload
echo "+++ Cleaning..."
#remove bash script if it exists in this dir
[[ -f "wp_cli.sh" ]] && rm "wp_cli.sh"
echo "========================="
echo "[Success]: Installation is complete."
echo "========================="
else
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment