hellow 运维
分类: 系统运维
2015-05-25 14:22:41
#!/bin/bash
# install Nginx 1.6.2 + mysql5.5.x + PHP-FPM 5.4.x + Zabbix 2.4.4 automatically.
# Tested on CentOS 6.5
##############################################
# 变量
##############################################
err_echo(){
echo
-e
"\e[91m[Error]: $1 33[0m"
exit
1
}
info_echo(){
echo
-e
"\e[92m[Info]: $1 33[0m"
}
warn_echo(){
echo
-e
"\e[93m[Warning]: $1 33[0m"
}
check_exit(){
if
[ $? -
ne
0 ];
then
err_echo
"$1"
exit1
fi
}
##############################################
# check
##############################################
if
[ $EUID -
ne
0 ];
then
err_echo
"please run this script as root user."
exit
1
fi
if
[
"$(awk '{if ( $3 >= 6.0 ) print "
CentOS 6.x
"}' /etc/redhat-release 2>/dev/null)"
!=
"CentOS 6.x"
];
then
err_echo
"This script is used for RHEL/CentOS 6.x only."
fi
##############################################
# Useradd deploy nginx程序运行账号
##############################################
info_echo
"Useradd deploy"
useradd
deploy
##############################################
# yum repo
##############################################
info_echo
"配置yum源......"
if
[ ! -f LNMP+zabbix.repo ];
then
cat
>
/etc/yum
.repos.d
/LNMP
+zabbix.repo <<
'EOF'
[nginx]
name=nginx repo
baseurl=http:
//nginx
.org
/packages/centos/6/
$basearch/
gpgcheck=0
enabled=1
[webtatic]
name=Webtatic Repository EL6 - $basearch
#baseurl=http://repo.webtatic.com/yum/el6/$basearch/
mirrorlist=http:
//mirror
.webtatic.com
/yum/el6/
$basearch
/mirrorlist
failovermethod=priority
enabled=0
gpgcheck=0
[epel]
name=Extra Packages
for
Enterprise Linux 6 - $basearch
baseurl=http:
//mirrors
.aliyun.com
/epel/6/
$basearch
http:
//mirrors
.aliyuncs.com
/epel/6/
$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
[remi]
name=Les RPM de remi pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/
mirrorlist=http:
//rpms
.famillecollet.com
/enterprise/6/remi/mirror
enabled=1
gpgcheck=0
[zabbix]
name=Zabbix Official Repository-$basearch
baseurl=http:
//repo
.zabbix.com
/zabbix/2
.4
/rhel/6/
$basearch/
enabled=1
gpgcheck=0
[zabbix-non-supported]
name=Zabbix Official Repository non-supported-$basearch
baseurl=http:
//repo
.zabbix.com
/non-supported/rhel/6/
$basearch/
enabled=1
gpgcheck=0
EOF
fi
##############################################
# Install nginx+Mysql+PHP+zabbix
##############################################
info_echo
"Install nginx+Mysql+PHP+zabbix......"
yum -y
install
nginx php php-fpm php-cli php-common php-gd php-mbstring php-mcrypt php-mysql php-pdo php-devel php-imagick php-xmlrpc php-xml php-bcmath php-dba php-enchant php-yaf mysql mysql-server zabbix zabbix-get zabbix-agent zabbix-server-mysql zabbix-web-mysql zabbix-server
check_exit
"Failed to install Nginx/Mysql/PHP/Zabbix!"
#########################################
# Nginx
#########################################
info_echo
"Nginx 配置文件更新 ...."
if
[ -f
/etc/nginx/nginx
.conf ];
then
mv
/etc/nginx/nginx
.conf
/etc/nginx/nginx
.conf.bak
cat
>
/etc/nginx/nginx
.conf <<
'EOF'
user deploy;
worker_processes 2;
pid
/var/run/nginx
.pid;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
client_header_buffer_size 4k;
open_file_cache max=65535 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 1;
server_names_hash_bucket_size 64;
server_name_in_redirect off;
include
/etc/nginx/mime
.types;
default_type application
/octet-stream
;
##
# Logging Settings
##
access_log
/var/log/nginx/access
.log;
error_log
/var/log/nginx/error
.log;
##
# Gzip Settings
##
gzip
on;
gzip_disable
"msie6"
;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text
/plain
text
/css
application
/json
application
/x-javascript
text
/xml
application
/xml
application
/xml
+rss text
/javascript
;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
log_format main
'$server_name $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time'
;
include
/etc/nginx/conf
.d/*.conf;
include
/etc/nginx/sites-enabled/
*.conf;
}
EOF
fi
sed
-i
"/worker_processes/cworker_processes $( grep "
processor
" /proc/cpuinfo| wc -l );"
/etc/nginx/nginx
.conf
info_echo
"zabbix 配置文件添加"
cat
>
/etc/nginx/conf
.d
/zabbix
.conf <<
'EOF'
server{
listen 80;
server_name _;
index index.php;
root
/data/web/zabbix
;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 30d;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
mv
/etc/nginx/conf
.d
/default
.conf
/etc/nginx/conf
.d
/default
.conf.bak
/etc/init
.d
/nginx
restart
#########################################
# Zabbix
#########################################
info_echo
"Downloading zabbix Web...."
info_echo
"创建网站目录..."
mkdir
-p
/data/web/zabbix
info_echo
"Downloading..."
if
[ ! -f
/tmp/zabbix
.
tar
.gz ];
then
cd
/tmp
&& wget -O zabbix.
tar
.gz
'http://sourceforge.net/projects/zabbix/files/latest/download?source=files'
fi
info_echo
"解压安装文件..."
cd
/tmp
&&
tar
-zxvf
/tmp/zabbix
.
tar
.gz
check_exit
"failed to extract zabbix frontend"
ZABBIX_DIR=`
ls
/tmp/
|
grep
zabbix-`
mv
/tmp/
${ZABBIX_DIR}
/frontends/php/
*
/data/web/zabbix/
chown
-R deploy.deploy
/data/web/zabbix
##############################################
# Database
##############################################
info_echo
"Mysql配置文件更新..."
sed
-i
'/^socket/i\port = 3306'
/etc/my
.cnf
sed
-i
'/^socket/a\skip-external-locking\nkey_buffer_size = 256M\nmax_allowed_packet = 1M\ntable_open_cache = 256\nsort_buffer_size = 1M\nread_buffer_size = 1M\nread_rnd_buffer_size = 4M\nmyisam_sort_buffer_size = 64M\nthread_cache_size = 8\nquery_cache_size= 16M\nthread_concurrency = 4\ncharacter-set-server=utf8\ninnodb_file_per_table=1'
/etc/my
.cnf
info_echo
"Restart mysql ..."
/etc/init
.d
/mysqld
start
info_echo
"Create Databases..."
mysql -e
'CREATE DATABASE `zabbix` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;'
mysql -e
"GRANT ALL PRIVILEGES on *.* to zabbix@'localhost' IDENTIFIED BY 'zabbix';"
mysql -e
"flush privileges"
info_echo
"配置zabbix的数据库项"
sed
-i
'/DBPassword=/a\DBPassword=zabbix'
/etc/zabbix/zabbix_server
.conf
info_echo
"importing schema.sql"
mysql -uzabbix -pzabbix zabbix < $(rpm -ql zabbix-server-mysql|
grep
schema.sql)
check_exit
"failed to import schema.sql"
info_echo
"importing images.sql"
mysql -uzabbix -pzabbix zabbix < $(rpm -ql zabbix-server-mysql|
grep
images.sql)
check_exit
"failed to import images.sql"
info_echo
"importing data.sql"
mysql -uzabbix -pzabbix zabbix < $(rpm -ql zabbix-server-mysql|
grep
data.sql)
check_exit
"failed to import data.sql"
#########################################
# PHP-FPM
#########################################
info_echo
"更新/etc/php.ini,www.conf ..."
sed
-i
'/^;default_charset/a\default_charset = "UTF-8"'
/etc/php
.ini
sed
-i
'/^expose_php/cexpose_php = Off'
/etc/php
.ini
sed
-i
'/^max_execution_time/cmax_execution_time = 300'
/etc/php
.ini
sed
-i
'/^max_input_time/cmax_input_time = 300'
/etc/php
.ini
sed
-i
'/^memory_limit/cmemory_limit = 256M'
/etc/php
.ini
sed
-i
'/^post_max_size/cpost_max_size = 32M'
/etc/php
.ini
sed
-i
'/^upload_max_filesize/cupload_max_filesize = 300M'
/etc/php
.ini
sed
-i
'/^max_file_uploads/cmax_file_uploads = 30'
/etc/php
.ini
sed
-i
'/^;date.timezone/cdate.timezone = "PRC"'
/etc/php
.ini
sed
-i
's/apache/deploy/g'
/etc/php-fpm
.d
/www
.conf
chown
deploy.deploy -R
/var/lib/php
info_echo
"Checking php-fpm configuration file..."
/etc/init
.d
/php-fpm
configtest
check_exit
"PHP-FPM configuration syntax error"
info_echo
"Restart PHP-FPM ..."
/etc/init
.d
/php-fpm
restart
info_echo
"Restart Zabbix Server ..."
/etc/init
.d
/zabbix-server
restart
info_echo
"Restart Zabbix Agent ..."
/etc/init
.d
/zabbix-agent
restart
#########################################
# Chkconfig
#########################################
chkconfig nginx on
chkconfig php-fpm on
chkconfig mysql on
chkconfig zabbix-agent on
chkconfig zabbix-server on
zabbix客户端的安装比较简单:
1:linux系统
下载zabbix软件包,解压zabbix软件包:
cd /home/soft/zabbix-1.8.11/ ./configure --prefix=/usr/local/zabbix --enable-agent && make && make install
在http://down.51cto.com/data/556418这个位置有他的配置文件和服务文件的模板,是我修改过的,可以直接使用。
/usr/sbin/groupadd zabbix
/usr/sbin/useradd zabbix -g zabbix
/sbin/chkconfig --add zabbix-agentd
/sbin/chkconfig --level 345 zabbix-agentd on
echo "zabbix-agent 10050/tcp #Zabbix Agent">>/etc/services
echo "zabbix-agent 10050/udp #Zabbix Agent">>/etc/services
/etc/init.d/zabbix-agentd restart
2:windows端的安装:
下载zabbix的windows软件包,解压zabbix软件包,windows端的配置文件和linux一样,执行下面的安装zabbix为服务:
C:\zabbix\zabbix_agentd.exe -c C:\zabbix\zabbix_agentd.conf -i
net start "Zabbix Agent"
@pause
官方网站:http://www.zabbix.com/download.php
学习文档:http://blog.51cto.com/zt/45
关闭selinux :
vi /etc/selinux/config
SELINUX=disabled
先添加一个zabbix的官方yum源
我的源:
[root@localhost ~]# cat /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/2.4/rhel/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://repo.zabbix.com/non-supported/rhel/6/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
You have new mail in /var/spool/mail/root
[root@localhost ~]#
rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm
安装依赖组件包
yum -y install iksemel libssh2 zabbix-server-mysql zabbix-web-mysql mysql-server zabbix-agent install net-snmp-devel net-snmp-libs net-snmp-perl net-snmp-utils net-snmp
每个组件包具体作用,请直接百度软件包名称即可,这里不再详加说明
这里使用yum源安装的数据库版本应该是Mysql 5.1.73,初始密码为空,一定要记得设置root用户密码!
3-1.
登陆数据库:
mysql -u root
3-2.
修改数据库密码:
set password for root@localhost=password('suiyi');
这里我设置的密码是"suiyi",你修改成你自己想要设置的密码就行
3-3.
创建zabbix的数据库:
create database zabbixdb character set utf8 collate utf8_bin;
我这里创建的数据库名字为zabbixdb,你可以自己定义,但是注意后续的操作也要试用你自己的数据库名称
为了支持中文,数据库字符集采用utf8
这里说的支持中文不是指的中文web管理界面的,而是你在定义添加设备的时候可以使用中文名称
3-4.
授权zabbix系统访问Mysql数据库的用户和密码:
grant all privileges on zabbixdb.* to zabbixuser@localhost identified by 'zabbixpass';
zabbixdb.*指的是访问zabbixdb库内的所有表,这里我定义的用户为zabbixuser 密码为 zabbixpass,同样这里也要修改成你自己的
3-5.
导入zabbix数据库表结构
mysql -uroot -psuiyi zabbixdb < /usr/share/doc/zabbix-server-mysql-2.4.1/create/schema.sql
mysql -uroot -psuiyi zabbixdb < /usr/share/doc/zabbix-server-mysql-2.4.1/create/images.sql
mysql -uroot -psuiyi zabbixdb < /usr/share/doc/zabbix-server-mysql-2.4.1/create/data.sql
这三个表,都要导入
记得每条指令的前面部分
mysql -uroot -psuiyi zabbixdb
mysql 数据库操作主指令
-uroot 以root用户登录
-psuiyi 这里的suiyi为你3-2步骤设置的root密码(这里注意,-p和密码之间不要有空格,直接输入密码就行 否则会报错)
zabbixdb 为3-3步骤创建的数据库名称
/usr/share/doc/zabbix-server-mysql-2.4.1/create/目录内的三个sql文件为安装zabbix软件时自动创建
编辑zabbix的配置文件
vi /etc/zabbix/zabbix_server.conf
依次找到DBHost DBName DBUser DBPassword 四个关键字,
修改成你自己上面定义的内容:
DBHost=localhost 指的是访问本机数据库 DBName=zabbixdb 3-3 创建的zabbix的数据库名字 DBUser=zabbixuser 3-4授权访问数据库的用户名 DBPassword=zabbixpass 3-4授权访问数据库的密码
编辑PHP环境配置文件:
vi /etc/httpd/conf.d/zabbix.conf
对应修改内容如下
php_value max_execution_time 300 php_value memory_limit 128M php_value post_max_size 16M php_value upload_max_filesize 2M php_value max_input_time 300 php_value date.timezone Asia/Shanghai
其实zabbix都给你设置好了,只有最后一项需要你改动下,去掉前面的#号
时区设置成Asia/Shanghai
添加固定服务端口,以便于后期监测和防火墙监控端口
编辑/etc/services文件
vi /etc/services
跳到文件末尾(大写G直接跳)追加以下内容:
zabbix-agent 10050/tcp # zabbix-agent-tcpzabbix-agent 10050/udp # zabbix-agent-udpzabbix-trapper 10051/tcp # zabbix-trapper-tcpzabbix-trapper 10051/udp # zabbix-trapper-udp
开启防火墙端口(tcp\80,10050,10051 udp\10050,10051)
当然你可以直接关闭防火墙,但是强烈建议生产环境一定不要这么做!
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 10050 -j ACCEPT
iptables -A INPUT -m state --state NEW -m udp -p udp --dport 10050 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 10051 -j ACCEPT
iptables -A INPUT -m state --state NEW -m udp -p udp --dport 10051 -j ACCEPT
service iptables save
设置所需服务自动启动:(httpd,mysqld,snmpd,snmptrapd,zabbix-agent,zabbix-server)
chkconfig httpd on ; service httpd start
chkconfig mysqld on ; service mysqld start
chkconfig snmpd on ; service snmpd start
chkconfig snmptrapd on ; service snmptrapd start
chkconfig zabbix-agent on ; service zabbix-agent start
chkconfig zabbix-server on ; service zabbix-server start