1. 程式人生 > >LNMP+Redis架構部署

LNMP+Redis架構部署

工作機制

  • L(Linux)N(Nginx)M(Mysql)P(PHP)架構想必大家都知道,LNMP架構主要作用是讓前端服務與後端儲存以及後端的一下服務進行連線起來,來實現php程式的動態請求。

   而今天我們又在LNMP架構上面加一個Redis程式,而Redis在整個架構中起到了一個數據快取的作用。

  • LNMP+Redis工作機制:當用戶通過瀏覽器訪問網站時,並使用賬號密碼進行登陸時,此時會向Redis發出查詢請求,若Redis快取中沒有相關資訊,則php會查詢mysql資料庫中的相關資訊,然後將相關資訊快取在redis中;在下次此使用者訪問時,php無需再從mysql資料庫中讀取資料,直接從redis中讀取快取並將資料返回,這樣就可以減少資料庫的讀取壓力。
  • 下面是簡單的工作機制示意圖

 

系統環境描述

  • Linux系統版本:我這裡使用的是Ubuntu系統,大家可以選用不同的Linux版本
   jia@uduntu:~$ lsb_release -a

    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 19.10
    Release: 19.10
    Codename: eoan

  • Nginx軟體版本
  nginx/1.16.1 (Ubuntu)
  •  PHP軟體版本
   7.3.11-0ubuntu0.19.10.1 amd64
  • MariaDB軟體版本
   1:10.3.17-1 all

 

  • Redis軟體版本
   5:5.0.5-2build1 all

 

部署Nginx

Nginx描述:Nginx (engine x) 是一個高效能的HTTP和反向代理web伺服器,同時也提供了MAP/POP3/SMTP服務。Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,在BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力在同類型的網頁伺服器中表現較好。

安裝

安裝Nginx軟體包有很多種方法比如:RPM包安裝、編譯安裝等,我在這裡軟體就全部使用RPM進行安裝了

jia@uduntu:~$ sudo apt -y install nginx

    Setting up fonts-dejavu-core (2.37-1) ...
    Setting up libjpeg-turbo8:amd64 (2.0.3-0ubuntu1) ...
    Setting up libjpeg8:amd64 (8c-2ubuntu8) ...    
    Setting up libnginx-mod-mail (1.16.1-0ubuntu2) ...
    Setting up fontconfig-config (2.13.1-2ubuntu2) ...
    Setting up libnginx-mod-stream (1.16.1-0ubuntu2) ...
    Setting up libtiff5:amd64 (4.0.10+git191003-1) ...
    Setting up libfontconfig1:amd64 (2.13.1-2ubuntu2) ...
    Setting up libgd3:amd64 (2.2.5-5.2) ...
    Setting up libnginx-mod-http-image-filter (1.16.1-0ubuntu2) ...
    Setting up nginx-core (1.16.1-0ubuntu2) ...
    Setting up nginx (1.16.1-0ubuntu2) ...
    Processing triggers for ufw (0.36-1ubuntu3) ...
    Processing triggers for systemd (242-7ubuntu3) ...
    Processing triggers for man-db (2.8.7-3) ...
    Processing triggers for libc-bin (2.30-0ubuntu2) ...
jia@uduntu:~$

 

出現上面字元即為安裝成功

啟動

程式啟動有兩種方法

  1. 作為系統服務進行啟動,啟動方法:

Ubuntu以及rhel7以上版本使用下面方式:

    systemctl  start nginx        \\啟動Nginx
    systemctl  stop  nginx        \\停止Nginx
    systemctl  restart  nginx        \\重新啟動Nginx

 

rhel7一下版本使用下面方式:

    server  nginx  start        \\啟動Nginx
    server  nginx  stop            \\停止Nginx
    server  nginx  restart        \\重新啟動Nginx

 

  1. 使用Nginx啟動指令碼進行控制Nginx啟停:
    sh nginx        \\啟動Nginx
    停止nginx可以使用結束程序的方式進行停止

 

測試並訪問

接下來讓我們啟動nginx並進行訪問:

jia@uduntu:~$ systemctl start nginx        \\我這裡使用的非root使用者所以要求輸入密碼
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to start 'nginx.service'.
    Authenticating as: jia
    Password: 
    ==== AUTHENTICATION COMPLET
jia@uduntu:~$

 

啟動成功後訪問:
訪問地址:

    http://Server IP Address

 

看到下面內容證明安裝並啟動成功:

部署PHP

PHP描述:PHP即“超文字前處理器”,是一種通用開源指令碼語言。PHP是在伺服器端執行的指令碼語言,與C語言類似,是常用的網站程式語言。PHP獨特的語法混合了C、Java、Perl以及 PHP 自創的語法。利於學習,使用廣泛,主要適用於Web開發領域。

安裝

    jia@uduntu:~$ sudo apt -y install php php7.3-fpm
    [sudo] password for jia:             //這裡正常輸入密碼
        \\安裝最後出現下面顯示錶示安裝成功
    Creating config file /etc/php/7.3/mods-available/json.ini with new version
    Setting up php7.3-readline (7.3.11-0ubuntu0.19.10.1) ...

    Creating config file /etc/php/7.3/mods-available/readline.ini with new version
    Setting up php7.3-cli (7.3.11-0ubuntu0.19.10.1) ...
    update-alternatives: using /usr/bin/php7.3 to provide /usr/bin/php (php) in auto mode
    update-alternatives: using /usr/bin/phar7.3 to provide /usr/bin/phar (phar) in auto mode
    update-alternatives: using /usr/bin/phar.phar7.3 to provide /usr/bin/phar.phar     (phar.phar) in auto mode

    Creating config file /etc/php/7.3/cli/php.ini with new version
    Setting up php7.3-fpm (7.3.11-0ubuntu0.19.10.1) ...

    Creating config file /etc/php/7.3/fpm/php.ini with new version
    Created symlink /etc/systemd/system/multi-user.target.wants/php7.3-fpm.service →     /lib/systemd/system/php7.3-fpm.service.
    Setting up php7.3 (7.3.11-0ubuntu0.19.10.1) ...
    Setting up php (2:7.3+69ubuntu2) ...
    Processing triggers for man-db (2.8.7-3) ...
    Processing triggers for systemd (242-7ubuntu3) ...

 

啟動

上面已經給大家說過Nginx的啟動方法了,php啟動方法與Nginx基本一樣

jia@uduntu:~$ systemctl start  php7.3-fpm
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to start 'php7.3-fpm.service'.
    Authenticating as: jia
    Password:             \\輸入密碼
    ==== AUTHENTICATION COMPLETE ===
jia@uduntu:~$ 

 

啟動成功,可以使用檢視程序的方式進行檢視

jia@uduntu:~$ ps uax | grep php            \\下面程序表示php執行程序
    root      10346  0.0  2.5 193732 17384 ?        Ss   08:29   0:00 php-fpm: master process     (/etc/php/7.3/fpm/php-fpm.conf)
    www-data  10357  0.0  0.9 194044  6348 ?        S    08:29   0:00 php-fpm: pool www
    www-data  10358  0.0  0.9 194044  6348 ?        S    08:29   0:00 php-fpm: pool www
    jia       10572  0.0  0.1   6296   920 pts/0    S+   08:33   0:00 grep --color=auto php
jia@uduntu:~$

 

配置Nginx

php啟動成功後下面配置Nginx,讓Nginx接收到的php請求轉交給php伺服器進行解析
nginx配置檔案:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html index.php ;        \\此處需要在後面新增index.php
        server_name _;
        location / {
        try_files $uri $uri/ =404;
        }
        location ~ \.php$ {            \\取消註釋
               include snippets/fastcgi-php.conf;        \\取消註釋
               fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;        \\這一行和下面一行指的是php偵聽方式,檢視php是偵聽那種方式然後取消註釋哪一行
               fastcgi_pass 127.0.0.1:9000;            \\
        }

在php-fpm配置檔案種檢視php的偵聽方式:

    listen = /run/php/php7.3-fpm.sock        \\這種偵聽方式適用於本地php
    listen = 127.0.0.1:9000        \\這種偵聽方式適用於遠端PHP

 

看你的那個php是那種方式偵聽的,然後將nginx配置檔案中的一行取消註釋,然後再重新啟動nginx使改動生效

測試

預設網頁程式碼的存放位置:

    /var/www/html

 測試只需要將index,html該名為index.php,內容更改為你的php程式碼
我的php程式碼內容為:

    <?php
        phpifo();
    ?>

 

下面來訪問一下,訪問地址不變

只要可以解析PHP程式碼了就表示配置安裝成功

部署MariaDB

部署MariaDBMariaDB描述:MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社群在維護,採用GPL授權許可 MariaDB的目的是完全相容MySQL,包括API和命令列,使之能輕鬆成為MySQL的代替品。MariaDB基於事務的Maria儲存引擎,替換了MySQL的MyISAM儲存引擎,它使用了Percona的 XtraDB,InnoDB的變體,分支的開發者希望提供訪問即將到來的MySQL 5.4 InnoDB效能。這個版本還包括了 PrimeBase XT (PBXT) 和 FederatedX儲存引擎。

安裝

 jia@uduntu:~$ sudo apt -y install mariadb-server
    Setting up mariadb-client-10.3 (1:10.3.17-1) ...
    Setting up libdbd-mysql-perl:amd64 (4.050-2build1) ...
    Setting up libhtml-parser-perl (3.72-3build2) ...
    Setting up mariadb-server-10.3 (1:10.3.17-1) ...
    Created symlink /etc/systemd/system/mysql.service → /lib/systemd/system/mariadb.service.
    Created symlink /etc/systemd/system/mysqld.service → /lib/systemd/system/mariadb.service.
    Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /lib/systemd/system/mariadb.service.
    Setting up libhttp-message-perl (6.18-1) ...
    Setting up libcgi-pm-perl (4.44-1) ...
    Setting up libhtml-template-perl (2.97-1) ...
    Setting up mariadb-server (1:10.3.17-1) ...
    Setting up libcgi-fast-perl (1:2.15-1) ...
    Processing triggers for systemd (242-7ubuntu3) ...
    Processing triggers for man-db (2.8.7-3) ...
    Processing triggers for libc-bin (2.30-0ubuntu2) ...
jia@uduntu:~$            \\出現上面程式碼表示安裝成功

 

啟動

同Nginx:

 jia@uduntu:~$ systemctl start mariadb
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to start 'mariadb.service'.
    Authenticating as: jia
    Password:         \\此處輸入密碼
    ==== AUTHENTICATION COMPLETE ===
 jia@uduntu:~$ 

 

檢視是否啟動成功:

 jia@uduntu:~$ ps uax | grep mysqld
    mysql     11669  0.2 11.5 1713056 78488 ?       Ssl  09:17   0:00 /usr/sbin/mysqld
    jia       12614  0.0  0.1   6296   924 pts/0    S+   09:23   0:00 grep --color=auto mysqld
 jia@uduntu:~$ 

 

初始化資料庫:

 jia@uduntu:~$ sudo mysql_secure_installation 
     \\下面是初始化過程
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
jia@uduntu:~$ 

 

初始化完成後,可直接使用自帶的mysql客戶端進行連線

 jia@uduntu:~$ sudo mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 56
    Server version: 10.3.17-MariaDB-1 Ubuntu 19.10

    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | ifnormation_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.000 sec)

     MariaDB [(none)]> 

配置php支援

安裝php連線資料庫的中介軟體:php-mysql

 jia@uduntu:~$ sudo apt -y install php-mysql
    Preparing to unpack .../php-mysql_2%3a7.3+69ubuntu2_all.deb ...
    Unpacking php-mysql (2:7.3+69ubuntu2) ...
    Setting up php7.3-mysql (7.3.11-0ubuntu0.19.10.1) ...

    Creating config file /etc/php/7.3/mods-available/mysqlnd.ini with new version

    Creating config file /etc/php/7.3/mods-available/mysqli.ini with new version

    Creating config file /etc/php/7.3/mods-available/pdo_mysql.ini with new version
    Setting up php-mysql (2:7.3+69ubuntu2) ...
    Processing triggers for php7.3-fpm (7.3.11-0ubuntu0.19.10.1) ...
jia@uduntu:~$             //安裝成功

 

配置php.ini檔案,將配置檔案中extension=mysqli這一行取消註釋就可以了,然後重新啟動php-fpm

 jia@uduntu:/etc/php/7.3/fpm$ systemctl restart php7.3-fpm
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to restart 'php7.3-fpm.service'.
    Authenticating as: jia
    Password:             \\輸入密碼
    ==== AUTHENTICATION COMPLETE ===
 jia@uduntu:/etc/php/7.3/fpm$ 

測試

測試php是否可使用資料庫,最好的方法就是使用php程式碼寫一個連線資料庫就可以了
程式碼如下:

    <?php
        $host = "localhost";    //mysql主機
        $user = "root";            //mysql使用者
        $passwd = "redhat";        //mysql密碼
        $conn = new mysqli($host,$user,$passwd);
        if (!$conn){
            die("連線資料庫失敗");
        }
        echo "連線資料庫成功";        
    ?>

 

顯示資料庫連線成功示安裝成功

部署Redis

Redis描述:Redis(全稱:Remote Dictionary Server 遠端字典服務)是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value資料庫,並提供多種語言的API。

安裝

jia@uduntu:~$ sudo apt -y install redis                \\出現下面提示表示安裝成功
    Setting up lua-cjson:amd64 (2.1.0+dfsg-2.1) ...
    Setting up libatomic1:amd64 (9.2.1-9ubuntu2) ...
    Setting up lua-bitop:amd64 (1.0.2-5) ...
    Setting up liblua5.1-0:amd64 (5.1.5-8.1build3) ...
    Setting up libhiredis0.14:amd64 (0.14.0-3) ...
    Setting up redis-tools (5:5.0.5-2build1) ...
    Setting up redis-server (5:5.0.5-2build1) ...
    Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-                                        server.service.
    Created symlink /etc/systemd/system/multi-user.target.wants/redis-server.service →     /lib/systemd/system/redis-server.service.
    Setting up redis (5:5.0.5-2build1) ...
    Processing triggers for systemd (242-7ubuntu3) ...
    Processing triggers for man-db (2.8.7-3) ...
    Processing triggers for libc-bin (2.30-0ubuntu2) ...
jia@uduntu:~$ 

 

啟動

啟動等同於Nginx啟動

jia@uduntu:~$ sudo systemctl start redis

 

啟動成功後進行檢查

jia@uduntu:~$ sudo netstat -anpl |  grep redis
    tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN          2094/redis-server 1 
    tcp6       0      0 ::1:6379                :::*                    LISTEN          2094/redis-server 1 
jia@uduntu:~$ 

可以看到redis埠(6379)就證明啟動成功
下面讓我們使用redis客戶端測試一下:

jia@uduntu:~$ sudo redis-cli         \\啟動redis客戶端
    127.0.0.1:6379> set test "hellow word"            \\設定變數
        OK
    127.0.0.1:6379> get test        \\輸出變數
        "hellow word"
    127.0.0.1:6379> del test        \\刪除變數
        (integer) 1
    127.0.0.1:6379> get test    
        (nil)
    127.0.0.1:6379> 

 

上面測試可以看出redis安裝成功,並啟動成功

配置php支援

安裝php連線redis中介軟體

jia@uduntu:~$ sudo apt -y install php-redis                \\輸出以下內容表示安裝成功
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following additional packages will be installed:
      php-igbinary
    The following NEW packages will be installed:
      php-igbinary php-redis
    0 upgraded, 2 newly installed, 0 to remove and 18 not upgraded.
    Needto get 239 kB of archives.
    After this operation, 1,052 kB of additional disk space will be used.
    Get:1 http://cn.archive.ubuntu.com/ubuntu eoan/universe amd64 php-igbinary amd64 3.0.0-    1build1 [101 kB]
    Get:2 http://cn.archive.ubuntu.com/ubuntu eoan/universe amd64 php-redis amd64 5.0.2+4.3.0-2build1 [138 kB]
    Fetched 239 kB in 13s (19.0 kB/s)                                                                                  
    Selecting previously unselected package php-igbinary.
    (Reading database ... 70748 files and directories currently installed.)
    Preparing to unpack .../php-igbinary_3.0.0-1build1_amd64.deb ...
    Unpacking php-igbinary (3.0.0-1build1) ...
    Seleting previously unselected package php-redis.
    Preparing to unpack .../php-redis_5.0.2+4.3.0-2build1_amd64.deb ...
    Unpacking php-redis (5.0.2+4.3.0-2build1) ...
    Setting up php-igbinary (3.0.0-1build1) ...
    Setting up php-redis (5.0.2+4.3.0-2build1) ...
jia@uduntu:~$ 

 

安裝成功後,配置php支援redis
找到php.ini這個配置檔案,新增下面配置,找到exension=mysql這一條,然後將下面程式碼貼上到下一行

    extension=redis.so

 

測試

測試php是否可以使用redis,下面頁面簡單既可以進行測試

<?php
   $redis = new Redis();        \\redis連線引數
   $redis->connect('127.0.0.1', 6379);        \\括號內第一項是指的redis server ip,第二項是 redis port
   echo "Connection to server successfully </br>";
   echo "Server is running: " . $redis->ping();
?>

 

將/var/www/html下面的index.php檔案內容替換為上面內容,然後直接訪問即可
連線地址:

    http://nginx server ip

 

顯示下面內容表示連線成功

Connection to server successfully
Server is running: 1