ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

NGINX+PHP+POSTGRESQL+ZANNBX 5.X

2020-08-07 20:00:24  阅读:213  来源: 互联网

标签:opt php POSTGRESQL nginx -- lnmp NGINX zabbix PHP


安装前准备工作

系统及应用版本
centos 8.3
nginx 1.18
php 7.4.8
postgresql 12.3

安装编译环境依赖包

dnf -y install gd gd-devel gcc gcc-c++ make automake pcre pcre-devel \
zlib zlib-devel openssl openssl-devel libxml2-devel libpng-devel curl-devel \
numactl langpacks-zh_CN.noarch glibc-common net-tools lrzsz readline readline-devel \
sqlite-devel libzip libzip-devel wget net-snmp-devel libevent-devel

安装包下载

wget http://nginx.org/download/nginx-1.18.0.tar.gz
wget https://www.php.net/distributions/php-7.4.8.tar.gz 
wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
wget https://ftp.postgresql.org/pub/source/v12.3/postgresql-12.3.tar.gz
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz

安装nginx

解压nginx源码包:

tar -zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0

编译安装:

./configure --prefix=/opt/lnmp/nginx
make && make install

创建nginx用户

useradd nginx
passwd nginx

将nginx命令加入环境变量

#设置环境变量
vim /etc/profile.d/nginx.sh
	PATH=$PATH:/opt/nginx/sbin
	export PATH
:wq!
source /etc/profile         #刷新配置
#修改nginx配置
grep "^\s*[^# \t].*$" /opt/lnmp/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

#启动测试nginx配置文件
/opt/lnmp/nginx/sbin/nginx -t -c /opt/lnmp/nginx/conf/nginx.conf
#启动nginx
/opt/lnmp/nginx/sbin/nginx -c /opt/lnmp/nginx/conf/nginx.conf
#停止nginx
/opt/lnmp/nginx/sbin/nginx -c /opt/lnmp/nginx/conf/nginx.conf -s stop

--------------------------------------
[root@angrymushroom-wk nginx-1.18.0]# nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@angrymushroom-wk nginx-1.18.0]# nginx 
[root@angrymushroom-wk nginx-1.18.0]# netstat -lnptu |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      97075/nginx: master 

源码安装postgresql

解压源码包

tar -zxf postgresql-12.3.tar.gz
cd postgresql-12.3

编译安装

./configure --prefix=/opt/lnmp/pgsql
make && make install

创建postgres用户

useradd postgres
passwd postgres

添加环境变量

vim /etc/profile.d/postgresql.sh ## 增加环境变量,不推荐直接在/etc/profile中添加,系统更新升级时会需要merge

cat /etc/profile.d/postgres.sh 
PATH=/opt/lnmp/pgsql/bin:$PATH
export PATH
:wq!
source /etc/profile ## 更新环境变量

将postgresql的库添加到全局

cat /etc/ld.so.conf.d/pgsql.conf 
/opt/lnmp/pgsql/lib
ldconfig              ##更新库
ldconfig -v |grep pg  ## 查看是否添加成功

初始化数据库

su - postgres
/opt/lnmp/pgsql/bin/initdb -D /opt/lnmp/pgsql/data

配置数据库允许远程登录

vim /opt/lnmp/pgsql/data/pg_hba.conf
 # IPv4 local connections:
 host    all             all             127.0.0.1/32            trust
 host  all    all    0.0.0.0/0    md5
vim /opt/lnmp/pgsql/data/postgresql.conf 
 listen_addresses = '*'
 port = 5432

postgresql运行命令

opt/lnmp/pgsql/bin/pg_ctl start -D /opt/lnmp/pgsql/data -l /opt/lnmp/pgsql/logs/server.log
/opt/lnmp/pgsql/bin/pg_ctl stop -D /opt/lnmp/pgsql/data 
/opt/lnmp/pgsql/bin/pg_ctl restart -D /opt/lnmp/pgsql/data 
/opt/lnmp/pgsql/bin/pg_ctl reload -D /opt/lnmp/pgsql/data 
/opt/lnmp/pgsql/bin/pg_ctl status -D /opt/lnmp/pgsql/data 

创建库和用户

psql -h 192.168.253.250 -U postgres                     ## postgres登录数据库
alter user postgres with password 'postgres';           ## 修改postgres密码
create user zabbixus with password 'zabbixpwd';         ## 创建用户设置密码
CREATE DATABASE zabbixdb OWNER zabbuxus;                ## 创建数据库并指定用户
grant ALL privileges on database zabbixdb to zabbixus;  ## 给用户赋数据库权限
psql -h 192.168.253.250 -U zabbix -W zabbixdb           ## 普通用户登录

安装php

php依赖包oniguruma 安装

tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
./autogen.sh
./configure --prefix=/usr --libdir=/lib64
make && make install

编译php

./configure --prefix=/opt/lnmp/php --with-config-file-path=/opt/lnmp/php/etc --with-pdo-pgsql=/opt/lnmp/pgsql/ --with-pgsql=/opt/lnmp/pgsql/ --with-freetype --with-jpeg --with-zlib --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --enable-mbstring --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-soap --with-gettext  --enable-fpm --with-zip --enable-gd --with-webp
make && make install

添加php配置文件并修改

cp php.ini-production /opt/lnmp/php/etc/php.ini
cp /opt/lnmp/php/etc/php-fpm.conf.default /opt/lnmp/php/etc/php-fpm.conf
cp /opt/lnmp/php/etc/php-fpm.d/www.conf.default  /opt/lnmp/php/etc/php-fpm.d/www.conf
vim /opt/lnmp/php/etc/php.ini
    date.timezone = Asia/Shanghai
    post_max_size 16M
    max_execution_time 300
    max_input_time 300
vim /etc/profile.d/php.sh
    PATH=/opt/lnmp/php/bin:/opt/lnmp/php/sbin:$PATH
    export PATH
vim /opt/lnmp/nginx/html/test.php    ## nginx访问php测试页面
    <?
    php phpinfo();
    ?>
vim /opt/lnmp/nginx/html/testpg.php  ## php连接postgresql数据库测试页面
<?php
$conn_string  =  "host=localhost port=5432 dbname=zabbixdb user=zabbixus password=zabbixpwd" ;  
$dbconn = pg_connect($conn_string);
if (!$dbconn)
            echo "连接失败!!!!!/r/n";
else
            echo "连接成功!!!!!/r/n";
    pg_close($dbconn);
?>

php运行命令

/opt/lnmp/php/sbin/php-fpm        ## 直接执行启动程序  pkill php-fpm 结束进程  php-fpm -t 检测配置文件是否有错
ps -ef |grep php-fpm
root       23942       1  0 Aug06 ?        00:00:00 php-fpm: master process (/opt/lnmp/php/etc/php-fpm.conf)
nobody     39558   23942  2 05:18 ?        00:00:47 php-fpm: pool www
nobody     40815   23942  2 05:37 ?        00:00:23 php-fpm: pool www
nobody     41628   23942  2 05:50 ?        00:00:05 php-fpm: pool www
netstat -lnptu |grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      23942/php-fpm: mast 

安装ZABBIX5

解压zabbix包

tar -zxf zabbix-5.0.2.tar.gz
cd zabbix-5.0.2

编译安装zabbix

./configure --prefix=/opt/zabbix --enable-server --enable-agent --with-postgresql=/opt/lnmp/pgsql/bin/pg_config --with-net-snmp --with-libcurl --with-libxml2   
make && make install

创建zabbix用户

useradd -d /opt/zabbix -s /sbin/nologin zabbix

导入zabbix表数据

 psql -d zabbixdb -U zabbixus  <database/postgresql/schema.sql
 psql -d zabbixdb -U zabbixus  <database/postgresql/images.sql
 psql -d zabbixdb -U zabbixus  <database/postgresql/data.sql
 psql -d zabbixdb -U zabbixus  <database/postgresql/timescaledb.sql  
 psql -d zabbixdb -U zabbixus  <database/postgresql/double.sql 

修改zabbix_server.conf和zabbix_agent.conf

vim /opt/zabbix/etc/zabbix_server.conf
    LogFile=/opt/zabbix/log/zabbix_server.log    ## 日志文件存放的路径
    PidFile=/opt/zabbix/zabbix_server.pid        ## pid文件存放的路径
    DBHost=localhost                             ## 数据库服务器地址
    DBName=zabbixdb                              ## 数据库名字
    DBUser=zabbixus                              ## 连接数据库的用户名
    DBPassword=zabbixpwd                         ## 连接数据库用户的密码
    #DBSocket=/var/lib/mysql/mysql.sock          ## 指定连接mysql的socket,mysql配置的client sock文件路径
    DBPort=5432                                  ## 数据库端口
    Timeout=10                                   ## 超时时间
    LogSlowQueries=3000                          ## 慢查询记录的时间
mkdir /opt/zabbix/log
chown zabbix.zabbix /opt/zabbix/log
vim /opt/zabbix/etc/zabbix_agentd.conf
    LogFile=/opt/zabbix/log/zabbix_agentd.log
    PidFile=/opt/zabbix/zabbix_agentd.pid
    Server=127.0.0.1                             ## 服务器IP                          
    ServerActive=127.0.0.1                       ## 服务器IP
    Hostname=angrymushroom-os801                 ## agent客户机主机名
vim /etc/profile.d/zabbix.sh 
    PATH=/opt/zabbix/bin:/opt/zabbix/sbin:$PATH
    export PATH
source /etc/profile
vim /etc/ld.so.conf.d/zabbix.conf 
    /opt/zabbix/lib
ldconfig
 

启动zabbix

/opt/zabbix/sbin/zabbix_agentd -c /opt/zabbix/etc/zabbix_agentd.conf
/opt/zabbix/sbin/zabbix_server -c /opt/zabbix/etc/zabbix_server.conf
## ps -ef |grep zabbix 查看进程  netstat -lnptu |grep zabbix 查看端口 pkill 结束进程

配置zabbix web页面

cp -a zabbix-5.0.2/ui/* /opt/lnmp/nginx/html/zabbix/   ## 将web页面cp到nginx的web根下的zabbix目录
## 在Windows C:\Windows\Fonts 下找一个 .ttf后缀的中文包上传到 /opt/lnmp/nginx/html/zabbix/assets/fonts/下
## 将上传的ttf语言包名替换成原本的语言包名
cd /opt/lnmp/nginx/html/zabbix/assets/fonts/
ls *.ttf
DejaVuSans.ttf  simhei.ttf       ## 这里将DejaVuSans.ttf mv 成DejaVuSans.ttf.bak 然后将simhei.ttf改为DejaVuSans.ttf
## 修改以后在浏览器打开  http://IP:Prot/zabbix 进入配置页面。第一页是个欢迎界面下一步之后会检查环境是否正确。如果有问题根据提示的信息修改然后刷新页面
## 最后完成的时候会让你下载一个zabbix.conf.php的配置文件 把它上传到对应目录 然后刷新页面即可进入zabbix登录页面
## 登录后可以在 user settings里设置中文语言

postgresql补充

列出数据库名(查看已有的数据库)

 \l  或  select * from pg_database;

切换数据库

\c 数据库名

列出表名

 \d 不加参数 或
SELECT  tablename  FROM  pg_tables  WHERE  tablename  NOT  LIKE  'pg%'  AND tablename NOT LIKE 'sql_%' ORDER  BY  tablename;

将数据库的所有权限付给用户

postgres=# GRANT ALL PRIVILEGES ON DATABASE zabbixdb TO zabbixus;  #将数据库 zabbixdb 权限授权于 zabbixus 但此时用户还是没有读写权限,需要继续授权表
GRANT
postgres=# \c zabbixdb;
GRANT ALL PRIVILEGES ON all tables in schema public TO zabbixus;
#注意,该sql语句必须在所要操作的数据库里执行
#这一句是将当前数据库下 public schema 的表都授权于 zabbixus
#如果要单独一个权限以及单独一个表,则:
GRANT SELECT ON TABLE mytable TO zabbixus;
------------
postgres=# grant all on database testdb to zabbixus;              #将testdb所有权限赋值给zabbixus
GRANT

导入整个数据库

psql -U username databasename < /data/dum.sql -- 用户名和数据库名

设置用户远程登录

[root@angrymushroom-wk ~]# psql -h 192.168.254.128 -U zabbixus -d zabbixdb
    psql: error: could not connect to server: could not connect to server: 拒绝连接
        Is the server running on host "192.168.254.128" and accepting
        TCP/IP connections on port 5432?

安装PostgreSQL数据库之后,默认是只接受本地访问连接。如果想在其他主机上访问PostgreSQL数据库服务器,就需要进行相应的配置。

  配置远程连接PostgreSQL数据库的步骤很简单,只需要修改data目录下的pg_hba.confpostgresql.conf配置文件。

  pg_hba.conf :配置对数据库的访问权限;

  postgresql.conf :配置PostgreSQL数据库服务器的相应的参数。

下面介绍具体配置的步骤:

修改 pg_hba.conf 文件,配置用户的访问权限(#开头的行是注释内容):  

[root@angrymushroom-wk ~]# vim /opt/pgsql12/data/pg_hba.conf
     # TYPE DATABASE  USER    CIDR-ADDRESS     METHOD
     # "local" is for Unix domain socket connections only
     local all    all                     trust
     # IPv4 local connections:
     host  all    all    127.0.0.1/32     trust
     host  all    all    192.168.1.0/24   md5        ## 设置允许某个网段
     host  all    all    0.0.0.0/0        md5        ## 设置允许全部网段
     # IPv6 local connections:
     host  all    all    ::1/128       trust

其中,第6条是新添加的内容,表示允许网段192.168.1.0上的所有主机使用所有合法的数据库用户名访问数据库,并提供加密的密码验证。其中,数字24是子网掩码,表示允许192.168.1.0--192.168.1.255的计算机访问。

修改 ostgresql.conf 文件,将数据库服务器的监听模式修改为监听所有主机发出的连接请求。

  定位到#listen_addresses = "localhost"。PostgreSQL安装完成后,默认只接受来自本机localhost的连接请求。

  将行开头都#去掉,将行内容修改为listen_addresses = "*"来允许数据库服务器监听来自任何主机的连接请求!

标签:opt,php,POSTGRESQL,nginx,--,lnmp,NGINX,zabbix,PHP
来源: https://www.cnblogs.com/AngryMushroom/p/13454939.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有