Hello. This is Komiya from the platform.
Continuing from last time, we will continue to make recipes for existing procedures. I think there will be about three more times, so please be patient.
This time it will be a recipe for a web server.
I don't think it's a normal web server without doing anything special, but if I write down what I'm doing,
I use APC for the intermediate code cache on the apache 2.2 + php5.3 system, I use wordpress and its ssh plugin, and I am mounted on AWS S3.
The fuse used for s3mount needs to be entered as source code from the latest version, and there are about 18 php packages with fixed versions.
It was troublesome to write package resources one by one, so I used script.
When using script, be careful because if you forget not_if it will be difficult to execute make many times.
Also, if I forgot to write the CD, the deployment location was recognized and it didn't appear, so I got an error that the file did not exist in the subsequent steps.
・Write httpd recipes
Prepare the configuration file
[shell]# mkdir -p /root/chef-repo/site-cookbooks/httpd/files/default/rpms # tar cf /root/chef-repo/site-cookbooks/httpd/files/default/rpms/web-rpm.tar ./web-rpm/ # tar tf /root/chef-repo/site-cookbooks/httpd/files/default/rpms/web-rpm.tar ./web-rpm/ # cd /root/chef-repo/site-cookbooks/httpd/recipes # mkdir -p /root/chef-repo/site-cookbooks/ httpd/files/default/etc/httpd/{conf,conf.d} # cp -p /opt/src/config/web/httpd.conf /root/chef-repo/site-cookbooks/httpd/files/default/etc/httpd/conf/ # vi /root/chef-repo/site-cookbooks/httpd/files/default/etc/httpd/conf/httpd.conf # mkdir -p /root/chef-repo/site-cookbooks/httpd/files/default/etc/php.d # cp -p /opt/src/config/ php/php.ini /root/chef-repo/site-cookbooks/httpd/files/default/etc/ # cd /root/chef-repo/site-cookbooks/httpd/files/default/rpms # wget http://rpms.famillecollet.com/enterprise/5/olds/x86\_64/php-pecl-apc-3.1.13-3.el5.remi.x86\_64.rpm # cp -p /etc/php.d/apc.ini /root/chef-repo/site-cookbooks/httpd/files/ default/etc/php.d/ # wget http://rpms.famillecollet.com/enterprise/5/remi/x86\_64/php-pecl-ssh2-0.11.3-2.el5.remi.x86\_64.rpm\[/shell\]
Apache Installation and Basic Configuration
[shell]# vi httpd-server.rb cookbook_file "/tmp/web-rpm.tar" do source "rpms/web-rpm.tar" mode 0644 end
cookbook_file "/tmp/db-rpm.tar.gz" do source "rpms/db-rpm.tar.gz" mode 0644 end
filename = "web-rpm.tar" script "install_httpd" do not_if 'rpm -qa|grep httpd' interpreter "bash" user "root" code <-EOL cd /tmp tar xzf /tmp/db-rpm.tar.gz rpm -U db-rpm/5.5.27/{mysql-lib*,mysql-5*} rpm -U db-rpm/mysqlclient* tar xf /tmp/#{filename} rpm -U web-rpm/httpd-2.2.3-65/httpd* EOL end
script "install_php" do not_if 'rpm -qa|grep php' ignore_failure true interpreter "bash" user "root" code <-EOL cd /tmp rpm -i --quiet --force web-rpm/php5.3.17/{php-5.3*,php-cli*,php-common*,php-devel*,php-mbstring*,php-mbstring*,php-mysql*,php-pdo*,php-pear*} rpm -i web-rpm/php5.3.17/lib* EOL end
cookbook_file "/tmp/php-pecl-apc-3.1.13-3.el5.remi.x86_64.rpm" do source "rpms/php-pecl-apc-3.1.13-3.el5.remi.x86_64.rpm" mode 0644 end
cookbook_file "/tmp/php-pecl-ssh2-0.11.0-1.el5.rf.x86_64.rpm" do source "rpms/php-pecl-ssh2-0.11.0-1.el5.rf.x86_64.rpm" mode 0644 end
package "php-pecl-apc" do only_if "rpm -qa|grep php" action :install source "/tmp/php-pecl-apc-3.1.13-3.el5.remi.x86_64.rpm" provider Chef::Provider::Package::Rpm end
%W{libssh2 libssh2-devel}.each do |pkg| package pkg do action :install end end
package "php-pecl-ssh2" do only_if "rpm -qa|grep php" action :install source "/tmp/php-pecl-ssh2-0.11.0-1.el5.rf.x86_64.rpm" provider Chef::Provider::Package::Rpm end
script "backup_apache_config" do not_if 'ls /etc/httpd/conf/httpd.conf.org' interpreter "bash" user "root" code <-EOL cp="" -p="" tc/httpd/conf/httpd.conf{,.org}="" cp="" -p="" tc/php.ini{,.org}="" mv="" tc/httpd/conf.d/proxy_ajp.conf{,.org}="" mv="" tc/httpd/conf.d/welcome.conf{,.org}="" eol="" end="" service="" "httpd"="" do="" supports="" :status=""> true, :restart => true, :reload => :true action [ :enable, :start ] end
cookbook_file "/etc/httpd/conf/httpd.conf" do source "etc/httpd/conf/httpd.conf" owner "root" group "root" mode 0644 notifies :reload, 'service[httpd]' end
cookbook_file "/etc/php.ini" do source "etc/php.ini" owner "root" group "root" mode 0644 notifies :reload, 'service[httpd]' end
cookbook_file "/etc/php.d/apc.ini" do source "etc/php.d/apc.ini" owner "root" group "root" mode 0644 notifies :reload, 'service[httpd]' end[/shell]
I think the resources I use are cookbook_file, package, script, and service.
The detailed meaning of each resource was very easy to understand in "Introductory Chef-Solo".
For detailed instructions, it is recommended to look closely at the resource manual.
To explain a little bit of what seems difficult to see at first glance,
The following line in the cookbook_file resource notifies you to reload the httpd service when the file is updated.
notifies :reload, 'service[httpd]'
In the following line in the service resource, enable means ckconfig on. Start is, of course, the start of the service.
action [ :enable, :start ]
The not_if in the script resource is an if-based restriction that executes the script if the result of the following command is non-0.
For example, if it is not included in rpm -qa|httpd, it is used when you want to include a package. Use only_if to make it run when the return value is 0.
I put ignore_failure true used in the script "install_php" because it was not installed, probably because of the warning that appears when installing php. It's a mystery whether this is okay.
Is it like this?
Setting up basic authentication
[shell]# cp -p /etc/httpd/conf.d/basic_auth.conf .. /files/default/etc/httpd/conf.d/ # cp -p /etc/httpd/conf/.htpasswd .. /files/default/etc/httpd/conf/
# vi basic_auth.rb cookbook_file "/etc/httpd/conf/.htpasswd" do source "etc/httpd/conf/.htpasswd" mode 0644 end
cookbook_file "/etc/httpd/conf.d/basic_auth.conf" do source "etc/httpd/conf.d/basic_auth.conf" owner "root" group "root" mode 0644 notifies :reload, 'service[httpd]' end[/ shell]
Module Placement and S3Mount (*)
[shell]# mkdir -p .. /files/default/usr/local/src/ec2 # cp -p /opt/src/module.tar.gz .. /files/default/usr/local/src/ # cp -p /usr/local/src/ec2/fuse-2.8.4.tar.gz .. /files/default/usr/local/src/ec2/ # cp -p /usr/local/src/ec2/s3fs-1.68.tar.gz .. /files/default/usr/local/src/ec2/ # cp -p /etc/passwd-s3fs .. /files/default/etc/ # mkdir -p .. /files/default/etc/init.d # cp -p /etc/init.d/fuse .. /files/default/etc/init.d/ # scp omb-web01:/etc/rc.local .. /files/default/etc/
# pwd /root/chef-repo/site-cookbooks/httpd/recipes # vi wordpress.rb cookbook_file "/tmp/module.tar.gz" do source "usr/local/src/module.tar.gz" mode 0644 end
filename = "module.tar.gz" script "install_module" do not_if 'ls /var/www/html/wp-settings.php' interpreter "bash" user "root" code <-EOL cd /var/www mv html html.org tar zxf /tmp/#{filename} EOL end
# vi s3mount.rb cookbook_file "/tmp/fuse-2.8.4.tar.gz" do source "usr/local/src/ec2/fuse-2.8.4.tar.gz" mode 0644 end
filename = "fuse-2.8.4" script "install_fuse" do not_if 'export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/lib64/pkgconfig/ && ldconfig && modprobe fuse && pkg-config --modversion fuse' interpreter "bash" user "root" code <-EOL cd /usr/local/src tar zxf /tmp/#{filename}.tar.gz cd #{filename} && ./configure --prefix=/usr && make && make install export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/lib64/pkgconfig/ && ldconfig && modprobe fuse pkg-config --modversion fuse EOL end
cookbook_file "/tmp/s3fs-1.68.tar.gz" do source "usr/local/src/ec2/s3fs-1.68.tar.gz" mode 0644 end
filename = "s3fs-1.68" script "install_s3fs" do not_if 'ls /usr/local/src/s3fs-1.68/config.log' interpreter "bash" user "root" code <-EOL export="" pkg_config_path="/usr/lib/pkgconfig:/usr/lib64/pkgconfig/" &&="" ldconfig="" &&="" modprobe="" fuse="" cd="" sr/local/src="" tar="" zxf="" mp/#{filename}.tar.gz="" cd="" #{filename}="" &&="" ./configure="" --prefix="/opt/s3fs" &&="" make="" &&="" make="" install="" eol="" end="" cookbook_file="" "/etc/passwd-s3fs"="" do="" source="" "etc/passwd-s3fs"="" mode="" 0400="" end="" cookbook_file="" "/etc/init.d/fuse"="" do="" source="" "etc/init.d/fuse"="" mode="" 0755="" end="" service="" "fuse"="" do="" supports="" :status=""> true, :restart => true action [ :enable, :start ] end
directory '/var/www/html/assets' do owner 'apache' group 'apache' mode 0777 action :create end
filename = "/var/www/html/assets" script "mount_s3fs" do not_if 'grep asset /etc/rc.local' interpreter "bash" user "root" code <-EOL cp="" -rp="" ar/www/html/assets{,.bk}="" &&="" rm="" -rf="" ar/www/html/uploads/*="" pt/s3fs/bin/s3fs="" busaid="" ar/www/html/assets="" -o="" allow_other="" cp="" -rp="" ar/www/html/assets.bk/*="" ar/www/html/uploads/.="" ln="" -sf="" sr/share/pear/apc.php="" ar/www/html/apc.php="" echo="" "/opt/s3fs/bin/s3fs="" busaid="" ar/www/html/assets="" -o="" allow_other"="">> /etc/rc.local EOL end[/shell] *The s3mount recipe has not been tested due to circumstances.
Syntax Test for Cookbooks
[shell]# knife cookbook test base_setting # knife cookbook test login_users # knife cookbook test httpd[/shell] FATAL: If there is no error, it seems OK (when it comes out, it will specifically appear as the line of this cookbook)
That's it for this time. Thank you for reading.
Next time we will talk about the DB server recipe. </-EOL></-EOL></-EOL>