Skip to content

Instantly share code, notes, and snippets.

@amieiro
Last active August 29, 2022 21:54
Show Gist options
  • Save amieiro/7e1fe821814a7cd63728f6c1be5b453f to your computer and use it in GitHub Desktop.
Save amieiro/7e1fe821814a7cd63728f6c1be5b453f to your computer and use it in GitHub Desktop.
WordPress installation script
#!/usr/bin/env bash
# wp-create.sh
# This script creates:
# 1. A user in the system with his own directory
# 2. Create the database, the user and grant access to this user to this database
# 3. Download WordPress, create the config file and install WordPress
# This script doesn't configure the webserver neither the PHP-FPM
# You have to be root to execute it
if [ $(id -u) -eq 0 ]; then
# Set variables
USER=jesusamieiro # Change it
USERPASSWORD="$(openssl rand -base64 20)"
WPPATH=/home/${USER}/wordpress # Change it
WPLOCALE=es_ES # Change it
WPURL=https://www.jesusamieiro.com # Change it
WPTITLE="My awesome site"
WPUSER=manager-${USER}
WPPASSWORD="$(openssl rand -base64 20)"
WPADMINEMAIL="email@example.com" # Change it
DATABASENAME=$USER
DATABASEUSER=$USER
DATABASEUSERPASSWORD="$(openssl rand -base64 20)"
# Get the MySQL root password
read -sp "Enter the MySQL root password: " MYSQLROOTPASSWORD
until mysql -u root -p${MYSQLROOTPASSWORD} -e ";" ; do
read -sp "Can't connect, please retry: " MYSQLROOTPASSWORD
done
# Create the Linux user
useradd -m -p "${USERPASSWORD}" ${USER}
# Create the working directory
mkdir -p ${WPPATH}
# Create the database, the user and grant access to this user to this database
mysql -uroot -p${MYSQLROOTPASSWORD} -e "CREATE DATABASE ${DATABASENAME} CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql -uroot -p${MYSQLROOTPASSWORD} -e "CREATE USER ${DATABASEUSER}@localhost IDENTIFIED BY '${DATABASEUSERPASSWORD}';"
mysql -uroot -p${MYSQLROOTPASSWORD} -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';"
mysql -uroot -p${MYSQLROOTPASSWORD} -e "FLUSH PRIVILEGES;"
# Download WordPress, create the config file and install WordPress
cd ${WPPATH}
wp core download --locale=${WPLOCALE} --allow-root
wp config create --dbname=${DATABASENAME} --dbuser=${DATABASEUSER} --dbpass="${DATABASEUSERPASSWORD}" --allow-root
wp core install --url=${WPURL} --title="${WPTITLE}" --admin_user=${WPUSER} --admin_password="${WPPASSWORD}" --admin_email=${WPADMINEMAIL} --allow-root
# Update the permissions for the WordPress directory
chown ${USER}:${USER} ${WPPATH} -R
find ${WPPATH} -type f -exec chmod 644 {} +
find ${WPPATH} -type d -exec chmod 755 {} +
# Print the user and database passwords
echo "The password for the system user ${USER} is: ${USERPASSWORD}"
echo "The password for the user ${DATABASEUSER} of the database ${DATABASENAME} is: ${DATABASEUSERPASSWORD}"
echo "The password for the WordPress user ${WPUSER} is: ${WPPASSWORD}"
echo "Now you have to configure the webserver for this WordPress"
else
echo "Only root user can execute this script"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment