Skip to content

Instantly share code, notes, and snippets.

@Rmanaf
Last active September 5, 2024 11:30
Show Gist options
  • Save Rmanaf/1844b49d72db670974e3cc70e20a36bd to your computer and use it in GitHub Desktop.
Save Rmanaf/1844b49d72db670974e3cc70e20a36bd to your computer and use it in GitHub Desktop.
This script will set up a basic WordPress installation on an Ubuntu server and is designed to be simple and straightforward, suitable for quick deployments.
#!/bin/bash
# Function to generate a random password
generate_password() {
echo $(openssl rand -base64 12)
}
# Ask for server name (FQDN) and validate
while true; do
read -p "Enter server name (e.g., server1.example.com): " server_name
if [ -z "$server_name" ]; then
echo "Server name cannot be empty. Please enter a valid server name."
else
break
fi
done
# Ask for domain and validate
while true; do
read -p "Enter domain (e.g., example.com): " domain
if [ -z "$domain" ]; then
echo "Domain cannot be empty. Please enter a valid domain."
else
# Check if the domain configuration already exists in Apache
if [ -e /etc/apache2/sites-available/${domain}.conf ]; then
echo "Domain ${domain} already exists in Apache configuration. Please choose a different domain."
else
break
fi
fi
done
# Ask for port number, default to 80
read -p "Enter port number (default: 80): " port_number
port_number=${port_number:-80}
# Set a default admin email based on the domain
default_email="admin@${domain}"
read -p "Enter admin email (default: ${default_email}): " admin_email
admin_email=${admin_email:-$default_email}
# Ask for WordPress admin username and password
read -p "Enter WordPress admin username (default: root): " wp_admin_user
wp_admin_user=${wp_admin_user:-root}
read -p "Enter WordPress admin password (leave blank to generate a new one): " wp_admin_pass
# Ask for database details
read -p "Enter database name (default: wordpress): " db_name
db_name=${db_name:-wordpress}
# Ask for database username and password
read -p "Enter database username (default: wpagent): " db_user
db_user=${db_user:-wpagent}
read -p "Enter database password (leave blank to generate a new one): " db_pass
# Ask for WordPress installation directory
read -p "Enter directory name for WordPress installation (default: wordpress): " wp_dir
wp_dir=${wp_dir:-wordpress}
# Ask if multisite is to be enabled
read -p "Enable Multisite? (y/N, default: N): " enable_multisite
enable_multisite=${enable_multisite:-N}
# Ask if phpMyAdmin should be installed
read -p "Install and configure phpMyAdmin? (y/N, default: N): " install_phpmyadmin
install_phpmyadmin=${install_phpmyadmin:-N}
# Ask if firewall (UFW) should be configured
read -p "Configure UFW firewall? (y/N, default: N): " configure_firewall
configure_firewall=${configure_firewall:-N}
# Update package list and install necessary packages
apt update
apt install -y apache2 mysql-server php php-mysql libapache2-mod-php php-cli unzip wget curl openssl ufw
# Check if passwords need to be generated after package installation
if [ -z "$wp_admin_pass" ]; then
wp_admin_pass=$(generate_password)
fi
if [ -z "$db_pass" ]; then
db_pass=$(generate_password)
fi
# Loop for database name until a non-existing one is provided
while true; do
db_exists=$(mysql -e "SHOW DATABASES LIKE '${db_name}'" | grep "${db_name}" > /dev/null; echo "$?")
if [ $db_exists -eq 0 ]; then
echo "Database ${db_name} already exists. Please choose a different name."
read -p "Enter database name: " db_name
else
break
fi
done
# Check if the database user exists
db_user_exists=$(mysql -e "SELECT User FROM mysql.user WHERE User='${db_user}'" | grep "${db_user}" > /dev/null; echo "$?")
# If the user does not exist, create the user and set the password
if [ $db_user_exists -ne 0 ]; then
mysql -e "CREATE USER '${db_user}'@'localhost' IDENTIFIED BY '${db_pass}';"
mysql -e "GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user}'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"
else
echo "Database user ${db_user} already exists."
# Loop to check if the password is correct
while true; do
mysql -u"${db_user}" -p"${db_pass}" -e ";" 2>/dev/null
if [ $? -eq 0 ]; then
break
else
echo "Invalid password. Please try again."
read -s -p "Enter the password for the existing database user '${db_user}': " db_pass
fi
done
fi
# Create the database
mysql -e "CREATE DATABASE ${db_name};"
# Download and set up WordPress
cd /tmp
wget https://wordpress.org/latest.zip
unzip latest.zip
mv wordpress /var/www/html/${wp_dir}
chown -R www-data:www-data /var/www/html/${wp_dir}
chmod -R 755 /var/www/html/${wp_dir}
# Configure wp-config.php
cp /var/www/html/${wp_dir}/wp-config-sample.php /var/www/html/${wp_dir}/wp-config.php
sed -i "s/database_name_here/${db_name}/" /var/www/html/${wp_dir}/wp-config.php
sed -i "s/username_here/${db_user}/" /var/www/html/${wp_dir}/wp-config.php
sed -i "s/password_here/${db_pass}/" /var/www/html/${wp_dir}/wp-config.php
# Generate and add WordPress salts
salts=$(curl -s https://api.wordpress.org/secret-key/1.1/salt/)
sed -i "/AUTH_KEY/c\\${salts}" /var/www/html/${wp_dir}/wp-config.php
# Install WordPress using WP-CLI
sudo -u $SUDO_USER wp core install --url="${domain}" --title="My WordPress Site" --admin_user="${wp_admin_user}" --admin_password="${wp_admin_pass}" --admin_email="${admin_email}" --path="/var/www/html/${wp_dir}" --skip-email
# Set up Apache virtual host
cat <<EOL >/etc/apache2/sites-available/${domain}.conf
<VirtualHost *:${port_number}>
ServerAdmin ${admin_email}
ServerName ${server_name}
ServerAlias ${domain}
DocumentRoot /var/www/html/${wp_dir}
<Directory /var/www/html/${wp_dir}>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOL
# Enable the site and restart Apache
a2ensite ${domain}
a2enmod rewrite
systemctl restart apache2
# Multisite configuration if enabled
if [[ $enable_multisite =~ ^[Yy]$ ]]; then
sudo -u $SUDO_USER wp core multisite-convert --path="/var/www/html/${wp_dir}"
echo "Multisite has been enabled."
fi
# Configure firewall if requested
if [[ $configure_firewall =~ ^[Yy]$ ]]; then
ufw allow OpenSSH
ufw allow 'Apache Full'
ufw --force enable
echo "Firewall has been configured."
fi
# Clean up downloaded files
rm /tmp/latest.zip
# phpMyAdmin installation and configuration
if [[ $install_phpmyadmin =~ ^[Yy]$ ]]; then
if [ -d "/usr/share/phpmyadmin" ]; then
echo "phpMyAdmin is already installed. Skipping installation."
else
apt install -y phpmyadmin
ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
echo "phpMyAdmin is available at http://${domain}/phpmyadmin"
fi
fi
# Summary of installation
echo "-------------------------------------------"
echo "Installation Completed Successfully!"
echo "-------------------------------------------"
echo "Admin Email: ${admin_email}"
echo "WordPress Admin Username: ${wp_admin_user}"
echo "WordPress Admin Password: ${wp_admin_pass}"
echo "Database Name: ${db_name}"
echo "Database Username: ${db_user}"
echo "Database Password: ${db_pass}"
echo "-------------------------------------------"
echo "Visit http://${domain}:${port_number} to access your WordPress site."
@Rmanaf
Copy link
Author

Rmanaf commented Sep 4, 2024

To download and run the script, you can use the following commands:

curl -O https://gist.githubusercontent.com/Rmanaf/1844b49d72db670974e3cc70e20a36bd/raw/4f462cc1273bdc27b640ee4f68b682c27f093286/wp-installer.sh
chmod +x wp-installer.sh
sudo ./wp-installer.sh

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