Dockerized WordPress Manager
In this article we will createa bash script to deploy our WordPress site using docker containerization.
Preferably in Bash to perform the following tasks:-
Check if docker and docker-compose are installed on the system. If not present, install the missing packages.
The script should be able to create a WordPress site using the latest WordPress Version. Please provide a way for the user to provide the site name as a command-line argument.
It must be a LEMP stack running inside containers (Docker) and a docker-compose file is a must.
Create a /etc/hosts entry for example.com pointing to localhost. Here we are assuming the user has provided example.com as the site name.
Prompt the user to open example.com in a browser if all goes well and the site is up and healthy.
Add another subcommand to enable/disable the site (stopping/starting the containers)
Add one more subcommand to delete the site (deleting containers and local files).
#!/bin/bash
# Check if docker is installed, and install if missing
if ! command -v docker &> /dev/null; then
echo "Docker is not installed. Installing Docker..."
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
echo "Docker installed successfully."
fi
# Check if docker-compose is installed, and install if missing
if ! command -v docker-compose &> /dev/null; then
echo "Docker Compose is not installed. Installing Docker Compose..."
sudo apt-get -y install docker-compose
echo "Docker Compose installed successfully."
fi
create_wordpress_site() {
site_name=$1
# Check if a site with the same name already exists
if [ -d "$site_name" ]; then
echo "Error: The site $site_name already exists."
exit 1
fi
# Create a directory for the site
mkdir "$site_name"
cd "$site_name"
# Create a basic docker-compose.yml for LEMP stack
cat <<EOF >docker-compose.yml
version: '3'
services:
wordpress:
image: wordpress:latest
ports:
- "80:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- wordpress:/var/www/html
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
- db_data:/var/lib/mysql
volumes:
wordpress:
db_data:
EOF
# Create a custom hosts entry for example.com
sudo -- sh -c "echo '127.0.0.1 example.com' >> /etc/hosts"
# Start the containers
docker-compose up -d
echo "Site created successfully. Open example.com in your browser."
}
enable_disable_site() {
site_name=$1
if [ ! -d "$site_name" ]; then
echo "Error: The site $site_name does not exist."
exit 1
fi
cd "$site_name"
if docker-compose ps | grep -q "Up"; then
docker-compose stop
echo "Site $site_name disabled."
else
docker-compose up -d
echo "Site $site_name enabled. Open example.com in your browser."
fi
}
delete_site() {
site_name=$1
if [ ! -d "$site_name" ]; then
echo "Error: The site $site_name does not exist."
exit 1
fi
cd "$site_name"
# Stop and remove the containers
docker-compose down
# Remove the site directory
cd ..
rm -rf "$site_name"
echo "Site $site_name deleted successfully."
}
# Check for the subcommand and call the appropriate function
case "$1" in
"create")
if [ -z "$2" ]; then
echo "Error: Please provide a site name."
exit 1
fi
create_wordpress_site "$2"
;;
"enable")
if [ -z "$2" ]; then
echo "Error: Please provide a site name."
exit 1
fi
enable_disable_site "$2"
;;
"delete")
if [ -z "$2" ]; then
echo "Error: Please provide a site name."
exit 1
fi
delete_site "$2"
;;
*)
echo "Usage: $0 {create <site_name> | enable <site_name> | delete <site_name>}"
exit 1
esac
Save this script in a file with this name wp_site_manager.sh.
- To create a WordPress site named "mywordpresssite," run:
./wp_site_manager.sh create mywordpresssite
- To enable or disable the site "mywordpresssite," use:
./wp_site_manager.sh enable mywordpresssite
./wp_site_manager.sh disable mywordpresssite
- To delete the site "mywordpresssite," run:
./wp_site_manager.sh delete mywordpresssite
Note: The script assumes that the user running the script has sudo privileges to modify the /etc/hosts
file. Additionally, the script uses the latest WordPress image and a basic MySQL configuration. For production use, you may want to adjust the configurations according to your needs (e.g., use volumes for persistent data, set secure passwords, etc.).
Let's modify the script to use "everwintools.com" as the site name. Make sure you have executed the script as mentioned in the previous response before running the commands below.
- Create the WordPress site named "everwintools.com":
./wp_site_manager.sh create everwintools.com
- Enable or disable the site "everwintools.com":
./wp_site_manager.sh enable everwintools.com
./wp_site_manager.sh disable everwintools.com
- Delete the site "everwintools.com":
./wp_site_manager.sh delete everwintools.com
Please note that the script will create the "everwintools.com" site using the latest WordPress version and run it within a Docker container as part of the LEMP stack. After creating the site, you should be able to access it by opening "everwintools.com" in your web browser (if all goes well and the site is up and healthy).
Now, give permission to the bash script:-
chmod +x wp_site_manager.sh
Now, run this command and boom, your project will containerize:
./wp_site_manager.sh
Now, check your docker, you will see the container and images of WordPress and MySQL under the Everwintools.com