How to automate installing WordPress on your Ubuntu VM

How to automate installing WordPress on your Ubuntu VM

ยท

3 min read

This blog will walk you through making a script, which is going to install and configure WordPress on your Ubuntu VM.

Installing WordPress along with MySQL Database requires several steps and hence, is quite tedious. So why not automate this using a BASH Script?

This is what we are going to do now.

Installing WordPress have the following steps:

  • Step 1: Install Apache

  • Step 2: Install MySQL

  • Step 3: Install PHP

  • Step 4: Install WordPress

  • Step 5: Create a Database for WordPress

  • Step 6: Setup and Configure WordPress

Writing the script

This is a very straightforward script. We have some functions:

  • install_packages

  • configure_mysql

  • configure_wordpress

  • configure_apache

And they do, what they're named for! After executing these functions, we are going to expose the http port of your VM, so that you can access WordPress from your system as well on this address:

http://<IP_OF_YOUR_VM>/wordpress/

#!/bin/bash

############################################################
## Written by - Harshit Sharma <harshits908@gmail.com>    ##
## THIS BASH SCRIPT CAN BE RUN ON UBUNTU. THIS WILL       ##
## INSTALL WORDPRESS ALONG WITH MYSQL DATABASE ON         ##
## YOUR SYSTEM AUTOMATICALLY AND READY-TO-USE             ##
############################################################

# Function to install required packages
install_packages() {
    echo $'STEP 1: Installing the required packages\n\n'
    sudo apt update
    sudo apt install -y apache2 mysql-server php libapache2-mod-php php-mysql
    sudo systemctl enable apache2
    sudo systemctl enable mysql
}

# Function to configure MySQL and create a WordPress database and user
configure_mysql() {
    # Create a WordPress database and user
    echo $'STEP 2: Configuring the MySQL Database\n\n'
    sudo systemctl enable mysql
    read -p "Enter your MySQL root password: " rootpass
    read -p "Enter your database name (eg wordpress): " db_name
    read -p "Enter your username (eg wpuser): " db_user
    read -p "Enter your password: " db_pass

    echo "CREATE DATABASE $db_name;" | sudo mysql -u root -p$rootpass
    echo "CREATE USER '$db_user'@'localhost' IDENTIFIED BY '$db_pass';" | sudo mysql -u root -p$rootpass
    echo "GRANT ALL PRIVILEGES ON wordpress.* TO '$db_user'@'localhost';" | sudo mysql -u root -p$rootpass
    echo "FLUSH PRIVILEGES;" | sudo mysql -u root -p$rootpass
}

# Function to download and configure WordPress
configure_wordpress() {
    echo $'STEP 3: Configuring Wordpress\n\n'
    sudo apt install -y wget >> activity.log
    sudo wget -P /var/www/html https://wordpress.org/latest.tar.gz
    sudo tar -xzf /var/www/html/latest.tar.gz -C /var/www/html
    sudo chown -R www-data:www-data /var/www/html/wordpress
    sudo mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
    sudo sed -i "s/database_name_here/$db_name/" /var/www/html/wordpress/wp-config.php
    sudo sed -i "s/username_here/$db_user/" /var/www/html/wordpress/wp-config.php
    sudo sed -i "s/password_here/$db_pass/" /var/www/html/wordpress/wp-config.php
}

# Function to enable Apache modules and restart Apache
configure_apache() {
    echo $'STEP 4: Finishing touch\n\n'
    sudo systemctl restart apache2
}

# Main function to run the script
main() {
    install_packages
    configure_mysql
    configure_wordpress
    configure_apache

    # Expose the http port of your VM to access WordPress
    sudo ufw allow http

    echo "WordPress and MySQL installation and configuration completed successfully."
    echo "You can now access your WordPress site by visiting http://your-server-ip/wordpress"
}

main

Running the script

Save the script as script.sh

Give the permission to execute:

sudo chmod +x ./script.sh

Execute the script:

./script.sh

And let the thing roll out!

Conclusion

So that was it for this blog! Feel free to use this script. โœ…

If you find this somewhat informative, please like this blog! โœจ

Connect with me:

๐Ÿ”— LinkedIn: linkedin.com/in/harshit-sharma--

๐Ÿ”— My YouTube Channel: youtube.com/c/CoderBuddy?sub_confirmation=1

๐Ÿ”— GitHub: github.com/harshit-sharma-gits

ย