系统环境
操作系统:centos 7 PHP版本:php72 zabbix版本:4.4 nginx版本:1.16.1 maraidb版本:10.3
1、更改yum源
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
###base源:阿里云镜像源
curl -o /etc/yum.repos.d/aliyun-base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
###epel源:阿里云镜像源
touch /etc/yum.repos.d/aliyun.epel.repo
cat >> /etc/yum.repos.d/aliyun.epel.repo << EOF
[EPEL]
name=aliyun_epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
EOF
###zabbix :清华大学镜像源
touch /etc/yum.repos.d/zabbix.repo
cat >> /etc/yum.repos.d/zabbix.repo << EOF
[zabbix]
name=zabbix
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/
enabled=1
gpgcheck=0
EOF
2、安装lnmp环境
rpm包方式安装
参考链接:LNMP环境搭建(一)
mysql要采用mariadb,现在zabbix采用的依赖需要mariadb-libs,会和mysql-libs冲突
touch /etc/yum.repos.d/mariadb.repo
cat >> /etc/yum.repos.d/mariadb.repo << EOF
[mariadb]
name=mariadb
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.3/centos7-amd64
enabled=1
gpgcheck=0
EOF
yum install mariadb mariadb-server -y
systemctl start mariadb
3、安装zabbix环境
yum install zabbix-server zabbix-server-mysql zabbix-get zabbix-agent zabbix-web zabbix-web-mysql -y
##从zabbix4.4开始,源中提供nginx的配置文件
##包名为zabbix-nginx-conf
##如果采用yum方式安装lnmp环境,直接安装后重新启动nginx和fpm-fpm服务并将网站根目录指向zabbix web目录即可
##可以免去很多配置工作
mysql创建zabbix用户并授权
###创建zabbix用户
CREATE USER 'zabbix'@'localhost' IDENTIFIED BY '123456';
CREATE DATABASE zabbix CHARSET 'utf8';
GRANT ALL ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY '123456';
###更改root密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
####开启远程登陆
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
导入zabbix数据表
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
修改zabbix配置文件
sed -i 's/# DBPassword=/DBPassword=123456/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBPort=/DBPort=3306/g' /etc/zabbix/zabbix_server.conf
修改nginx配置文件
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
touch /etc/nginx/nginx.conf
cat >> /etc/nginx/nginx.conf << EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 50M; #最大上传50M
server_tokens off; #关闭版本号显示
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩
gzip_min_length 1k;
# gzip 压缩级别 1-10
gzip_comp_level 2;
# 进行压缩的文件类型。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png image/jpg image/bmp;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
server{
listen 80;
server_name localhost;
root /usr/share/zabbix;
index index.php;
location ~ [^/]\.php(/|\$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_index index.php;
fastcgi_param DOCUMENT_ROOT /usr/share/zabbix;
fastcgi_param SCRIPT_FILENAME /usr/share/zabbix\$fastcgi_script_name;
fastcgi_param PATH_TRANSLATED /usr/share/zabbix\$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING \$query_string;
fastcgi_param REQUEST_METHOD \$request_method;
fastcgi_param CONTENT_TYPE \$content_type;
fastcgi_param CONTENT_LENGTH \$content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
}
EOF
修改php配置文件
sed -i 's/post_max_size = 8M/post_max_size = 16M/g' /etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /etc/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 600/g' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time = 600/g' /etc/php.ini
echo -e "\ndate.timezone = UTC" >> /etc/php.ini
sed -i 's/pdo_mysql.default_socket=/pdo_mysql.default_socket=\/var\/lib\/mysql\/mysql.sock/g' /etc/php.ini
sed -i 's/mysqli.default_socket =/mysqli.default_socket =\/var\/lib\/mysql\/mysql.sock/g' /etc/php.ini
###修改php运行用户
sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf
#重启php服务
systemctl restart php-fpm
启动相关服务
###关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
###放通防火墙
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload
###启动zabbix服务
systemctl start zabbix-server zabbix-agent
###更改相关目录权限
chmod 755 -Rf /etc/zabbix/web/
chown -Rf nginx:nginx /etc/zabbix/web/
###需要更改
chmod -Rf 755 /var/lib/php
chown -Rf nginx:nginx /var/lib/php
###不修改该目录权限打开网页的时候会显示一片空白,如果通过lamp方式安装好zabbix后切换到lnmp环境会在网页上提示Cannot start session. zabbix错误
###查看nginx错误日志能看到类似于以下内容的错误
#0 /usr/share/zabbix/bak/setup.php(60): CSession::start()
#1 {main}
thrown in /usr/share/zabbix/bak/include/classes/core/CSession.php on line 46" while reading response header from upstream, client: 10.40.20.82, server: localhost, request: "GET /bak/setup.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "10.40.200.193"
###加入开机启动项
systemctl enable php-fpm nginx mariadb zabbix-server zabbix-agent