nginx:192.168.31.100
php:192.168.31.100
zabbix:192.168.31.100
mariadb:192.168.31.101
准备
#两台服务器都关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
安装nginx
- 下载nginx源码包,安装nginx依赖包
wget -P /usr/local/src http://nginx.org/download/nginx-1.16.1.tar.gz
yum -y install gcc gcc-c++ make pcre zlib openssl openssl-devel
- 解压nginx源码包,创建www用户,开始编译安装
cd /usr/local/src
tar -zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1
useradd www -s /sbin/nologin
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
- 创建软链接文件
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
安装php7.3
yum -y install epel-release
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum -y install yum-utils
yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll php73-php-xmlwriter
#修改php启动用户为www
sed -i 's/user = apache/user = www/g;s/group = apache/group = www/g' /etc/opt/remi/php73/php-fpm.d/www.conf
#删除空行和注释行
sed -i '/^;/d;/^$/d;/ *;/d;/^[ \t]*$/d' /etc/opt/remi/php73/php.ini
#修改php默认配置
sed -i 's/post_max_size = 8M/post_max_size = 16M/g;s/max_execution_time = 30/max_execution_time = 300/g;s/max_input_time = 60/max_input_time = 300/g' /etc/opt/remi/php73/php.ini
sed -i '/max_input_time = 300/a date.timezone = Asia/Shanghai' /etc/opt/remi/php73/php.ini
安装zabbix
- 下载zabbix源码包,安装依赖包
wget -P /usr/local/src https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
yum -y install curl curl-devel net-snmp net-snmp-devel perl-DBI gcc-c++ gcc make libevent libevent-devel mariadb mysql-devel
- 创建zabbix 组和用户
groupadd zabbix
useradd -g zabbix zabbix
usermod -s /sbin/nologin zabbix
- 编译安装
cd /usr/local/src
tar -zxf zabbix-5.0.2.tar.gz
cd zabbix-5.0.2
./configure --prefix=/usr/local/zabbix/ --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl
make
make install
ln -s /usr/local/zabbix/sbin/zabbix_server /usr/local/sbin/
cp misc/init.d/tru64/zabbix_server /etc/init.d/zabbix_server
chmod o+x /etc/init.d/zabbix_server
- 修改zabbix配置文件
cp /usr/local/zabbix/etc/zabbix_server.conf /usr/local/zabbix/etc/zabbix_server.conf.bak
sed -i '/^#/d;/^$/d;/ *#/d;/^[ \t]*$/d' /usr/local/zabbix/etc/zabbix_server.conf
sed -i '/DBName=zabbix/i DBHost=192.168.31.101' /usr/local/zabbix/etc/zabbix_server.conf
sed -i '/DBUser=zabbix/a DBPassword=zabbix' /usr/local/zabbix/etc/zabbix_server.conf
安装mariadb数据库
yum -y install mariadb mariadb-server mariadb-client mariadb-devel
systemctl start mariadb
mysql_secure_installation
systemctl enable mariadb
mysql -u root -p
Enter password:
MariaDB [(none)]> create database zabbix charset=utf8;
MariaDB [(none)]> grant all on zabbix.* to zabbix@'%' identified by 'zabbix';
MariaDB [(none)]> flush privileges;
整合
- 修改nginx配置文件
cat > /usr/local/nginx/conf/nginx.conf <<EOF
user www www;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
access_log /usr/local/nginx/logs/access.log;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 128k;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
large_client_header_buffers 4 16k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
include vhosts/*.conf;
}
EOF
- 创建zabbix虚拟主机
mkdir -p /usr/local/nginx/conf/vhosts
vim /usr/local/nginx/conf/vhosts/zabbix.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/local/nginx/html/zabbix;
location / {
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
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;
}
}
- 创建nginx发布目录
mkdir /usr/local/nginx/html/zabbix
- 复制zabbix web文件到发布目录
cd zabbix-5.0.2
cp -Rf ui/* /usr/local/nginx/html/zabbix/
- 导入zabbix SQL文件到数据库中
cd zabbix-5.0.2
mysql -h 192.168.31.101 -uzabbix -pzabbix zabbix <database/mysql/schema.sql
mysql -h 192.168.31.101 -uzabbix -pzabbix zabbix <database/mysql/images.sql
mysql -h 192.168.31.101 -uzabbix -pzabbix zabbix <database/mysql/data.sql
- 启动nginx、php和zabbix
nginx
systemctl restart php73-php-fpm
/etc/init.d/zabbix_server start
访问http://IP/setup.php
错误
访问网页显示 http 500
- 查看日志
查看nginx 错误日志,php执行函数session_start出错,原因是没有目录或权限问题
2021/03/29 19:54:45 [error] 78862#0: *11 FastCGI sent in stderr: "PHP message: PHP Warning: session_start(): open(/var/opt/remi/php73/lib/php/session/sess_9q7gcmffnbde2gk0br6eqpbohq, O_RDWR) failed: Permission denied (13) in /usr/local/nginx/html/zabbix/include/classes/core/CSession.php on line 45PHP message: PHP Warning: session_start(): Failed to read session data: files (path: /var/opt/remi/php73/lib/php/session) in /usr/local/nginx/html/zabbix/include/classes/core/CSession.php on line 45PHP message: PHP Fatal error: Uncaught Exception: Cannot start session. in /usr/local/nginx/html/zabbix/include/classes/core/CSession.php:46
Stack trace:
#0 /usr/local/nginx/html/zabbix/setup.php(66): CSession::start()
#1 {main}
thrown in /usr/local/nginx/html/zabbix/include/classes/core/CSession.php on line 46" while reading response header from upstream, client: 192.168.31.1, server: localhost, request: "GET /setup.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.31.100"
- 解决
[root@localhost html]# ll /var/opt/remi/php73/lib/php/session
total 4
-rw-------. 1 apache apache 33 Mar 29 20:07 sess_9q7gcmffnbde2gk0br6eqpbohq
查看有这个目录,但目录所属为apache用户,修改所属
chown -R root:www /var/opt/remi/php73/lib/php/session
重新启动php和nginx
再次访问可看到恢复正常
- 数据库无法连接
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hDncGZ3r-1626160904361)(https://itrochoid.top/upload/2021/04/image-1002d059787a4320997052224faf3656.png)] - 解决
cd /usr/local/nginx/html/zabbix/conf
mv zabbix.conf.php.example zabbix.conf.php
vim zabbix.conf.php #修改数据库为正确的IP、用户和密码