一、 安装mysql服务:
1、 安装mysql服务
简单脚本,可视需求修改。
#!/bin/bash
# 获取mysql软件源
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm && yum -y install mysql80-community-release-el7-3.noarch.rpm
rm -rf mysql80-community-release-el7-3.noarch.rpm
# 禁用mysql8.0版本镜像,启用5.7版本镜像(可视需要修改需安装的mysql版本)
yum-config-manager --disable mysql80-community && yum-config-manager --enable mysql57-community
# 安装mysql5.7服务端
yum -y install mysql-community-server
# 启动mysql服务,是mysql服务初始化,并关闭服务
mkdir -p /data/mysql/{data,logs,bin_log}
chown -R mysql.mysql /data/mysql
# 生成mysql配置文件,按需修改
cat << EOF > /etc/my.cnf
[mysqld]
datadir=/data/mysql/data/
log-error=/data/mysql/logs/error-log
pid-file=/var/run/mysqld/mysqld.pid
socket=/data/mysql/mysql.sock
max_connections=2000
back_log=300
max_connect_errors=2000
open_files_limit=10240
max_allowed_packet=32M
wait_timeout=10
sort_buffer_size=16M
join_buffer_size=16M
query_cache_size=128M
query_cache_limit=4M
thread_stack=512M
log_bin=/data/mysql/bin_log/bin-log
binlog_format=row
sql_mode =STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
character-set-server=utf8
event_scheduler=ON
lower_case_table_names=1
server_id=1
expire_logs_days = 7
max_binlog_size = 500m
EOF
mkdir /var/lib/mysql
# 重启mysql服务,并设置开机自启
systemctl restart mysqld && systemctl enable mysqld
ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock
# 获取mysql初始密码
echo you mysql passwd is `awk /temporary\ password/'{print $NF}' /data/mysql/logs/error-log.err`
2、 修改初始密码
mysql -uroot -p password
ALTER USER USER() IDENTIFIED BY 'YOUPASSWORD';
3、 创建zabbix连接数据库账号、密码,并创建zabbix使用数据库
create database zabbix;
grant all on zabbix.* to zabbix@'%' identified by 'zabbix';
二、 安装nginx
1、 源码编译安装nginx
#!/bin/bash
# 安装依赖环境
yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc make git
mkdir -p /data/soft && cd /data/soft
# 获取源码包
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -xzvf nginx-1.16.0.tar.gz
mv nginx-1.16.0 nginx
cd nginx
git clone https://github.com/alibaba/nginx-http-concat
./configure \
--prefix=/etc/nginx \
--sbin-path=/etc/nginx/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/etc/nginx/nginx.pid \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-stream_ssl_preread_module \
--with-stream \
--add-module=./nginx-http-concat
make -j2 && make install
mkdir -p /usr/lib/systemd/system
cat <<\EOF >/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/etc/nginx/nginx.pid
ExecStartPre=/etc/nginx/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/etc/nginx/nginx -c /etc/nginx/nginx.conf
ExecReload=/etc/nginx/nginx -s reload
ExecStop=/etc/nginx/nginx -s stop
ExecQuit=/etc/nginx/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service
三、 安装PHP服务
yum -y install php php-mysql php-devel php-fpm
php-fpm 需在 5.4 版本以上
四、 安装zabbix server端和agent 端
1、 获取软件源
mkdir -p /data/soft && cd /data/soft
wget https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/4.4.4/zabbix-4.4.4.tar.gz
2、 解压,并安装依赖
tar -xf zabbix-4.4.4.tar.gz
yum -y install gcc gcc-c++ gcc-devel pcre pcre-devel mysql-dev mysql-devel net-snmp-devel curl-devel php-gd php-mysql php-bcmath php-mbstring php-xm libevent libevent-devel zlib zlib-devel php-xml php-fpm
3、 编译安装zabbix
cd zabbix-4.4.4 && ./configure --prefix=/data/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl
make -j2 && make install
4、 将zabbix初始化数据导入mysql内
mysql -uroot -p zabbix < /data/soft/zabbix-4.4.4/database/mysql/schema.sql
mysql -uroot -p zabbix < /data/soft/zabbix-4.4.4/database/mysql/images.sql
mysql -uroot -p zabbix < /data/soft/zabbix-4.4.4/database/mysql/data.sql
5、 创建启动zabbix的用户
useradd -r -s /sbin/nologin zabbix
6、 将zabbix前端页面copy到nginx默认页面内
mkdir -p /data/html
cp -a /data/soft/zabbix-4.4.4/frontends/php/* /data/html
7、 创建zabbix_server zabbix_agentd 启动脚本
cat << EOF > /usr/lib/systemd/system/zabbix_server.service
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/data/zabbix/etc/zabbix_server.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-server
Type=forking
Restart=on-failure
PIDFile=/data/zabbix/run/zabbix_server.pid
KillMode=control-group
ExecStart=/data/zabbix/sbin/zabbix_server -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
8、修改nginx配置文件,使其支持php、php静态页面
cat << EOF > /etc/nginx/nginx.conf
user nginx;
worker_processes 2;
events {
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
location / {
root /data/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /data/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}
}
}
EOF
cat << EOF > /usr/lib/systemd/system/zabbix_agentd.service
[Unit]
Description=Zabbix Agentd
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/data/zabbix/etc/zabbix_agentd.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agentd
Type=forking
Restart=on-failure
PIDFile=/data/zabbix/run/zabbix_agentd.pid
KillMode=control-group
ExecStart=/data/zabbix/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl start zabbix_server zabbix_agentd && systemctl enable zabbix_server zabbix_agentd
9、 修改php-fpm 配置,使php-fpm 使用与nginx相同的用户和组
cat << EOF > /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 15
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
EOF
修改 user、group后,需注意 /var/lib/php/session 的权限
useradd -r -s /sbin/nologin nginx && chown nginx.nginx /var/lib/php/session
]# grep -Ev "^$|^;" /etc/php.ini
[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 30M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = Off
session.bug_compat_warn = Off
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off
[Assertion]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
EOF
五、 启动服务
#启动mysql、nginx、php-fpm 服务
systemctl restart mysqld && systemctl enable mysql
systemctl restart php-fpm && systemctl enable php-fpm
systemctl restart nginx && systemctl enable nginx
六、配置
通过 IP:port/setup.php 进入zabbix配置页面,若需使用域名访问zabbix,则需在nginx上配置基于域名的虚拟主机,若此时通过IP+port的方式无法访问zabbix web端,可查看日志看是否报错,检查目录权限等方式排查。