The quickest way to Setup WordPress: A Bash Script to Install PHP, WordPress, and MySQL on Ubuntu Server — Part01

Syedusmanahmad
4 min readAug 4, 2023

--

Description: Setting up a WordPress website on an Ubuntu server can be a time-consuming task, involving multiple installations and configurations. In this blog, we present an efficient and time-saving solution: a custom bash script that automates the entire process. The script installs the latest versions of PHP, WordPress, and MySQL, streamlining the setup procedure while ensuring a secure and optimized environment. Whether you’re a seasoned developer or a WordPress enthusiast, this script will simplify the deployment of your website, allowing you to focus on creating captivating content and engaging with your audience. Join us on this step-by-step journey toward a hassle-free WordPress installation on your Ubuntu server!

Introduction:

In the ever-evolving world of website development and content management, WordPress remains a popular and powerful platform for creating stunning websites. However, the process of setting up a WordPress site on an Ubuntu server often involves numerous manual steps, making it time-consuming and prone to human errors. But fear not! In this blog, we present an elegant solution to this predicament — a custom bash script that automates the entire setup process, ensuring a smooth and efficient installation of PHP, WordPress, and MySQL.

Why Automate? Whether you are an experienced developer or a WordPress enthusiast, automating the installation process can save valuable time and reduce the chance of mistakes. Our bash script simplifies the complex installation tasks, allowing you to focus on what truly matters — building and curating compelling content for your audience.

Getting Started: Before we dive into the script, let’s explore the key components it automates:

  1. PHP: The server-side scripting language that powers WordPress and enables dynamic content.
  2. WordPress: The world’s most popular content management system, facilitating easy website creation and management.
  3. MySQL: The robust relational database management system that stores WordPress data.

The Bash Script: Step-by-Step Guide Our script guides you through the entire installation process, ensuring a seamless deployment of your WordPress site on your Ubuntu server. Let’s walk through the step-by-step process of executing the script:

#!/bin/bash

print_green() {
echo -e "\e[32m$1\e[0m"
}
print_red() {
echo -e "\e[31m$1\e[0m"
}
if [[ $EUID -ne 0 ]]; then
print_red "This script must be run as root."
exit 1
fi

print_green "Updating package index and upgrading installed packages..."
apt-get update && apt-get upgrade -y

# Install required packages for PHP, MySQL, and WordPress
print_green "Installing prerequisites..."
apt-get install -y software-properties-common
add-apt-repository -y ppa:ondrej/php # For latest PHP version
apt-get update
apt-get install -y php7.4 php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip php7.4-xmlrpc

# Install MySQL server and set the root password (replace 'your_mysql_password' with your desired password)
print_green "Installing MySQL server..."
apt-get install -y mysql-server
mysql_secure_installation

print_green "Installing Apache web server..."
apt-get install -y apache2
a2enmod rewrite
systemctl restart apache2

# Download and extract WordPress
print_green "Downloading and extracting WordPress..."
cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz -C /var/www/html/
chown -R www-data:www-data /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress

print_green "WordPress installation is complete."
rm /tmp/latest.tar.gz

print_green "IMPORTANT: Make sure to note down the MySQL root password for future reference."
  1. Prerequisites: We’ll outline the necessary prerequisites, including a functional Ubuntu server and administrative privileges.
  2. Script Execution: With just a single command, you can run the script and initiate the automated installation process.
  3. PHP Installation: The script installs the latest version of PHP, setting the stage for WordPress to function efficiently.
  4. WordPress Setup: You can sit back as the script installs the latest version of WordPress, configuring it for optimal performance.
  5. MySQL Database: The script creates a MySQL database and sets up a dedicated user account to ensure seamless WordPress integration.
  6. Final Touches: Our script ensures the necessary permissions and configurations are in place, making your WordPress site fully operational.

To execute the script run the following commands:

#chmod +x wp_installation.sh
#./wp_installation.sh

Conclusion: With our powerful bash script, setting up a WordPress site on your Ubuntu server has never been easier. By automating the process of installing PHP, WordPress, and MySQL, we eliminate tedious manual steps and reduce the risk of errors. The result? A lightning-fast, secure, and feature-rich WordPress website, ready to captivate your audience with compelling content and seamless user experience.

Embrace the power of automation and embark on a stress-free journey to WordPress success. So, what are you waiting for? Let’s get started on creating your dream website today! Happy automating!

I hope you enjoyed reading this article, please feel free to contact me Syedusmanahmad if you have any questions.

Please feel free to write @ engr.syedusmanahmad@gmail.com | Linkedin https://www.linkedin.com/in/engrusman-ahmad for any queries on AWS/DevOps & stay tuned for the next write-up.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--

Syedusmanahmad

AWS & DevOps Architect | Linux, Docker, Kubernetes, Terraform, Jenkins, Git&GitHub, Ansible expert