1. 程式人生 > 實用技巧 >NGINX+PHP+POSTGRESQL+ZANNBX 5.X

NGINX+PHP+POSTGRESQL+ZANNBX 5.X

安裝前準備工作

系統及應用版本
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 = "*"來允許資料庫伺服器監聽來自任何主機的連線請求!