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.19000        \\這種偵聽方式適用於遠程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

 

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※想知道網站建置網站改版該如何進行嗎?將由專業工程師為您規劃客製化網頁設計後台網頁設計

※不管是台北網頁設計公司台中網頁設計公司,全省皆有專員為您服務

※Google地圖已可更新顯示潭子電動車充電站設置地點!!

※帶您來看台北網站建置台北網頁設計,各種案例分享

您可能也會喜歡…