下载 nginx-1.16.0.tar.gz 并安装
tar -xvf nginx-1.16.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.16.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
安装完后,查看一下nginx的版本和配置参数:
[root@zabbix_nginx ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
nignx安装完成。
也可以采用 nginx rpm 安装方式。
rpm -ivh nginx-1.16.0-1.el7.ngx.x86_64.rpm
启动nginx 服务
systemctl start nginx.service
查看nginx 进程
[root@zabbix_nginx conf.d]# ps -ef|grep nginx
root 65151 1 0 23:18 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 65152 65151 0 23:18 ? 00:00:00 nginx: worker process
root 65598 56021 0 23:25 pts/0 00:00:00 grep --color=auto nginx
下面开始zabbix替换默认web服务器httpd为ngnix(rpm方式安装的)。
zabbix本身环境之前是采用的lamp环境的 rpm 包去安装zabbix的。现在要换成nginx作为web服务。
替换思路:zabbix的web服务是用php写的,httpd只是一个web服务器。有了替换思路我们就进行下一步,我们首先找到php程序存放的目录。
/etc/httpd/conf.d/zabbix.conf 文件完整内容如下:
[root@zabbix_nginx conf.d]# more zabbix.conf
#
# Zabbix monitoring system php web frontend
#
Alias /zabbix /usr/share/zabbix
<Directory "/usr/share/zabbix">
Options FollowSymLinks
AllowOverride None
Require all granted
<IfModule mod_php5.c>
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 max_input_vars 10000
php_value always_populate_raw_post_data -1
# php_value date.timezone Europe/Riga
php_value date.timezone Asia/Shanghai
</IfModule>
</Directory>
<Directory "/usr/share/zabbix/conf">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/app">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/include">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/local">
Require all denied
</Directory>
找到zabbix.conf 并打开文件 /etc/httpd/conf.d/zabbix.conf, 根据路径来看不难判断这个文件应该就是httpd 配置文件,打开文件根据 Directory 可以判断 /usr/share/zabbix 为程序所在目录。
找到zabbix 程序所在目录后,我们就着手配置 nginx 就好了,进入 nginx 的配置目录并打开 /etc/nginx/conf.d/default.conf文件(或者另外创建一个zabbix.conf 的文件)------ 可以先备个份:cp default.conf default.conf.bak
安装好lnmp环境,nginx 是基于 php-fpm, rhel7.4 只有php相关rpm包,但没有php-fpm的rpm包,所以需要自己下载相应版本的 php-fpm 的 rpm包并安装。
安装php-fpm:
yum list|grep php
yum -y install php72w-fpm
默认 /etc/nginx/conf.d/default.conf 完整内容如下:
[root@zabbix_nginx conf.d]# more default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
zabbix 不想放在网站根目录下,避免和网站应用混在一起,这样zabbix的目录就放在别处,在 apache里,有 alias,比较方便,在nginx下没有虚拟目录概念的,是用 location 配合 alias 使用,但使用 alias 标签的目录块中不能使用rewrite 的break。
下面先说简单的配置方式:
先备个份:
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
然后编辑 default.conf 为下面的内容:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# 采用别名zabbix方式:http://IP/zabbix,这样去访问,就不用nginx默认/目录了
location /zabbix {
# /usr/share/zabbix 是zabbix 前端的php文件所在目录
alias /usr/share/zabbix;
index index.html index.htm index.php;
}
# 设置下面几个目录不允许外部访问
location ^~ /conf {
deny all;
}
location ^~ /app {
deny all;
}
location ^~ /include {
deny all;
}
location ^~ /local {
deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# 配置nginx 和 php-fpm 通信
# 我们使用端口的形式来进行通讯
# 前面注释去掉并修改成你需要的
location ~ ^/zabbix/.+\.php$ {
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
配置好之后保存文件
启动服务:
systemctl start php-fpm
systemctl restart nginx
systemctl restart zabbix-server
设置开机启动:
systemctl enable php-fpm
systemctl enable zabbix-server
systemctl enable nginx
访问zabbix服务: http://IP/zabbix
输入用户名,密码登进去之后,会发现以前的zabbix 数据都在。
注意:
如果登进去之后,报错,此时多半是 php.ini 没配好。如时区没有修改 等等。编辑php.ini ,修改好了就ok了。
比如:
date.timezone = Asia/Shanghai