1. 程式人生 > >三臺伺服器搭建LNMP詳細步驟

三臺伺服器搭建LNMP詳細步驟

搭建說明

  • 已關閉防火牆和selinux;
  • 本次只寫將nginx、mysql、php分別部署到三臺伺服器之後的配置;
  • 將zabbix安裝到nignx伺服器中來驗證mysql與php是否連線;
  • 本次安裝除了mysql採用二進位制安裝,其餘全是原始碼編譯安裝;
  • 本次編譯除了php與zabbix編譯需修改引數,其他編譯或安裝詳情請參照LNMP架構搭建,所需包下載地址也請參照LNMP架構搭建
  • 本次搭建所用到的地址如下表:
安裝內容 IP地址
nginx、zabbix 192.168.91.128
mysql 192.168.91.129
php 192.168.91.130

配置

192.168.91.128伺服器安裝nginx配置

  • nginx編譯請參照LNMP架構搭建
  • 在地址為192.168.91.128這臺伺服器中,修改/usr/local/nginx/conf/nginx.conf配置檔案
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    server {
        listen       80;
        server_name  localhost;		##注:若想用域名訪問,請修改localhost
            location / {
            root   html;
            index  index.php index.html index.htm;		##注:新增index.php
            }
    }
        location ~ \.php$ {
            root           html;		##注:網頁檔案存放目錄,在本機的/usr/local/nginx/html/目錄下
            fastcgi_pass   192.168.91.130:9000;		##注:修改為php伺服器地址
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/$fastcgi_script_name;		##注:將$/scripts修改為根目錄(192.168.91.130伺服器的存放網頁檔案的目錄)
            include        fastcgi_params;
        }
}
  • 檢查配置檔案是否有誤
[[email protected] 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
  • 啟動服務並檢視埠
[[email protected] nginx]# nginx 
[[email protected] nginx]# ss -ntl
State       Recv-Q Send-Q               Local Address:Port                 Peer Address:Port 
LISTEN      0      128                              *:80                              *:*     
  • 在存放網頁的目錄中建立index.php檔案(與配置檔案中的“root”項對應)
[[email protected] nginx]# cat > html/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF

192.168.91.130伺服器安裝php配置

  • 安裝php所需的依賴包
[[email protected] ~]# yum -y install libxml2-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel gmp-devel libmcrypt-devel readline-devel libxslt-devel mhash-devel
  • 編譯php
./configure --prefix=/usr/local/php7 \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir=/usr \
--with-mysqli=mysqlnd \     ##路徑修改為mysqlnd
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql=mysqlnd \    ##修改為mysqlnd
--with-pdo-sqlite \
--with-pear \
--with-jpeg-dir \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2 \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip
[[email protected] php-7.2.10]# make -j 2 && make install
  • 編譯完成後,配置過程請參照LNMP架構搭建
  • 配置完成後,配置/usr/local/php7/etc/php-fpm.d/www.conf配置檔案,修改如下內容:
[[email protected] ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 192.168.91.130:9000		##注:監聽的IP地址改為伺服器的IP地址,埠為9000
listen.allowed_clients = 192.168.91.128		##注:允許192.168.91.128這臺伺服器訪問
  • 在192.168.91.130伺服器上建立/var/www/根目錄
[[email protected] ~]# mkdir /var/www
  • 在建立的目錄中建立index.php檔案,檔案的內容如下:
[[email protected] ~]# cat > /var/www/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
  • 啟動php-fpm服務,並檢視套接字
[[email protected] php-7.2.10]# service php-fpm start
Starting php-fpm  done
[[email protected] php-7.2.10]# ss -ntl
State       Recv-Q Send-Q               Local Address:Port                 Peer Address:Port 
LISTEN      0      128                 192.168.91.130:9000                            *:*     
  • 開啟瀏覽器,輸入nginx伺服器地址,看能否出現測試頁面
    在這裡插入圖片描述

192.168.91.129伺服器配置

  • 二進位制安裝mysql請參照LNMP架構搭建
  • 登入資料庫,建立zabbix使用者,建立zabbix資料庫,並授權zabbix允許zabbix
##注:修改密碼
mysql> set password=password('123456');      
Query OK, 0 rows affected, 1 warning (0.00 sec)

##注:建立zabbix資料庫
mysql> create database zabbix character set utf8 collate utf8_bin;      
Query OK, 1 row affected (0.01 sec)

##注:給zabbix使用者授權,允許zabbix使用者通過192.168.91.128進行登入並訪問zabbix資料庫
mysql> grant all on zabbix.* to [email protected] identified by '123456'; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

##注:給zabbix使用者授權,允許zabbix使用者通過192.168.91.102進行登入訪問zabbix資料庫
mysql> grant all on zabbix.* to [email protected] identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

## 注:重新整理許可權
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

192.168.91.128伺服器上安裝zabbix

  • 建立zabbix使用者和組
[[email protected] ~]# groupadd -r zabbix
[[email protected] ~]# useradd -r -M -s /sbin/nolgin -g zabbix zabbix
  • 安裝zabbix編譯所需的依賴包
[[email protected] ~]# yum -y install net-snmp-devel libevent-devel mariadb mariadb-devel libxml2-devel libcurl-devel
  • 編譯安裝zabbix
[[email protected] zabbix-3.4.14]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp-devel \
--with-libcurl \
--with-libxml2
[[email protected] zabbix-3.4.14]# make install
  • 將zabbix中自帶的三個mysql表匯入到zabbix資料庫中(注意先後順序)
[[email protected] ~]# cd zabbix-3.4.14/database/mysql/
[[email protected] mysql]# mysql -uzabbix -p123456 -h192.168.91.129 zabbix < schema.sql  
[[email protected] mysql]# mysql -uzabbix -p123456 -h192.168.91.129 zabbix < images.sql 
[[email protected] mysql]# mysql -uzabbix -p123456 -h192.168.91.129 zabbix < data.sql
  • 修改/usr/local/etc/zabbix_server.conf配置檔案
[[email protected] ~]# vim /usr/local/etc/zabbix_server.conf
# DBHost=localhost
DBHost=192.168.91.129    ##資料庫的IP地址或主機名

# DBName=
DBName=zabbix      ##資料庫的名字

# DBUser=
DBUser=zabbix       ##登入資料庫的使用者

# DBPassword=
DBPassword=123456     ##登入資料庫的密碼
  • 將zabbix網頁所需要的*.php檔案拷貝到/usr/local/nginx/html網頁主頁面下
[[email protected] zabbix-3.4.14]# cp -a frontends/php/* /usr/local/nginx/html/
cp: overwrite ‘/usr/local/nginx/html/index.php’? y
  • 啟動zabbix_server和zabbix_agentd服務
[[email protected] zabbix-3.4.14]# zabbix_server 
[[email protected] zabbix-3.4.14]# zabbix_agentd 
[[email protected] zabbix-3.4.14]# ss -ntl
State       Recv-Q Send-Q               Local Address:Port                 Peer Address:Port 
LISTEN      0      128                              *:10050                           *:*     
LISTEN      0      128                              *:10051                           *:*     
  • 將/root/zabbix-3.4.14/frontends/php/目錄下的所有檔案打包壓縮並傳輸到php伺服器上
[[email protected] zabbix-3.4.14]# tar -Jcf php.tar.xz frontends/php/*
[[email protected] zabbix-3.4.14]# ll php.tar.xz 
-rw-r--r--. 1 root root 3139992 Oct 24 19:12 php.tar.xz
[[email protected] zabbix-3.4.14]# scp php.tar.xz [email protected]:/var/www/ && rm -rf php.tar.xz 
[email protected]'s password: 
ph.tar.xz       

192.168.91.130伺服器

  • 將傳輸的打包檔案進行解壓,並下解壓目錄下的檔案移動到/var/www目錄下
[[email protected] ~]# cd /var/www
[[email protected] www]# ls
index.php  php.tar.xz
[[email protected] www]# tar -xf php.tar.xz && rm -rf php.tar.xz
[[email protected] www]# ls
frontends  index.php 
[[email protected] www]# mv frontends/php/* . && rm -rf frontends/
mv: overwrite ‘./index.php’? y
  • 修改/etc/php.ini配置檔案引數,並重啟服務
[[email protected] ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini 
[[email protected] ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini 
[[email protected] ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini 
[[email protected] ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini 
[[email protected] ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  • 將/var/www/目錄的許可權設定為777
[[email protected] ~]# ll /var/www -d
drwxr-xr-x. 13 root root 4096 Oct 24 19:33 /var/www
[[email protected] ~]# chmod -R 777 /var/www/conf
  • 開啟瀏覽器,輸入nginx伺服器的IP地址進行訪問
    在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述