top of page

Vagrant - Automate Website setup (Infrastructure as a code)

In the previous article, I demonstrate the manual deployment of a website and WordPress blog on Linux machines. In this article, we will use the concept of "infrastructure as a code (IaC)" and automate the entire process using vagrant provisioning.

Infrastructure as code (IaC) is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. -Wiki

Automate Website setup

Let's start with the basic set-up of the Linux machine (Creating vagrant file, Directories and initial configurations):

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms
$ mkdir IAAC

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms
$ cd iaac

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac
$ mkdir website

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac
$ mkdir wordpress

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac
$ cd website

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac/website
$ vagrant init geerlingguy/centos7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.



Edit the vagrant file and set networking:

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
   config.vm.network "private_network", ip: "192.168.33.17"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
   config.vm.network "public_network"

Enable provisioning

Enable provisioning and add the provisioning code to the vagrant file:


Code:

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
   config.vm.provision "shell", inline: <<-SHELL
     yum install httpd wget unzip -y
	 systemctl start httpd
	 systemctl enable httpd
	 cd /tmp/
	 wget https://www.tooplate.com/zip-templates/2119_gymso_fitness.zip
	 unzip -o 2119_gymso_fitness.zip
	 cp -r 2119_gymso_fitness/* /var/www/html/
	 systemctl restart httpd
   SHELL
end

The final step is to create the machine (vagrant up) and access the site using the IP address we set at the beginning (192.168.33.17):



Automate WordPress Setup

In this section, we will set up a WordPress blog by using vagrant provisioning without


Navigate to the WordPress folder and create a new ubuntu machine:

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac
$ ls
website/  wordpress/

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac
$ cd wordpress

David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/iaac/wordpress
$ vagrant init geerlingguy/ubuntu2004

Edit the vagrant file and set networking:

# Create a private network, which allows host-only access to the machine
  # using a specific IP.
   config.vm.network "private_network", ip: "192.168.33.44"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
   config.vm.network "public_network"

Enable provisioning

Enable provisioning and add the provisioning code to the vagrant file:

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
   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
   cp /vagrant/wordpress.conf /etc/apache2/sites-available/wordpress.conf

   sudo a2ensite wordpress
   sudo a2enmod rewrite
   sudo a2dissite 000-default
   sudo service apache2 reload

   mysql -u root -e 'CREATE DATABASE wordpress;'
   mysql -u root -e 'CREATE USER wordpress@localhost IDENTIFIED BY "admin123";'
   mysql -u root -e 'GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON wordpress.* TO wordpress@localhost IDENTIFIED BY "admin123";'
   mysql -u root -e 'FLUSH PRIVILEGES;'
   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/admin123/' /srv/www/wordpress/wp-config.php
   service mysql restart


Prior we start the new VM, i want to focus on the following line of code: cp /vagrant/wordpress.conf /etc/apache2/sites-available/wordpress.conf

We can't copy this content as requested in the WordPress setup guide by default, so we'll have to make a new file in the same directory as the vagrant file and copy its content during setup:

Now just access the VM IP and start setting your new WordPress blog :)






42 views0 comments