Introduction
Creating a local development environment for a WordPress site can be a complex task, but we are going to simplify the process by automating this setup using vagrant. In this blog post, we'll guide you through the steps of installing and configuring a WordPress site on a virtual machine using a Vagrantfile. The provided Vagrantfile includes a script that automates the installation of necessary components such as Apache, MySQL, and PHP, making the process efficient and hassle-free.
If your are unaware with vagrant setup and installation then do check out my previous blogs, links as below:
Step 1: Create a Virtual Machine with Vagrant
Start by creating a new directory for your project and navigate to it in the terminal. Run the following command to initialize a new Vagrantfile:
vagrant init ubuntu/jammy64
#Replace this with your linux distro name
This command creates a basic Vagrantfile in your project directory. Open the file with a text editor to customize it.
Step 2: Configure the Vagrantfile
Replace the content of the Vagrantfile with the following configuration:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.network "public_network"
config.vm.network "private_network", ip: "192.168.56.50"
# you can change the IP address, but be careful so they don't collide with some occupied ip
config.vm.provision "shell", inline: <<-SHELL
#installing dependencies
sudo apt update
sudo apt install apache2 \
ghostscript \
libapache2-mod-php \
mysql-server \
php \
php-bcmath \
php-curl \
php-imagick \
php-intl \
php-json \
php-mbstring \
php-mysql \
php-xml \
php-zip -y
#installing wordpress
sudo mkdir -p /srv/www
sudo chown www-data: /srv/www
curl https://wordpress.org/latest.tar.gz | sudo -u www-data tar zx -C /srv/www
#configure apache2
cat > /etc/apache2/sites-available/wordpress.conf << EOF
<VirtualHost *:80>
DocumentRoot /srv/www/wordpress
<Directory /srv/www/wordpress>
Options FollowSymLinks
AllowOverride Limit Options FileInfo
DirectoryIndex index.php
Require all granted
</Directory>
<Directory /srv/www/wordpress/wp-content>
Options FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
EOF
#enabling wordpress site
sudo a2ensite wordpress
#enabling URL rewriting
sudo a2enmod rewrite
#Disabling the default page “It Works” site
sudo a2dissite 000-default
#configuring mysql database
sudo mysql -u root -e 'CREATE DATABASE wordpress;'
sudo mysql -u root -e 'CREATE USER wordpress@localhost IDENTIFIED BY "yourpassword";'
sudo mysql -u root -e 'GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON wordpress.* TO wordpress@localhost;'
sudo mysql -u root -e 'FLUSH PRIVILEGES;'
#configuring wordpress to connect with mysql
sudo -u www-data cp /srv/www/wordpress/wp-config-sample.php /srv/www/wordpress/wp-config.php
sudo -u www-data sed -i 's/database_name_here/wordpress/' /srv/www/wordpress/wp-config.php
sudo -u www-data sed -i 's/username_here/wordpress/' /srv/www/wordpress/wp-config.php
sudo -u www-data sed -i 's/password_here/yourpassword/' /srv/www/wordpress/wp-config.php
#restart mysql and apache2 to apply changes
systemctl restart mysql
systemctl restart apache2
SHELL
end
This Vagrantfile sets up a virtual machine based on the Ubuntu image and provisions it with the necessary components for running WordPress.
Step 3: Start the Virtual Machine
Save the modified Vagrantfile and run the following command to start the virtual machine:
vagrant up
Vagrant will download the Ubuntu box(imagefile of ubuntu) and execute the provisioning script to install and configure the required software.
Step 4: Access Your WordPress Site
Once the virtual machine is up and running, you can access your WordPress site by opening a web browser and navigating to your public IP address. You can see the ip address of your machine using following command
sudo ip addr show
You should see the WordPress installation page.
Follow the on-screen instructions to complete the WordPress setup, and you're ready to start developing your website locally.
Conclusion
Setting up a WordPress site on a virtual machine using Vagrant provides a convenient and isolated environment for development. The provided Vagrantfile automates the installation process, saving you time and effort. Feel free to customize the Vagrantfile to suit your specific requirements and start building amazing WordPress projects with ease.