Setup nginx pada Vagrant

Standard
$ mkdir vagrant ; cd vagrant

$ vagrant box add hashicorp/precise32
vim VagrantFile

Add the following:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.network :forwarded_port, host: 5000, guest: 80
end

Vagrant up

vagrant up
vagrant ssh
# Exit the guest box if you are logged in
$ exit

# Inside the vagrant directory on the host system
$ mkdir -p puppet/{manifests,modules}

# We'll also make a directory for our nginx-served files
$ mkdir app

Now we’ll create our base Puppet ‘manifest’:

$ vim puppet/manifests/init.pp

Inside we’ll put:

exec { 'apt-get update':
  path => '/usr/bin',
}

package { 'vim':
  ensure => present,
}

file { '/var/www/':
  ensure => 'directory',
}

We now need to instruct Vagrant to provision Puppet at install. We do this by adding the following to ourVagrantFile:

config.vm.provision :puppet do |puppet|
    puppet.manifests_path = 'puppet/manifests'
    puppet.module_path = 'puppet/modules'
    puppet.manifest_file = 'init.pp'
  end
Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.network :forwarded_port, host: 5000, guest: 80

  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = 'puppet/manifests'
    puppet.module_path = 'puppet/modules'
    puppet.manifest_file = 'init.pp'
  end
end
vagrant reload --provision
notice: /Stage[main]//Exec[apt-get update]/returns: executed successfully
notice: /Stage[main]//File[/var/www/]/ensure: created
notice: /Stage[main]//Package[vim]/ensure: ensure changed 'purged' to 'present'
notice: Finished catalog run in 15.62 seconds
$ cd puppet/modules
$ mkdir -p nginx/{files,manifests}
$ mkdir -p php/{files,manifests}
$ mkdir -p mysql/{files,manifests}
include nginx, php, mysql
# In puppet/modules
vim nginx/manifests/init.pp

We’ll define our nginx class like so:

# vagrant/puppet/modules/nginx/manifests/init.pp
class nginx {

  # Symlink /var/www/app on our guest with 
  # host /path/to/vagrant/app on our system
  file { '/var/www/app':
    ensure  => 'link',
    target  => '/vagrant/app',
  }

  # Install the nginx package. This relies on apt-get update
  package { 'nginx':
    ensure => 'present',
    require => Exec['apt-get update'],
  }

  # Make sure that the nginx service is running
  service { 'nginx':
    ensure => running,
    require => Package['nginx'],
  }

  # Add a vhost template
  file { 'vagrant-nginx':
    path => '/etc/nginx/sites-available/127.0.0.1',
    ensure => file,
    require => Package['nginx'],
      source => 'puppet:///modules/nginx/127.0.0.1',
  }

  # Disable the default nginx vhost
  file { 'default-nginx-disable':
    path => '/etc/nginx/sites-enabled/default',
    ensure => absent,
    require => Package['nginx'],
  }

  # Symlink our vhost in sites-enabled to enable it
  file { 'vagrant-nginx-enable':
    path => '/etc/nginx/sites-enabled/127.0.0.1',
    target => '/etc/nginx/sites-available/127.0.0.1',
    ensure => link,
    notify => Service['nginx'],
    require => [
      File['vagrant-nginx'],
      File['default-nginx-disable'],
    ],
  }
}
# vagrant/puppet/modules/nginx/files/127.0.0.1
server {
  listen 80;
  server_name _;
  root /var/www/app;
  index index.php;

  location / {
    try_files $uri /index.php;
  }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}
# vagrant/puppet/modules/php/manifests/init.pp
class php {

  # Install the php5-fpm and php5-cli packages
  package { ['php5-fpm',
             'php5-cli']:
    ensure => present,
    require => Exec['apt-get update'],
  }

  # Make sure php5-fpm is running
  service { 'php5-fpm':
    ensure => running,
    require => Package['php5-fpm'],
  }
}
# vagrant/puppet/modules/mysql/manifests/init.pp
class mysql {

  # Install mysql
  package { ['mysql-server']:
    ensure => present,
    require => Exec['apt-get update'],
  }

  # Run mysql
  service { 'mysql':
    ensure  => running,
    require => Package['mysql-server'],
  }

  # Use a custom mysql configuration file
  file { '/etc/mysql/my.cnf':
    source  => 'puppet:///modules/mysql/my.cnf',
    require => Package['mysql-server'],
    notify  => Service['mysql'],
  }

  # We set the root password here
  exec { 'set-mysql-password':
    unless  => 'mysqladmin -uroot -proot status',
    command => "mysqladmin -uroot password a9120ed2b58af37862a83f5b9f850819ed08b2a9",
    path    => ['/bin', '/usr/bin'],
    require => Service['mysql'];
  }
}
 vagrant reload --provision
$ git clone git@github.com:/foobar/my_dev_environment
$ cd my_dev_environment
$ git clone git@github.com:/foobar/my_application app

$ vagrant up
$ echo "<?php phpinfo();" > app/index.php
http://127.0.0.1:5000

Leave a Reply