Puppet管理Nginx主机


一:介绍

Puppet管理Nginx主机,将nginx主机加入到puppet中,实现自动安装、配置、和启动服务

二:nginx模块结构


[root@master modules]# tree /etc/puppet/modules/nginx/
/etc/puppet/modules/nginx/
├── files
├── manifests
│  ├── conf.pp
│  ├── init.pp
│  └── install.pp
└── templates
    ├── nginx.conf.erb
    └── vhost.erb

三:配置解释

install.pp为安装nginx的配置文件

[root@master manifests]# cat install.pp
class nginx::install {
    package {"nginx":
    ensure => present,
    }
}

conf.pp为配置nginx的配置文件

[root@master manifests]# cat conf.pp
class nginx::conf {
    define nginx::vhost($port,$hostname,$rootdir,$filename=$title){
    file {"/etc/nginx/conf.d":
        ensure => directory,
        owner => "root",
        group => "root",
        mode => "744",
        recurse => true,
        require => Class["nginx::install"],
    }
    file {"$filename":
        owner => "root",
        group => "root",
        mode => "644",
        path => "/etc/nginx/conf.d/${filename}",
        content => template("nginx/vhost.erb"),
        require => File["/etc/nginx/conf.d"],
    }
}

nginx::vhost{"www.puppet.com.conf":
    port => "80",
    hostname => "www.puppet.com",
    rootdir => "/var/www/puppet",
    }
}
 

init.pp为nginx模块的入口文件

[root@master manifests]# cat init.pp
class nginx {
    include nginx::install,nginx::conf
}

templates下面为nginx配置文件模板:

[root@master templates]# cat vhost.erb
server {
listen <%= port %>;
server_name <%= hostname %>;
root <%= rootdir %>;
index index.php;

location ~ .*\.php {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_headers_hash_max_size 512;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}

location ~ \.(css|js)?$ {
expires 2h;
}

location ~ .*\.(mp3|jpg|jpeg|rar|png|zip|wmv|rm|doc|ppt|gif|bmp|xls|pdf|swf)$ {
expires 5d;
}
}

Puppet 学习系列:

Puppet 学习一:安装及简单实例应用

Puppet学习二:简单模块配置和应用

有关Puppet agent端三种备份恢复方案探讨研究
选择更安全的方式注册你的Puppet节点
通过配置SSH深刻理解Puppet的语法及工作机制
Puppet利用Nginx多端口实现负载均衡
CentOS(5和6)下Puppet的C/S模式实例

Puppet 的详细介绍:请点这里
Puppet 的下载地址:请点这里

相关内容