Use the ERB template in Puppet to automatically configure the Nginx Virtual Host
The template file is in the templates directory under the puppet module ". erb. The puppet template is mainly used for files, such as configuration files of various services. For the same service, you can consider using the template file for different configurations, for example, you can use the ERB template for Nginx and Apache Virtual Host Configuration. We recommend that you use the YUM source in the system to install nginx or other third-party YUM sources, if you use the official Nginx source to install nginx, you can check/etc/yum. repos. d/nginx. the repo file content is as follows:
[Nginx]
Name = nginx repo
Baseurl = http://nginx.org/packages/centos/?releasever/?basearch/
Gpgcheck = 0
Enabled = 1
The second method is to use createrepo to build your own YUM source. This method is even better. We can download the suitable rpm package on the official nginx website and add it to our YUM source, in a custom environment with strict requirements for automated O & M, most O & M personnel choose this method. After installing nginx in this way, you will find that it is much easier to install Nginx than the source code. For example, if nginx is automatically allocated to the user who runs nginx: nginx, nginx logs are automatically stored in/var/log/nginx, and the working directory is/etc/nginx. This is different from nginx installed by source code compilation, please pay attention to the screening during the experiment.
I skipped other elementary knowledge points like Puppet. I directly pasted the file content. The file structure of/etc/puppet is as follows:
| -- Auth. conf
| -- Fileserver. conf
| -- Manifests
| -- Nodes
| -- Client.bkjia.com. pp
| '-- Test.bkjia.com. pp
| '-- Site. pp
| -- Modules
| '-- Nginx
| -- Files
| -- Manifests
| '-- Init. pp
| '-- Templates
| -- Nginx. conf. erb
| '-- Nginx_vhost.conf.erb
'-- Puppet. conf
The file content of site. pp is as follows:
1 import "nodes/*. pp"
The contents of client.bkjia.com. pp are as follows:
Node 'client .bkjia.com '{
Include nginx
Nginx: vhost {'client .bkjia.com ':
Sitedomain => "client.bkjia.com ",
Rootdir => "client ",
}
}
The content of test.bkjia.com. pp is as follows:
Node 'test .bkjia.com '{
Include nginx
Nginx: vhost {'test .bkjia.com ':
Sitedomain => "test.bkjia.com ",
Rootdir => "test ",
}
}
/Etc/puppet/modules/nginx/manifests/init. pp
The file content is as follows:
Class nginx {
Package {"nginx ":
Ensure => present,
}
Service {"nginx ":
Ensure => running,
Require => Package ["nginx"],
}
File {"nginx. conf ":
Ensure => present,
Mode => 644, owner => root, group => root,
Path => "/etc/nginx. conf ",
Content => template ("nginx/nginx. conf. erb "),
Require => Package ["nginx"],
}
}
Define nginx: vhost ($ sitedomain, $ rootdir ){
File {"/etc/nginx/conf. d/$ {sitedomain}. conf ":
Content => template ("nginx/nginx_vhost.conf.erb "),
Require => Package ["nginx"],
}
}
/Etc/puppet/modules/nginx/templates/nginx. conf. erb
The file content is as follows:
User nginx;
Worker_processes 8;
Error_log/var/log/nginx/error. log warn;
Pid/var/run/nginx. pid;
Events {
Use epoll;
Worker_connections 51200;
}
Http {
Include/etc/nginx/mime. types;
Default_type application/octet-stream;
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
Access_log/var/log/nginx/access. log main;
Sendfile on;
# Tcp_nopush on;
Keepalive_timeout 65;
# Gzip on;
Include/etc/nginx/conf. d/*. conf;
}
/Etc/puppet/modules/nginx/templates/nginx_vhost.conf.erb
The file content is as follows:
Server {
Listen 80;
Server_name <% = sitedomain %>;
Access_log/var/log/nginx/<% = sitedomain %>. access. log;
Location /{
Root/var/www/<% = rootdir %>;
Index. php index.html index.htm;
}
}
Finally, we can verify the effect on machines named client.bkjia.com and test.bkjia.com. The command is as follows:
Puppetd -- test -- server server.bkjia.com
Puppet Learning Series:
Puppet Learning 1: Installation and simple instance applications
Puppet 2: simple module configuration and application
Research on three Backup Recovery solutions for Puppet agent
Register your Puppet node in a safer way
Deep understanding of Puppet syntax and working mechanism through SSH Configuration
Puppet uses Nginx multiple ports for Load Balancing
C/S mode instance of Puppet in CentOS (5 and 6)
For more information about Puppet, click here.
Puppet: click here
This article permanently updates the link address: