工作机制

  • 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