使用 Vagrant 设置 WordPress 本地开发环境的原因和方法

The Limitations of LAMP Stacks

Historically, when setting up a local development environment, WordPress developers resorted to traditional LAMP stacks (Linux + Apache + MySQL + PHP) such as MAMP, WAMP or XAMPP. Meanwhile, advances in WordPress development have landed most modern WordPress installations on virtual private servers ("VPS") running Nginx with configurations – that emphasize caching – tailored specifically toward speeding up WordPress. MAMP, WAMP or XAMPP, however, run on Apache; with no optimization for WordPress and with no widely-accepted configuration for caching or any other optimization measures.

Developers agree that it is "extremely important to make sure that your development environment is identical to the production environment, and matches staging and testing servers if you have those too."[#]

By not mirroring your environments, the chance increases that something that seems to work just fine in an Apache-based local development environment will fail to work on the production server. A common frustration with LAMP stacks today is that they, now, seldom match the environment of the live, or production, server. Cutting-edge server setups for WordPress hosting these days include some combination of Nginx, MySQL, PHP-FPM and Memcached (note the 'd'!) – at a minimum.

Why Vagrant?

Even subtle differences between environments increase the likelihood that your code modifications, plugins or web apps will fail in the testing, or staging, environment – resulting in wasted time troubleshooting, when you otherwise could be developing further. Now, consider the possibility that you may want to test your WordPress configurations by throwing into the mix the PECL memcache extension, or Varnish, and you'll happily discover: Why Vagrant. With Vagrant, you can easily recreate the same environment, anywhere, (literally) no matter what operating system or platform you choose for development. You can provision machines locally or with a VPS such as DigitalOcean.

Not Ready to Break up with your Beloved LAMP Stack?

With Vagrant, even those that continue developing with a LAMP stack have discovered that "Vagrant ... seems to solve all of the issues I've had in the past when trying to get a local environment up for WordPress."[#] Lastly, Vagrant is portable – allowing other members of your team to create their development environments from the same configuration you use – ensuring that all your team members are running code through the same environment, against the same dependencies, all configured the same way; regardless of whether any of them are working from Linux, Mac OS X, or Windows workstations.

Prerequisite for Windows users

Vagrant is configured with a shell, or an operating system's command line interface ("CLI"), and OpenSSH. Unfortunately, an SSH client is generally not distributed with Windows, by default. However, Windows users can connect to Vagrant with any of their favorite SSH clients, e.g. one of the shells provided in GitHub for Windows; a terminal in Cygwin; Git Bash or PuTTY, among others.

<a id="starţh2>Getting Started在本地工作站,您需要:

  1. Download & install the latest version of VirtualBox for your operating system.
  2. Download the matching VirtualBox Extension Pack (the same for all platforms) and install it on your computer;
  3. 安装 VirtualBox 应该使扩展包成为可识别的文件类型。

  4. Download & install the latest version of Vagrant for your operating system;
  5. 任何项目的第一步是配置 VagrantfileVagrantfile 的目的是双重: (i) 标记您的项目的根目录(Vagrant 的许多配置与这个根目录有关); 和 (ii) 描述您需要运行项目的机器类型和资源,以及安装哪些软件以及您想要访问它的方式。

  6. Mac OS X & Linux-variant users: Open a shell, e.g. terminal, and create, or navigate to, the directory in which you'd like to save your Vagrantfile (on Ubuntu, this would look like):
    sudo mkdir ~/[name of your choosing]cd [newly-created folder]
    Vagrant has a built-in command for initializing the newly-created directory for usage with Vagrant: vagrant init. NOTE: You can also run vagrant init in a pre-existing directory to setup Vagrant for an existing project.
  7. Next, execute the initializing command:
    vagrant init
  8. Vagrant 应该运行一个 Vagrantfile 每个项目,而 Vagrantfile 应该被承诺为 version control (如 Git) 这样,任何与该项目合作的人都可以从 Vagrant 受益,而无需使用任何前置 legwork.One 概念是 Vagrant 使用 <a href="http://en.wikipedia.org/wiki/Comparison_of_revision_control_software" target="_getting-started/boxes.html" (如 Git)。

  9. Next, execute the following commands:
    vagrant box add precise64 http://files.vagrantup.com/precise64.boxvagrant up
  10. 微软视窗用户: 无论命令行程序如何,接下来的步骤都是一样的. 如果您选择继续使用 Windows 命令提示,您可以通过 (一) 键盘上按下 Windows 键 (二. ),然后按下 R 键,打开相关的 CLI;该键将打开 RUN 对话框. 现在, (三) 类型:

    cmd
    和 (四) 按 Enter. 下一个,执行以下命令(按下Enter 在每行之后 : < pre>cd C:\HashiCorp\Vagrant\binvagrant 框添加精确64 http://files.vagrantup.com/precise64.boxvagrant 页面存档备份,存于互联网档案馆. 输入精确的64个流浪者NOTE: 有时,Windows工作站在试图执行一个最初的Vagrant命令时会吐出一个错误消息. 重启计算机通常能解决大多数安装后的问题. 稍后,如果得到一个大意为"找不到sh可执行文件"的错误消息,并重新启动计算机未自动更新路径,您可以通过执行以下命令(假设sh.exe文件在被引用的文件夹中,下方)来设置 SSH PATH : < pre> set PATH- PATH %; C:\ 方案文件 (x86)\Git\bin </pre/ol >

    在运行上述命令后,您将在 VirtualBox 运行 Ubuntu 12.04 LTS 64 位的完全运行 VPS。 您可以通过 SSH 连接到这个机器,使用命令 vagrant ssh

    使用 PuTTY?

    PuTTY 与 OpenSSH 不兼容,不兼容。 因此,PuTTY 不会识别由 Vagrant 提供的 binsecure_private_key 作为有效的私钥。 使用 PuTTYgen 到 Loadb> (即导入 ecure_private_key 在您的 Home

    • Windows XP: C:\Documents and Settings\{your username}\.vagrant.d\
    • Windows 7: C:\Users\{your username}\.vagrant.d\
    and convert the key file into PuTTY's format (a .ppk file) by clicking on the Save private key button.

    Then, launch PuTTY and enter the following connection information:

     1<tr><td>Session</td>
     2  <td></td>
     3  <td><b>Host Name:</b></td>
     4  <td>127.0.0.1</td></tr>
     5
     6 <tr><td></td>
     7      <td></td>
     8     <td><b>Port:</b></td>
     9      <td>2222</td></tr>
    10
    11     <tr><td></td>
    12          <td></td>
    13         <td><b>Connection type:</b></td>
    14          <td>SSH</td></tr>
    15
    16      <tr><td>Connection</td>
    17      <td>Data</td>
    18      <td><b>Auto-login username:</b></td>
    19      <td>vagrant</td></tr>
    20
    21      <tr><td>Connection/SSH</td>
    22      <td>Auth</td>
    23     <td><b>Private key file for authentication:</b></td>
    24     <td>Click on the <b>Browse</b> button and find the <b>.ppk</b> private key you just converted</td></tr>
    25
    26      <tr><td>Session</td>
    27            <td></td>
    28            <td><b>Saved Sessions</b></td>
    29            <td>vagrant (and then click the <b>Save</b> button for the <b>Load, save or delete a stored session</b> area)</td></tr></table>
    

    Finally, click on the Open button, at the bottom of the PuTTY window, to log in automatically to your Ubuntu VPS;and when you're done playing around, you can remove all traces of it with the command vagrant destroy. The Using a Box and subsequent sections of Getting Started | VagrantDocs are quite good – please refer to that guide for a walk-through of setting up a more complete project, if you would like to continue building on your own, custom Vagrant box. Once you've got your box all set up, you can deploy WordPress, and whatever other software packages you'd like. However, if you're a little timid about 'flying solo' ...

    Not to Fear, the Power of the Open-source Community is Here!

    如果您想在开发WordPress, 本地, 有Vagrant, 在 GitHub(探索Vagrant世界, 以及如何帮助提高开发效率和与生产系统同步; 替换我们所熟悉的通用的MAMP或XAMPP设置, 同时确保所有团队成员都能在相同的环境下为一个项目开发, 而不担心本地机器上的操作系统) 上, 跳出i> Vagrant Vagrants for WordPress Development </i> 。 使用 Variing Vagrants ,您可以通过执行简单的 Vagrant up 命令,自动安装Nginx、PHP-FPM和MySQL,启动一个实例;在开始移动配置文件并导入 SQL 垃圾站 &ndash 之前,您可以在初始命令后几分钟,到浏览器中的现有开发站点,或启动全新的WordPress安装站点。 < sup> [<a title=' 与Vagrant Jeremy Felt" href="http://jeremyfelt.com/code/2013/03/17/evolving-wordpress-developing-development - with-vagrant/"目标="blank""/a> < sup>

    And so, without further ado:

    1. At a minimum, complete Steps 1 - 3 under Getting Started, above:
    2. OS X & Linux-variant users: Open a terminal and create, or navigate to, the directory in which you'd like to save your work (on Ubuntu, this would look like):
      sudo mkdir -p /srv/[name of your choosing]cd /srv/[newly-created folder]
      and clone Varying Vagrant Vagrants from GitHub, by executing the following commands:
      sudo git clone https://github.com/10up/varying-vagrant-vagrants [local folder name of your choosing]
      Windows users: that downloaded & installed GitHub for Windows can click on the button (the default storage directory can be changed under tools => options – in addition to the default shell); or, if you would rather not use GitHub for Windows, you can download a ZIP file of the repository to a local directory, that you'd like to use for your Vagrantfile, and extract it from there.
    3. Now, in a terminal or Windows command prompt, navigate to the new directory:
      cd [new local folder w/repository from Step 2, above]
      and execute the following command:
      vagrant up
    4. 有了它,你的VPS正在运行(你第一次 vagrant up,然而,Vagrant可能需要大约30到60分钟,因为它下载了所有预先包装的软件和相关更新)。

    5. Modify your local machine's hosts file so that local.wordpress.dev is mapped to 192.168.50.4;[#]
    6. 一旦映射,访问浏览器中的local.wordpress.dev将启动Wordpress的初始安装。 继续通过它来创造你的第一个WordPress开发环境 在Vagrant。 您正在开发的主题和插件可以进入相对的www/wordpress-default/wp-content/ 目录.

    从这里开始 你应该做实验 如果您熟悉对象缓存,可以安装用于 PECL memcache 工作的常用插件. 记忆本身已经安装完毕 准备就绪后就开始运行 如果您开始在"https://github.com/10up/varying-vagrants-vagrants/blob/master/README.md"目标="'_blank'>documentation forVarying Vagrant Vagrants,你会看到相当多的地方,你可以与自己定制的接通,继续扩展开发环境. 对于同样为你做了许多前腿工作的替代办法,请检查:<a href='http://www.thisprogrammingthing.com/2013/getting-started- with-vagrant/'target='''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 从流浪者开始为WordPress开发 Mike Green .

    Additional Resources

    As always, if you need help with the steps outlined in this HowTo, look to the DigitalOcean Community for assistance by posing your question(s), below.

    Article Submitted by: Pablo Carranza
    Published At
    Categories with 技术
    comments powered by Disqus
    CategorySub-categoryFieldValue