Vagrant搭建LNMP环境

Vagrant搭建LNMP环境

安装Vagrant和Virtualbox. 这个不多说, 安装完成两个软件后, 在一个空闲的硬盘分区中

  1. <span class="pln">mkdir Ubuntu_lnmp</span>
  2. <span class="pln">vagrant init ubuntu</span><span class="pun">/</span><span class="pln">trusty64</span>

打开vagrantfile打开这两个配置

  1. <span class="pln">config</span><span class="pun">.</span><span class="pln">vm</span><span class="pun">.</span><span class="pln">network </span><span class="str">"private_network"</span><span class="pun">,</span><span class="pln"> ip</span><span class="pun">:</span><span class="pun">“</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">10.10</span><span class="str">"</span>
  2. <span class="str">config.vm.provider "</span><span class="pln">virtualbox</span><span class="str">" do |vb|</span>
  3. <span class="str"># Display the VirtualBox GUI when booting the machine</span>
  4. <span class="str"># vb.gui = true</span>
  5. <span class="str"># Customize the amount of memory on the VM:</span>
  6. <span class="str">vb.memory = "</span><span class="lit">1024</span><span class="str">"</span>
  7. <span class="str">end</span>

vagrant up等待安装 vagrant ssh的连接用户名和密码都是vagrant

vagrant环境搞定

  1. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update</span>

安装nginx

  1. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install nginx</span>

安装Mysql请设置密码, 在远程连接时候, 可以使用SSH方式连接, ssh的用户名和密码都是vagrant

  1. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install mysql</span><span class="pun">-</span><span class="pln">server mysql</span><span class="pun">-</span><span class="pln">client libmysqlclient</span><span class="pun">-</span><span class="pln">dev</span>

安装Git版本控制

  1. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> git</span>

安装PHP以及相关模块

  1. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install php5</span><span class="pun">-</span><span class="pln">fpm php5</span><span class="pun">-</span><span class="pln">mysql php5</span><span class="pun">-</span><span class="pln">cli php5</span><span class="pun">-</span><span class="pln">gd php5</span><span class="pun">-</span><span class="pln">memcache php5</span><span class="pun">-</span><span class="pln">memcached php5</span><span class="pun">-</span><span class="pln">json php5</span><span class="pun">-</span><span class="pln">mcrypt php5</span><span class="pun">-</span><span class="pln">curl php</span><span class="pun">-</span><span class="pln">pear build</span><span class="pun">-</span><span class="pln">essential php5</span><span class="pun">-</span><span class="pln">dev </span><span class="pun">-</span><span class="pln">y</span>
  2. <span class="pln">sudo pecl install xdebug </span><span class="pun">-</span><span class="pln">y</span>
  3. <span class="pln">sudo php5enmod json</span>
  4. <span class="pln">sudo php5enmod mcrypt</span>

删除/usr/share/nginx/html

  1. <span class="pln">sudo ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">vagrant </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html</span>

注意: 这里的 /vagrant 目录其实就是你之前创建的 ubuntu_lnmp 目录, 你在本地修改 ubuntu_lnmp 虚拟机中 /vagrant 目录也会同步

修改/etc/php5/fpm/php.ini

  1. <span class="pln">cgi</span><span class="pun">.</span><span class="pln">fix_pathinfo </span><span class="pun">=</span><span class="lit">1</span>
  2. <span class="pln">display_errors </span><span class="pun">=</span><span class="typ">On</span>
  3. <span class="pln">date</span><span class="pun">.</span><span class="pln">timezone </span><span class="pun">=</span><span class="pln"> PRC</span>
  4. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">sites</span><span class="pun">-</span><span class="pln">available</span><span class="pun">/</span><span class="kwd">default</span>

修改

  1. <span class="pln">server </span><span class="pun">{</span>
  2. <span class="pun">...</span>
  3. <span class="com">//找到index</span>
  4. <span class="pln">index index</span><span class="pun">.</span><span class="pln">php index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm</span>
  5. <span class="pun">...</span>
  6. <span class="pln">location </span><span class="pun">~</span><span class="pln"> \.php </span><span class="pun">{</span>
  7. <span class="com"># fastcgi_split_path_info ^(.+\.php)(/.+)$;</span>
  8. <span class="com"># # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini</span>
  9. <span class="com">#</span>
  10. <span class="com"># # With php5-cgi alone:</span>
  11. <span class="pln">fastcgi_pass </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">:</span><span class="lit">9000</span><span class="pun">;</span>
  12. <span class="com"># # With php5-fpm:</span>
  13. <span class="com"># fastcgi_pass unix:/var/run/php5-fpm.sock;</span>
  14. <span class="pln">fastcgi_index index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span>
  15. <span class="pln">include fastcgi_params</span><span class="pun">;</span>
  16. <span class="kwd">set</span><span class="pln"> $path_info </span><span class="str">""</span><span class="pun">;</span>
  17. <span class="kwd">set</span><span class="pln"> $real_script_name $fastcgi_script_name</span><span class="pun">;</span>
  18. <span class="kwd">if</span><span class="pun">(</span><span class="pln">$fastcgi_script_name </span><span class="pun">~</span><span class="str">"^(.+?\.php)(/.+)$"</span><span class="pun">)</span><span class="pun">{</span>
  19. <span class="kwd">set</span><span class="pln"> $real_script_name $1</span><span class="pun">;</span>
  20. <span class="kwd">set</span><span class="pln"> $path_info $2</span><span class="pun">;</span>
  21. <span class="pun">}</span>
  22. <span class="pln">fastcgi_param SCRIPT_FILENAME $document_root$real_script_name</span><span class="pun">;</span>
  23. <span class="pln">fastcgi_param SCRIPT_NAME $real_script_name</span><span class="pun">;</span>
  24. <span class="pln">fastcgi_param PATH_INFO $path_info</span><span class="pun">;</span>
  25. <span class="pun">}</span>
  26. <span class="pun">}</span>

这样是为了支持PHP的PHPINFO. 其实不是很难理解, fastcgi_param 配置项名称会出现在$_SERVER[配置项名称]中

修改nginx的用户名称

  1. <span class="pln">sudo vi </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">.</span><span class="pln">conf</span>

找到user www-data; 改为 user vagrant;

修改配置php-fpm配置

  1. <span class="pln">sudo vi </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php5</span><span class="pun">/</span><span class="pln">fpm</span><span class="pun">/</span><span class="pln">pool</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">www</span><span class="pun">.</span><span class="pln">conf</span>

找到以下配置项目修改

  1. <span class="pln">user </span><span class="pun">=</span><span class="pln"> vagrant</span>
  2. <span class="kwd">group</span><span class="pun">=</span><span class="pln"> vagrant</span>
  3. <span class="pun">;</span><span class="pln">listen </span><span class="pun">=</span><span class="str">/var/</span><span class="pln">run</span><span class="pun">/</span><span class="pln">php5</span><span class="pun">-</span><span class="pln">fpm</span><span class="pun">.</span><span class="pln">sock </span><span class="pun">注释掉</span><span class="pun">改为</span>
  4. <span class="pln">listen </span><span class="pun">=</span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">:</span><span class="lit">9000</span>
  5. <span class="pln">listen</span><span class="pun">.</span><span class="pln">owner </span><span class="pun">=</span><span class="pln"> vagrant</span>
  6. <span class="pln">listen</span><span class="pun">.</span><span class="kwd">group</span><span class="pun">=</span><span class="pln"> vagrant</span>

我的pm.相关配置

  1. <span class="pln">pm</span><span class="pun">.</span><span class="pln">max_children </span><span class="pun">=</span><span class="lit">1000</span>
  2. <span class="pln">pm</span><span class="pun">.</span><span class="pln">start_servers </span><span class="pun">=</span><span class="lit">25</span>
  3. <span class="pln">pm</span><span class="pun">.</span><span class="pln">min_spare_servers </span><span class="pun">=</span><span class="lit">25</span>
  4. <span class="pln">pm</span><span class="pun">.</span><span class="pln">max_spare_servers </span><span class="pun">=</span><span class="lit">50</span>
  5. <span class="pln">pm</span><span class="pun">.</span><span class="pln">max_requests </span><span class="pun">=</span><span class="lit">1000</span>

为了防止cli和fpm的php.ini不相同, 可以讲cli的php.ini文件删除, 然后ln过去一个

  1. <span class="pln">sudo cp </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php5</span><span class="pun">/</span><span class="pln">fpm</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php5</span><span class="pun">/</span><span class="pln">cli</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini</span>

这样修改一个配置文件, 就可以达到两边同步了。

下面关于LNMP(Linux+Nginx+MySQL+PHP)的文章您也可能喜欢,不妨看看:

相关推荐