Skip to content

Instantly share code, notes, and snippets.

@Maxstupo
Last active September 15, 2018 17:50
Show Gist options
  • Save Maxstupo/5fb562bec486ad97473f8a67476d781e to your computer and use it in GitHub Desktop.
Save Maxstupo/5fb562bec486ad97473f8a67476d781e to your computer and use it in GitHub Desktop.
Install Wordpress
#!/bin/bash
#
# Program: install_wordpress.sh
# Purpose: Installs WordPress (Tested on Ubuntu Server 16.04.2 LTS)
# Date: 23/03/2017
# Author: Maxstupo
#
# Command Line Arguments:
# $1 = the username for ownership of files (default: admin)
# $2 = the new db user password (default: password)
# $3 = the webserver root directory (default: /var/www/html)
#
#
# Note: Apache2 and MySQL must be installed before running this script. (e.g. LAMP installed)
# Note: After running the script, you need to add 'AllowOverride All' for the WP_ROOT path within the active virtualhost conf to allow permalinks.
# Note: After running the script, you will need to add the generated salts to WP_ROOT/wp-config.php
#
#
WP_OWNER=${1:-admin}
# The install directory for wordpress.
WP_ROOT=${3:-/var/www/html}
# Webserver group
WS_GROUP=www-data
# The login username for creating the wordpress database.
DB_USERNAME=root
# The login details for the new wordpress user.
NEW_DB_USER_NAME=wordpressuser
NEW_DB_USER_PASSWORD=${2:-password}
echo "Installing wordpress at ${WP_ROOT}"
####################### Update and install wordpress packages #######################
echo "Updating and installing wordpress packages..."
sudo apt-get update
sudo apt-get install php-ssh2 php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc
echo "Enabling apache2 rewrite."
sudo a2enmod rewrite # wordpress permlink management
echo "Restarting apache2..."
sudo service apache2 restart
############################ Download and un-zip wordpress ###########################
echo " "
echo "Downloading wordpress latest..."
cd ~/
wget http://wordpress.org/latest.tar.gz
echo "Unpackaging wordpress latest..."
tar xzvf latest.tar.gz #unzip wordpress
# Clear the webserver root
echo "Clearing directory ${WP_ROOT}"
sudo rm -r ${WP_ROOT}
sudo mkdir ${WP_ROOT}
echo "Moving contents of wordpress directory to ${WP_ROOT}"
sudo mv ~/wordpress/* ${WP_ROOT}/ # Move unpacked wordpress into active apache directory.
echo "Deleting packed/unpacked wordpress downloads..."
sudo rm -rf ~/latest.tar.gz
sudo rm -rf ~/wordpress
cp ${WP_ROOT}/wp-config-sample.php ${WP_ROOT}/wp-config.php # Use default config.
echo "Replaced wp-config.php with wp-config-sample.php"
echo "Creating directory ${WP_ROOT}/wp-content/uploads"
sudo mkdir ${WP_ROOT}/wp-content/uploads # Make upload folder so permissions can be set.
echo "Creating directory ${WP_ROOT}/wp-content/upgrade"
sudo mkdir ${WP_ROOT}/wp-content/upgrade # Make upgrade folder so permissions can be set.
echo "Creating file ${WP_ROOT}/.htaccess"
sudo touch ${WP_ROOT}/.htaccess # Create htaccess for wordpress so permissions can be set.
echo "Appending FS_METHOD='direct' to ${WP_ROOT}/wp-config.php"
echo "define('FS_METHOD','direct');" | sudo tee -a ${WP_ROOT}/wp-config.php > /dev/null
echo "Finished downloading/unpackaging wordpress."
#######################################################################################
################################## Setup permissions ##################################
echo " "
echo "Setting wordpress file/directory permissions..."
echo "Setting owner and group for ${WP_ROOT}"
sudo chown -R ${WP_OWNER}:${WS_GROUP} ${WP_ROOT}
echo "Setting all directory permissions within ${WP_ROOT} to 2750..."
# Set permissions for directories to allow all to read but only allow owner and group to write/execute. Also set gidbit so all newly created files will be set with correct permissions.
sudo find ${WP_ROOT} -type d -exec sudo chmod 2750 {} \; # u+rwx,g+rxs
echo "Setting all file permissions within ${WP_ROOT} to 0750..."
sudo find ${WP_ROOT} -type f -exec sudo chmod 0750 {} \; # u+rwx,g+rx
echo "Setting ${WP_ROOT}/wp-content to 0770 / 2770..."
# Allow wordpress to manage wp-content (Allows plugin/theme manager from within website)
sudo find ${WP_ROOT}/wp-content -type d -exec sudo chmod 2770 {} \;
sudo find ${WP_ROOT}/wp-content -type f -exec sudo chmod 0770 {} \;
echo "Setting ${WP_ROOT}/wp-config.php to 0770..."
#Allow wordpress to manage wp-config.php (but prevent world access)
sudo chmod 0770 ${WP_ROOT}/wp-config.php # ug+rwx
echo "Setting ${WP_ROOT}/.htaccess to 0770..."
# Allow wordpress to manage htaccess (permlink management).
sudo chmod 0770 ${WP_ROOT}/.htaccess # ug+rwx
echo "Finsihed setting permissions."
#########################################################################################
################################ Generate & display SALTS ###############################
echo " "
echo "Generating wordpress salts..."
echo " "
sudo curl -s https://api.wordpress.org/secret-key/1.1/salt/
echo " "
echo "Use the above salts within ${WP_ROOT}/wp-config.php"
echo " "
#########################################################################################
################################ Setup wordpress database ###############################
echo "Setting up database..."
# Setup database for wordpress.
sudo mysql -u ${DB_USERNAME} -p -e "CREATE DATABASE IF NOT EXISTS wordpress;CREATE USER IF NOT EXISTS ${NEW_DB_USER_NAME}@localhost IDENTIFIED BY '${NEW_DB_USER_PASSWORD}';GRANT ALL PRIVILEGES ON wordpress.* TO ${NEW_DB_USER_NAME}@localhost;FLUSH PRIVILEGES;"
#########################################################################################
echo "Restarting apache2...."
sudo service apache2 restart
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment