DEV Community

Cover image for Building a Vagrant Development Environment (CentOS7.3) with Ansible
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Building a Vagrant Development Environment (CentOS7.3) with Ansible

This article was originally published on bmf-tech.com.

Overview

We will set up a development environment on Vagrant's CentOS7.3 using Ansible.

Environment

  • PHP7
  • Ruby
  • Python
  • Nginx
  • MySQL5.7
  • Redis
  • Mailcatcher

Setup

This is a directory structure that somewhat mimics best practices.

ansible/
├── group_vars
│   └── vagrant.yml
├── host
├── roles
│   ├── common
│   │   └── tasks
│   │       ├── add_remi_repo.yml
│   │       ├── install_common.yml
│   │       ├── install_epel_release.yml
│   │       └── main.yml
│   ├── composer
│   │   └── tasks
│   │       ├── install_composer.yml
│   │       └── main.yml
│   ├── mailcatcher
│   │   └── tasks
│   │       ├── install_mailcatcher.yml
│   │       └── main.yml
│   ├── mysql
│   │   └── tasks
│   │       ├── install_mysql.yml
│   │       └── main.yml
│   ├── nginx
│   │   ├── tasks
│   │   │   ├── install_nginx.yml
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── bmf-tech.com.conf
│   │       └── localdev.conf
│   ├── php
│   │   └── tasks
│   │       ├── install_php.yml
│   │       └── main.yml
│   ├── python
│   │   └── tasks
│   │       ├── install_python.yml
│   │       └── main.yml
│   ├── redis
│   │   └── tasks
│   │       ├── install_redis.yml
│   │       └── main.yml
│   └── ruby
│       └── tasks
│           ├── install_ruby.yml
│           └── main.yml
├── site.retry
└── site.yml
Enter fullscreen mode Exit fullscreen mode

Please refer to the contents on github - my-ansible-vagrant.

The Vagrantfile looks like this.

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos7.3"

  config.vm.network "private_network", ip: "192.168.33.10"

  config.vm.synced_folder "/path/to/directory", "/var/www/html",:mount_options => ["dmode=775,fmode=664"]

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "ansible/site.yml"
    ansible.inventory_path = "ansible/host"
    ansible.limit = 'all'
  end

  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.hostname = "localdev"
  config.hostsupdater.aliases = ["localdev"]
end
Enter fullscreen mode Exit fullscreen mode

You can execute provisioning with vagrant provision.

Additional Notes

Issues with php-fpm Configuration?

To use php7 with nginx, it seems necessary to use a CGI called php-fpm, which was quite tricky. If you encounter a 500 error, reviewing these settings might solve the issue.

VAGRANTにてCENTOS7にNGINX+PHP-FPM+PHP7でLARAVELの開発環境構築(前編)

Cannot Access the IP Address Specified in Vagrantfile

Although the setup was completed, I struggled quite a bit because I couldn't access the IP specified in the Vagrantfile. By referring to the following articles, I reviewed the IP settings and adjusted the firewalld settings, which eventually resolved the issue. (It seems the cause was a bug in vagrant1.9.0.)

Impressions

There are quite a few differences in CentOS7 compared to previous OS versions, but I didn't struggle much with those. Instead, I had more trouble with MySQL5.7. It works for now, but I think there's still room for improvement.

References

Top comments (0)