|
| 1 | +.. index:: |
| 2 | + single: Workflow; Vagrant |
| 3 | + |
| 4 | +Symfony Standard Edition with Vagrant |
| 5 | +===================================== |
| 6 | + |
| 7 | +You can easily setup a development environment for the Symfony Standard Edition |
| 8 | +by using Vagrant. Vagrant is a tool that can create a virtual machine (based on |
| 9 | +a set of configuration files) with simple commands. In this case, you will be |
| 10 | +creating a virtual machine that runs Linux and has an Apache web server, a |
| 11 | +MySQL database server, and PHP (a LAMP stack). When you are finished creating |
| 12 | +this Vagrant configuration in your project, you can store the files in your |
| 13 | +version control system (git, svn, ...) and work with other developers without |
| 14 | +having to help them configure their development machines! This configuration |
| 15 | +is also a great way to try out the Symfony Standard Edition without having to |
| 16 | +know how to configure a web server or database server. |
| 17 | + |
| 18 | +Prerequisites |
| 19 | +------------- |
| 20 | + |
| 21 | +1. Download and install the latest `Virtualbox`_. |
| 22 | + |
| 23 | +2. Download and install the latest `Vagrant`_. |
| 24 | + |
| 25 | +3. Install the Symfony Standard Edition as detailed in :doc:`/cookbook/workflow/new_project_git`. |
| 26 | + |
| 27 | +Setup |
| 28 | +----- |
| 29 | + |
| 30 | +You will be creating a set of files under a new ``vagrant`` directory: |
| 31 | + |
| 32 | +.. code-block:: text |
| 33 | +
|
| 34 | + vagrant/.gitignore |
| 35 | + vagrant/Vagrantfile |
| 36 | + vagrant/puppet/modules.sh |
| 37 | + vagrant/puppet/manifests/symfony.pp |
| 38 | +
|
| 39 | +1. Create a new set of directories at the root of your project to store the |
| 40 | + Vagrant configuration files: |
| 41 | + |
| 42 | + .. code-block:: bash |
| 43 | +
|
| 44 | + $ mkdir vagrant |
| 45 | + $ mkdir vagrant/puppet |
| 46 | + $ mkdir vagrant/puppet/manifests |
| 47 | +
|
| 48 | +2. Create a new file ``vagrant/Vagrantfile`` and paste the following into it. |
| 49 | + |
| 50 | + .. code-block:: text |
| 51 | +
|
| 52 | + # -*- mode: ruby -*- |
| 53 | + # vi: set ft=ruby : |
| 54 | +
|
| 55 | + Vagrant.configure("2") do |config| |
| 56 | + config.vm.box = "precise32" |
| 57 | +
|
| 58 | + config.vm.box_url = "http://files.vagrantup.com/precise32.box" |
| 59 | +
|
| 60 | + config.vm.network :private_network, ip: "192.168.33.10" |
| 61 | +
|
| 62 | + config.vm.synced_folder "..", "/vagrant" #, :nfs => true |
| 63 | +
|
| 64 | + config.vm.provision :shell, :path => "puppet/modules.sh" |
| 65 | +
|
| 66 | + config.vm.provision :puppet do |puppet| |
| 67 | + puppet.manifests_path = "puppet/manifests" |
| 68 | + puppet.manifest_file = "symfony.pp" |
| 69 | + puppet.facter = { |
| 70 | + "fqdn" => "symfony.local", |
| 71 | + "host_ipaddress" => "192.168.33.1", |
| 72 | + } |
| 73 | + end |
| 74 | + end |
| 75 | +
|
| 76 | +3. Create a new file ``vagrant/puppet/modules.sh`` and paste the following |
| 77 | + into it. |
| 78 | + |
| 79 | + .. code-block:: text |
| 80 | +
|
| 81 | + #!/bin/sh |
| 82 | +
|
| 83 | + if [ ! -d "/etc/puppet/modules" ]; then |
| 84 | + mkdir -p /etc/puppet/modules; |
| 85 | + fi |
| 86 | +
|
| 87 | + if [ ! -d "/etc/puppet/modules/apache" ]; then |
| 88 | + puppet module install puppetlabs-apache; |
| 89 | + fi |
| 90 | +
|
| 91 | + if [ ! -d "/etc/puppet/modules/mysql" ]; then |
| 92 | + puppet module install puppetlabs-mysql; |
| 93 | + fi |
| 94 | +
|
| 95 | + if [ ! -d "/etc/puppet/modules/apt" ]; then |
| 96 | + puppet module install puppetlabs-apt; |
| 97 | + fi |
| 98 | +
|
| 99 | + if [ ! -d "/etc/puppet/modules/git" ]; then |
| 100 | + puppet module install puppetlabs-git; |
| 101 | + fi |
| 102 | +
|
| 103 | +4. Create a new file ``vagrant/puppet/manifests/symfony.pp`` and paste the |
| 104 | + following into it. |
| 105 | + |
| 106 | + .. code-block:: text |
| 107 | +
|
| 108 | + # update system first before new packages are installed |
| 109 | + class { 'apt': |
| 110 | + always_apt_update => true, |
| 111 | + } |
| 112 | + Exec['apt_update'] -> Package <| |> |
| 113 | +
|
| 114 | +
|
| 115 | + # install Apache |
| 116 | + class { 'apache': } |
| 117 | + class { 'apache::mod::php': } |
| 118 | +
|
| 119 | +
|
| 120 | + # install MySQL |
| 121 | + class { 'mysql': } |
| 122 | + class { 'mysql::server': |
| 123 | + config_hash => { 'root_password' => 'symfony' }, |
| 124 | + } |
| 125 | + class { 'mysql::php': } |
| 126 | +
|
| 127 | +
|
| 128 | + # install Git for composer |
| 129 | + class { 'git': } |
| 130 | +
|
| 131 | +
|
| 132 | + # install PHP Extensions used with Symfony |
| 133 | + class php-extensions { |
| 134 | + package { ['php-apc', 'php5-intl', 'php5-xdebug']: |
| 135 | + ensure => latest, |
| 136 | + require => Package['httpd'], |
| 137 | + notify => Service['httpd'], |
| 138 | + } |
| 139 | + } |
| 140 | +
|
| 141 | + include php-extensions |
| 142 | +
|
| 143 | +
|
| 144 | + # install a local composer.phar file |
| 145 | + class composer { |
| 146 | + exec { 'composerPhar': |
| 147 | + cwd => '/vagrant', |
| 148 | + command => 'curl -s http://getcomposer.org/installer | php', |
| 149 | + path => ['/bin', '/usr/bin'], |
| 150 | + creates => '/vagrant/composer.phar', |
| 151 | + require => [ Class['apache::mod::php', 'git'], Package['curl'] ], |
| 152 | + } |
| 153 | +
|
| 154 | + package { 'curl': |
| 155 | + ensure => present, |
| 156 | + } |
| 157 | + } |
| 158 | +
|
| 159 | + include composer |
| 160 | +
|
| 161 | +
|
| 162 | + # install the Symfony vendors using composer |
| 163 | + class symfony { |
| 164 | + exec { 'vendorsInstall': |
| 165 | + cwd => '/vagrant', |
| 166 | + command => 'php composer.phar install', |
| 167 | + timeout => 1200, |
| 168 | + path => ['/bin', '/usr/bin'], |
| 169 | + creates => '/vagrant/vendor', |
| 170 | + logoutput => true, |
| 171 | + require => Exec['composerPhar'], |
| 172 | + } |
| 173 | + } |
| 174 | +
|
| 175 | + include symfony |
| 176 | +
|
| 177 | +
|
| 178 | + # Create a web server host using the Symfony web/ directory |
| 179 | + apache::vhost { 'www.symfony.local': |
| 180 | + priority => '10', |
| 181 | + port => '80', |
| 182 | + docroot_owner => 'vagrant', |
| 183 | + docroot_group => 'vagrant', |
| 184 | + docroot => '/vagrant/web/', |
| 185 | + logroot => '/vagrant/app/logs/', |
| 186 | + serveraliases => ['symfony.local',], |
| 187 | + } |
| 188 | +
|
| 189 | + # Create a database for Symfony |
| 190 | + mysql::db { 'symfony': |
| 191 | + user => 'symfony', |
| 192 | + password => 'symfony', |
| 193 | + host => 'localhost', |
| 194 | + grant => ['all'], |
| 195 | + } |
| 196 | +
|
| 197 | +
|
| 198 | + # Configure Apache files to run as the "vagrant" user so that Symfony |
| 199 | + # app/cache and app/logs files can be successfully created and accessed |
| 200 | + # by the web server |
| 201 | +
|
| 202 | + file_line { 'apache_user': |
| 203 | + path => '/etc/apache2/httpd.conf', |
| 204 | + line => 'User vagrant', |
| 205 | + require => Package['httpd'], |
| 206 | + notify => Service['httpd'], |
| 207 | + } |
| 208 | +
|
| 209 | + file_line { 'apache_group': |
| 210 | + path => '/etc/apache2/httpd.conf', |
| 211 | + line => 'Group vagrant', |
| 212 | + require => Package['httpd'], |
| 213 | + notify => Service['httpd'], |
| 214 | + } |
| 215 | +
|
| 216 | +
|
| 217 | + # Configure php.ini to follow recommended Symfony web/config.php settings |
| 218 | +
|
| 219 | + file_line { 'php5_apache2_short_open_tag': |
| 220 | + path => '/etc/php5/apache2/php.ini', |
| 221 | + match => 'short_open_tag =', |
| 222 | + line => 'short_open_tag = Off', |
| 223 | + require => Class['apache::mod::php'], |
| 224 | + notify => Service['httpd'], |
| 225 | + } |
| 226 | +
|
| 227 | + file_line { 'php5_cli_short_open_tag': |
| 228 | + path => '/etc/php5/cli/php.ini', |
| 229 | + match => 'short_open_tag =', |
| 230 | + line => 'short_open_tag = Off', |
| 231 | + require => Class['apache::mod::php'], |
| 232 | + notify => Service['httpd'], |
| 233 | + } |
| 234 | +
|
| 235 | + file_line { 'php5_apache2_date_timezone': |
| 236 | + path => '/etc/php5/apache2/php.ini', |
| 237 | + match => 'date.timezone =', |
| 238 | + line => 'date.timezone = UTC', |
| 239 | + require => Class['apache::mod::php'], |
| 240 | + notify => Service['httpd'], |
| 241 | + } |
| 242 | +
|
| 243 | + file_line { 'php5_cli_date_timezone': |
| 244 | + path => '/etc/php5/cli/php.ini', |
| 245 | + match => 'date.timezone =', |
| 246 | + line => 'date.timezone = UTC', |
| 247 | + require => Class['apache::mod::php'], |
| 248 | + notify => Service['httpd'], |
| 249 | + } |
| 250 | +
|
| 251 | + file_line { 'php5_apache2_xdebug_max_nesting_level': |
| 252 | + path => '/etc/php5/apache2/conf.d/xdebug.ini', |
| 253 | + line => 'xdebug.max_nesting_level = 250', |
| 254 | + require => [ Class['apache::mod::php'], Package['php5-xdebug'] ], |
| 255 | + notify => Service['httpd'], |
| 256 | + } |
| 257 | +
|
| 258 | + file_line { 'php5_cli_xdebug_max_nesting_level': |
| 259 | + path => '/etc/php5/cli/conf.d/xdebug.ini', |
| 260 | + line => 'xdebug.max_nesting_level = 250', |
| 261 | + require => [ Class['apache::mod::php'], Package['php5-xdebug'] ], |
| 262 | + notify => Service['httpd'], |
| 263 | + } |
| 264 | +
|
| 265 | +
|
| 266 | + # Configure Symfony dev controllers so that the Vagrant host machine |
| 267 | + # at the host_ipaddress (specified in the Vagrantfile) has access |
| 268 | +
|
| 269 | + file_line { 'symfony_web_config_host_ipaddress': |
| 270 | + path => '/vagrant/web/config.php', |
| 271 | + match => '::1', |
| 272 | + line => " '::1', '${::host_ipaddress}',", |
| 273 | + } |
| 274 | +
|
| 275 | + file_line { 'symfony_web_app_dev_host_ipaddress': |
| 276 | + path => '/vagrant/web/app_dev.php', |
| 277 | + match => '::1', |
| 278 | + line => " || !in_array(@\$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1', '${::host_ipaddress}'))", |
| 279 | + } |
| 280 | +
|
| 281 | +5. Create a new file ``vagrant/.gitignore`` and paste the following into it. |
| 282 | + |
| 283 | + .. code-block:: text |
| 284 | +
|
| 285 | + .vagrant |
| 286 | +
|
| 287 | +6. Switch to the vagrant directory. |
| 288 | + |
| 289 | + .. code-block:: bash |
| 290 | +
|
| 291 | + $ cd vagrant |
| 292 | +
|
| 293 | +7. Create the development virtual machine. |
| 294 | + |
| 295 | + .. code-block:: bash |
| 296 | +
|
| 297 | + $ vagrant up |
| 298 | +
|
| 299 | +A virtual machine is now being prepared in Virtualbox by Vagrant. This process |
| 300 | +will take several minutes to complete on the initial run, so be patient. When |
| 301 | +the process has completed, you can view the Symfony demo site in a browser at: |
| 302 | + |
| 303 | +http://192.168.33.10/app_dev.php |
| 304 | + |
| 305 | +Now you can start developing with Symfony! Any changes made to your Symfony |
| 306 | +project directory will appear in the virtual machine. |
| 307 | + |
| 308 | +Further Configuration |
| 309 | +--------------------- |
| 310 | + |
| 311 | +A MySQL database has been created on the Vagrant virtual machine which you can |
| 312 | +use. Just update your app/config/parameters.yml file: |
| 313 | + |
| 314 | +.. code-block:: yaml |
| 315 | +
|
| 316 | + parameters: |
| 317 | + database_driver: pdo_mysql |
| 318 | + database_host: 127.0.0.1 |
| 319 | + database_port: ~ |
| 320 | + database_name: symfony |
| 321 | + database_user: symfony |
| 322 | + database_password: symfony |
| 323 | +
|
| 324 | +The database name, user, and password are "symfony". |
| 325 | + |
| 326 | +Other Vagrant Commands |
| 327 | +---------------------- |
| 328 | + |
| 329 | +While you are in the ``vagrant`` directory, you can perform other commands. |
| 330 | + |
| 331 | +If you came across an issue during the initial setup, execute: |
| 332 | + |
| 333 | +.. code-block:: bash |
| 334 | +
|
| 335 | + $ vagrant provision |
| 336 | +
|
| 337 | +This will execute the ``vagrant/puppet/modules.sh`` and |
| 338 | +``vagrant/puppet/manifests/symfony.pp`` scripts again. |
| 339 | + |
| 340 | +If you need to access the virtual machine command line, execute: |
| 341 | + |
| 342 | +.. code-block:: bash |
| 343 | +
|
| 344 | + $ vagrant ssh |
| 345 | +
|
| 346 | +While in the virtual machine command line, you can access your project code in |
| 347 | +its ``/vagrant`` directory. This is useful when you want to update composer |
| 348 | +dependencies for instance: |
| 349 | + |
| 350 | +.. code-block:: bash |
| 351 | +
|
| 352 | + $ vagrant ssh |
| 353 | + $ cd /vagrant |
| 354 | + $ php composer.phar update |
| 355 | + $ exit |
| 356 | +
|
| 357 | +If you need to refresh the virtual machine, execute: |
| 358 | + |
| 359 | +.. code-block:: bash |
| 360 | +
|
| 361 | + $ vagrant reload |
| 362 | +
|
| 363 | +If you are done developing and want to remove the virtual machine, execute: |
| 364 | + |
| 365 | +.. code-block:: bash |
| 366 | +
|
| 367 | + $ vagrant destroy |
| 368 | +
|
| 369 | +And if you want to install again after destroying, execute: |
| 370 | + |
| 371 | +.. code-block:: bash |
| 372 | +
|
| 373 | + $ vagrant up |
| 374 | +
|
| 375 | +Hopefully your new Vagrant configuration will help you develop your Symfony |
| 376 | +project without having to worry about your local server setup or the setup of |
| 377 | +another developer's machine. |
| 378 | + |
| 379 | +.. _`Virtualbox`: https://www.virtualbox.org/wiki/Downloads |
| 380 | +.. _`Vagrant`: http://downloads.vagrantup.com/ |
0 commit comments