{"id":12590,"date":"2014-03-31T17:50:17","date_gmt":"2014-03-31T12:20:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12590"},"modified":"2016-12-19T14:53:42","modified_gmt":"2016-12-19T09:23:42","slug":"installing-lamp-stack-through-chef","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/installing-lamp-stack-through-chef\/","title":{"rendered":"Installing LAMP stack through Chef"},"content":{"rendered":"<p>Chef as we all know is a Configuration Management Tool. It enables Infrastructure as Code.<\/p>\n<p>Recently I had to give a <a title=\"Chef DevOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">technical presentation on Chef<\/a> in my current organization which helped me to get some insights into how Chef works &amp; how it eases the deployment process.<\/p>\n<p>In this article I would touch upon very basic entities associated with Chef &amp; how to deploy a complete LAMP stack on Chef-solo.<\/p>\n<p><strong>Chef-solo<\/strong> behaves as a standalone system, it is in itself a client &amp; a server.<\/p>\n<p><em>Knife<\/em> is the most important part of Chef as this is the command line tool for chef all the deployment from Chef server to Chef client happens via knife. In case we want to launch instances in cloud through Chef server we will make use of <strong>knife ec2<\/strong> for AWS instances.<\/p>\n<p>Without wasting any more time let\u2019s move further with the installation steps. For this example I would be Ubuntu 12.04 as the OS<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Installing Chef<\/span><\/strong><\/p>\n<p>We can download <strong>chef-solo<\/strong> from github.<\/p>\n<p>[shell]root@ankush# curl -L https:\/\/www.opscode.com\/chef\/install.sh | bash<\/p>\n<p>root@ankush# chef-solo -v<\/p>\n<p>root@ankush# wget http:\/\/github.com\/opscode\/chef-repo\/tarball\/master<\/p>\n<p>root@ankush# tar -zxf master<\/p>\n<p>root@ankush# mv opscode-chef-repo* chef-repo<\/p>\n<p>root@ankush# rm master<\/p>\n<p>root@ankush# cd chef-repo<\/p>\n<p>root@ankush# ls[\/shell]<\/p>\n<p>Now we would specify the cookbook path under .chef directory in knife.rb file.<\/p>\n<p>[shell]root@ankush:~\/chef-repo# mkdir .chef<\/p>\n<p>root@ankush:~\/chef-repo# echo &amp;quot;cookbook_path [ &#8216;\/root\/chef-repo\/cookbooks&#8217; ]&amp;quot; &amp;gt; .chef\/knife.rb[\/shell]<\/p>\n<p>Create any cookbook via knife e.g phpapp<\/p>\n<p>[shell]root@ankush:~\/chef-repo# knife cookbook create phpapp<\/p>\n<p>root@ankush:~\/chef-repo# cd cookbooks\/phpapp<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks\/phpapp# ls<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks\/phpapp# cd ..[\/shell]<\/p>\n<p><strong>Installing Apache<\/strong><\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download apache2<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf apache2*<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm apache2*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download apt<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf apt*<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm apt*.tar.gzbelow line<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download iptables<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf iptables*<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm iptables*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download logrotate<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf logrotate*<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm logrotate*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download pacman<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf pacman*<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm pacman*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# cd phpapp[\/shell]<\/p>\n<p>Open metadata.rb , add below line<\/p>\n<p><em>depends &#8220;apache2&#8221;<\/em><\/p>\n<p>Open recipes\/default.rb , add below lines<\/p>\n<p><em>include_recipe &#8220;apache2&#8221;<\/em><\/p>\n<p><em>\u00a0apache_site &#8220;default&#8221; do<\/em><\/p>\n<p><em>\u00a0 enable true<\/em><\/p>\n<p><em>end<\/em><\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks\/phpapp# cd ..\/..[\/shell]<\/p>\n<p>Create a new file called solo.rb in your text editor &amp; make below entries<\/p>\n<p><em>file_cache_path &#8220;\/root\/chef-solo&#8221;<\/em><\/p>\n<p><em>cookbook_path &#8220;\/root\/chef-repo\/cookbooks&#8221;<\/em><\/p>\n<p>This file configures chef-solo, telling it where to keep its cache of files and where our cookbooks are.<\/p>\n<p>Now create a file called web.json &amp; add below lines<\/p>\n<p><em>{<\/em><\/p>\n<p><em>&#8220;run_list&#8221;: [ &#8220;recipe[apt]&#8221;, &#8220;recipe[phpapp]&#8221; ]<\/em><\/p>\n<p><em>}<\/em><\/p>\n<p>Why have we not included the apt cookbook inside our recipe as we did with the apache2 cookbook? It&#8217;s because our PHP application requires Apache to function but we don&#8217;t necessarily want to tie our cookbook to platforms that only support apt.<\/p>\n<p>[shell]root@ankush#chef-solo -c solo.rb -j web.json[\/shell]<\/p>\n<p>Now open the page in browser it will show default page for apache.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Installing mysql<\/span><\/strong><\/p>\n<p>[shell]root@ankush:~\/chef-repo# cd cookbooks<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download mysql<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf mysql*<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm mysql-*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# cd mysql\/recipes\/<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks\/mysql\/recipes# ls<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks\/mysql\/recipes# cd ..\/..\/phpapp[\/shell]<\/p>\n<p>Open metadata.rb, add below lines<\/p>\n<p><em>depends &#8220;mysql&#8221;<\/em><\/p>\n<p>Open recipes\/default.rb , add below lines<\/p>\n<p><em>include_recipe &#8220;apache2&#8221;<\/em><\/p>\n<p><span style=\"color: #008000;\"><em>include_recipe &#8220;mysql::client&#8221;<\/em><\/span> &#8212;- <strong>This to be added<\/strong><\/p>\n<p><span style=\"color: #008000;\"><em>include_recipe &#8220;mysql::server&#8221;<\/em><\/span>\u00a0\u00a0 &#8212;- <strong>This to be added<\/strong><\/p>\n<p><em>apache_site &#8220;default&#8221; do<\/em><\/p>\n<p><em>\u00a0 enable true<\/em><\/p>\n<p><em>end<\/em><\/p>\n<p>Run chef-solo again<\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks\/phpapp# cd ..\/..<\/p>\n<p>root@ankush:~\/chef-repo# chef-solo -c solo.rb -j web.json[\/shell]<\/p>\n<p>You will now get errors like<\/p>\n<p><strong><em>FATAL: Stacktrace dumped to \/root\/chef-solo\/chef-stacktrace.out<\/em><\/strong><\/p>\n<p><strong><em>FATAL: Chef::Exceptions::CookbookNotFound: Cookbook build-essential not found. If you&#8217;re loading build-essential from another cookbook, make sure you configure the dependency in your metadata<\/em><\/strong><\/p>\n<p>Open cookbooks\/mysql\/metadata.rb &amp; specify the dependencies<\/p>\n<p><em>depends &#8220;openssl&#8221;<\/em><\/p>\n<p><em>depends &#8220;build-essential&#8221;<\/em><\/p>\n<p>[shell]root@ankush:~\/chef-repo# cd cookbooks<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download openssl<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf openssl*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm openssl*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download build-essential<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf build-essential-*.tar.gz&amp;gt;<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm build-essential-*.tar.gz[\/shell]<\/p>\n<p>Now we&#8217;ve fulfilled those dependencies let&#8217;s try and re-run chef-solo!<\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks# cd ..<\/p>\n<p>root@ankush:~\/chef-repo# chef-solo -c solo.rb -j web.json[\/shell]<\/p>\n<p>Again we have an <strong>ERROR<\/strong><\/p>\n<p><strong><em>FATAL: You must set node[&#8216;mysql&#8217;][&#8216;server_debian_password&#8217;], node[&#8216;mysql&#8217;][&#8216;server_root_password&#8217;], node[&#8216;mysql&#8217;][&#8216;server_repl_password&#8217;] in chef-solo mode. For more information, see https:\/\/github.com\/opscode-cookbooks\/mysql#chef-solo-note<\/em><\/strong><\/p>\n<p>Open web.json to specify the mysql root password<\/p>\n<p>{<\/p>\n<p><span style=\"color: #008000;\"><em>&#8220;mysql&#8221;: {&#8220;server_root_password&#8221;: &#8220;ankush&#8221;, &#8220;server_debian_password&#8221;: &#8220;ankush&#8221;, &#8220;server_repl_password&#8221;: &#8220;ankush&#8221;}<\/em>,<\/span><\/p>\n<p><em>&#8220;run_list&#8221;: [ &#8220;recipe[apt]&#8221;, &#8220;recipe[phpapp]&#8221; ]<\/em><\/p>\n<p>}<\/p>\n<p>[shell]root@ankush:~\/chef-repo# chef-solo -c solo.rb -j web.json[\/shell]<\/p>\n<p>Now to <strong>install PHP<\/strong><\/p>\n<p>[shell]root@ankush:~\/chef-repo# cd cookbooks\/<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download php<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf php*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm php*.tar.gz[\/shell]<\/p>\n<p>The php cookbook depends on the xml, yum-epel, windows, and iis cookbooks, so we&#8217;ll need those even though we won&#8217;t be using all of them. We&#8217;ll also have to install sub-dependencies yum (a dependency of yum-epel), chef_handler, and powershell (dependencies of windows).<\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download xml<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf xml-*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download yum<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf yum-*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download yum-epel<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf yum-epel-*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download powershell<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf powershell-*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# knife cookbook site download iis<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# tar zxf iis-*.tar.gz<\/p>\n<p>root@ankush:~\/chef-repo\/cookbooks# rm *.tar.gz[\/shell]<\/p>\n<p>Let&#8217;s use the php cookbook in our cookbook.<\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks# cd phpapp[\/shell]<\/p>\n<p>Open metadata.rb add below line<\/p>\n<p><em>depends &#8220;php&#8221;<\/em><\/p>\n<p>Open recipes\/default.rb , add below lines<\/p>\n<p><em>include_recipe &#8220;php&#8221;<\/em><\/p>\n<p><em>include_recipe &#8220;php::module_mysql&#8221;<\/em><\/p>\n<p><em>include_recipe &#8220;apache2::mod_php5&#8221;<\/em><\/p>\n<p>Save the file and we&#8217;re good to run chef-solo again to install all of those things<\/p>\n<p>[shell]root@ankush:~\/chef-repo\/cookbooks\/phpapp# cd ..\/..<\/p>\n<p>root@ankush:~\/chef-repo# chef-solo -c solo.rb -j web.json[\/shell]<\/p>\n<p>So that&#8217;s PHP installed. Let&#8217;s confirm that by creating a test page. Open <strong><em>\/var\/www\/test.php<\/em><\/strong> in your editor.<\/p>\n<p><strong><em>&lt;?php phpinfo(); ?&gt;<\/em><\/strong><\/p>\n<p>Now goto http:\/\/yourserver\/test.php , you will see the phpinfo page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chef as we all know is a Configuration Management Tool. It enables Infrastructure as Code. Recently I had to give a technical presentation on Chef in my current organization which helped me to get some insights into how Chef works &amp; how it eases the deployment process. In this article I would touch upon very [&hellip;]<\/p>\n","protected":false},"author":91,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[2391,2390],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12590"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/91"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12590"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12590\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}