#关闭iptables
service iptables stop
chkconfig iptablse off
#关闭selinux
setenforce 0
sed -i.bak '/^SELINUX=/ s#.*#SELINUX=disabled#'  /etc/selinux/config
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel
#建立nginx的用户 -s指定用户的shell类型 -M不创建用户家目录
useradd nginx -s /sbin/nologin -M



cd && wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xvf nginx-1.10.3.tar.gz && cd nginx-1.10.3


./configure --prefix=/usr/local/product/nginx1.10 --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
make && make install
#--with-http_stub_status_module:支持nginx状态查询
#--with-http_ssl_module:支持https
#--with-http_spdy_module:支持google的spdy,这个必须ssl支持
#--with-pcre:为了支持rewrite重写功能,必须制定pcre

#建立软连接,便于软件的更新
ln -s /usr/local/product/nginx1.10  /usr/local/nginx


###
##php 的安装
###
cd && wget --no-check-certificate https://www.php.net/distributions/php-5.5.35.tar.gz
tar -xvf php-5.5.35.tar.gz && cd php-5.5.35


./configure --prefix=/usr/local/product/php-5.5.38 \
 --with-config-file-path=/usr/local/product/php-5.5.38/etc \
 --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 \
--with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local \
--with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local \
--enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local \
--with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd \
--enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath

make && make install
ln -s /usr/local/product/php-5.5.38 /usr/local/php
#源码安装包内的配置文件 
cp php.ini-production /usr/local/php/etc/php.ini
#修改php-fpm原始配置文件
cd /usr/local/php/etc/
cp  php-fpm.conf.default php-fpm.conf

#修改php.ini参数-zabbix环境需要修改的参数,否则zabbix web会出现警告
sed -i.bak '/max_execution/ s#.*#max_execution_time = 300#' php.ini
sed -i.bak '/memory_limit/ s#.*#memory_limit = 128M#' php.ini 
sed -i.bak '/post_max_size/ s#.*#post_max_size = 16M#' php.ini
sed -i.bak '/upload_max_filesize/ s#.*#upload_max_filesize = 2M#' php.ini 
sed -i.bak '/max_input_time/ s#.*#max_input_time = 300#' php.ini 
sed -i.bak '/date.timezone/ s#.*#date.timezone = PRC#' php.ini
###
###mysql的安装
###
#建立指定用户组和建立指定的用户家目录,指定的用户组 指定shell类型的mysql用户
groupadd mysql
useradd -r -g mysql -d /data/mysql -s /sbin/nologin mysql
#-p 有创建完提示
#-p, --parents     no error if existing, make parent directories as needed
#  -v, --verbose     print a message for each created directory

mkdir -pv /data/mysql
chown -R mysql.mysql /data/mysql


yum install cmake gcc* ncurses-devel -y
cd && wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.62.tar.gz
tar -xvf mysql-5.5.62.tar.gz && cd mysql-5.5.62

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/product/mysql5.5.62 -DDEFAULT_CHARSET=utf8 \
-DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DWITH_EXTRA_CHARSETS=all \
 -DWITH_READLINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3306 -DDEFAULT_COLLATION=utf8_general_ci


make && make install
ln -s /usr/local/product/mysql5.5.62 /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql


cd /usr/local/mysql/support-files/
cp my-medium.cnf /data/mysql/my.cnf
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld



cd /usr/local/mysql/scripts
./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/

sed -i.bak '22a\datadir=/data/mysql/' /data/mysql/my.cnf
[ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak
yum remove -y  mysql
/etc/init.d/mysqld start
chkconfig mysqld on

ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin

mysqladmin -uroot password  "3kquan"
#建立数据库和用户,以及配置用户权限
mysql -uroot -p3kquan << EOF 
create database zabbix default charset utf8;
grant all privileges on zabbix.* to zabbix@'localhost' identified by 'zabbix';
flush privileges;
exit;
EOF
#zabbix


cd && wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.0.27/zabbix-3.0.27.tar.gz


yum install mysql-devel net-snmp-devel curl-devel -y
tar -xvf zabbix-3.0.27.tar.gz && cd zabbix-3.0.27

./configure --prefix=/usr/local/zabbix-3.0.27/ --enable-server \
--enable-agent --with-mysql --with-net-snmp --with-libcurl \
--with-libxmli2

make install 
 
groupadd zabbix
useradd zabbix -s /sbin/nologin -M -g zabbix

mkdir /var/lib/mysql
ln -s /tmp/mysql.sock /var/lib/mysql/
mysql -uroot -p3kquan zabbix < database/mysql/schema.sql
mysql -uroot -p3kquan zabbix < database/mysql/images.sql
mysql -uroot -p3kquan zabbix < database/mysql/data.sql 

#frontends在源码包的目录里
mkdir /data/web/zabbix.haocheng.com -p
mkdir /data/logs/zabbix -p
cp -rp frontends/php/* /data/web/zabbix.haocheng.com/

#配置nignx虚拟主机
cd /usr/local/nginx/conf
mkdir extra && cd extra
cat > zabbix.conf <<'EOF'
server {
listen 8027;
server_name zabbix.haocheng.com;
access_log /data/logs/zabbix/zabbix.haocheng.com.access.log main;
index index.html index.php index.html;
root /data/web/zabbix.haocheng.com;
 
location /{
       try_files $uri $uri/ /index.php?$args;
}
location ~ ^(.+.php)(.*)$ {
       fastcgi_split_path_info ^(.+.php)(.*)$;
       include fastcgi.conf;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
EOF

cat >/usr/local/nginx/conf/nginx.conf<<'EOF'
user  nginx;
worker_processes  1;
 
#error_log  logs/error.log warning;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
pid        logs/nginx.pid;
 
events {
    worker_connections  1024;
}
 
 
http {
    include       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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
    include extra/*.conf;
}
EOF




cd /usr/local/zabbix-3.0.27/etc

sed -i '/LOgFile/s#.*#LogFile=/tmp/zabbix_server.log#' zabbix_server.conf
sed -i '/PidFile/s#.*#PidFile=/tmp/zabbix_server.pid#' zabbix_server.conf
sed -i '/DBHost/s#.*#DBHost=localhost#' zabbix_server.conf
sed -i '/DBNname/s#.*#DBName=zabbix#' zabbix_server.conf
sed -i '/DBUser/ s#.*#DBUser=zabbix#' zabbix_server.conf
sed -i '/DBPassword/ s#.*#DBPassword=zabbix#' zabbix_server.conf

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

#启动服务:
/usr/local/php/sbin/php-fpm
/usr/local/zabbix-3.0.27/sbin/zabbix_server
/usr/local/nginx/sbin/nginx
/usr/local/zabbix-3.0.27/sbin/zabbix_agentd 



#添加到开机自启动:
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
echo "/usr/local/zabbix-3.0.27/sbin/zabbix_server" >> /etc/rc.local
echo "/usr/local/php/sbin/php-fpm" >>/etc/rc.local