LNMP+Redis架構部署

工做機制

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

   而今天咱們又在LNMP架構上面加一個Redis程序,而Redis在整個架構中起到了一個數據緩存的做用。php

  • 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: eoanhtml

  • 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進行安裝了mysql

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:
~$

 

出現上面字符即爲安裝成功nginx

啓動

程序啓動有兩種方法git

  1. 做爲系統服務進行啓動,啓動方法:

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

    systemctl  start nginx        \\啓動Nginx
    systemctl  stop  nginx        \\中止Nginx
    systemctl  restart  nginx        \\從新啓動Nginx

 

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

    server  nginx  start        \\啓動Nginx
    server  nginx  stop            \\中止Nginx
    server  nginx  restart        \\從新啓動Nginx

 

  1. 使用Nginx啓動腳本進行控制Nginx啓停:
    sh nginx        \\啓動Nginx
    中止nginx能夠使用結束進程的方式進行中止

 

測試並訪問

接下來讓咱們啓動nginx並進行訪問:sql

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

 

相關文章
相關標籤/搜索