To set up WordPress in an Ubuntu virtual machine using Vagrant, you can follow these steps:

  1. Install VirtualBox: Download and install Oracle VirtualBox from the official website (https://www.virtualbox.org/) for your host operating system.
  2. Install Vagrant: Download and install Vagrant from the official website (https://www.vagrantup.com/) for your host operating system.
  3. Create a new directory: Create a new directory where you want to set up your Vagrant project.
  4. Initialize Vagrant: Open a terminal or command prompt and navigate to the project directory.
  5. Run vagrant init hashicorp/bionic64 to initialize vagrant.  This command initializes a new Vagrant project with an Ubuntu 18.04 LTS (Bionic Beaver) base box.
  6. Edit Vagrantfile: Open the Vagrantfile generated in the project directory using a text editor. Modify it to include the following configuration. An example Vagrant file that can be used to set up a WordPress environment in an Ubuntu virtual machine using VirtualBox as the provider:



# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
# Set the box to use as a base image
config.vm.box = "ubuntu/bionic64"
# Set the provider to use
config.vm.provider "virtualbox"
# Forward the ports used by WordPress
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 3306, host: 3306
# Set the hostname and IP address
config.vm.hostname = "wordpress.local"
config.vm.network "private_network", ip: "192.168.33.10"
# Configure the virtual machine
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = 2
end
# Provision the virtual machine with WordPress and MySQL
config.vm.provision "shell", path: "provision.sh"
end

This Vagrantfile forwards ports 80 and 3306 to the host machine, sets a private IP address of 192.168.33.10 for the virtual machine, and provisions the virtual machine with a shell script called provision.sh.

Here is an example provision.sh script that can be used to install WordPress and MySQL:

#!/usr/bin/env bash
# Update the package manager
sudo apt-get update

# Install Apache and PHP
sudo apt-get install -y apache2 php php-mysql php-curl php-gd php-mbstring php-xml

# Install MySQL
sudo apt-get install -y mysql-server

# Configure MySQL
sudo mysql -u root -e "CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -u root -e "CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'password';"
sudo mysql -u root -e "GRANT ALL ON wordpress.* TO 'wordpress'@'localhost';"
sudo mysql -u root -e "FLUSH PRIVILEGES;"

# Download and configure WordPress
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

This script installs Apache, PHP, and MySQL, creates a new database and user for WordPress, and downloads and configures WordPress.

To use this Vagrantfile and script, save them to a directory on your host machine, navigate to that directory in a terminal, and run the vagrant up command. This will create and provision the virtual machine. Once the virtual machine is running, you can access WordPress by navigating to http://localhost:8080 in your web browser.