LNMP+Redis工作机制:当用户通过浏览器访问网站时,并使用账号密码进行登陆时,此时会向Redis发出查询请求,若Redis缓存中没有相关信息,则php会查询mysql数据库中的相关信息,然后将相关信息缓存在redis中;在下次此用户访问时,php无需再从mysql数据库中读取数据,直接从redis中读取缓存并将数据返回,这样就可以减少数据库的读取压力。
查看系统版本内核等信息:
wangqd@wangqd:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
LSB是Linux Standard Base的缩写, lsb_release命令 用来显示LSB和特定版本的相关信息。如果使用该命令时不带参数,则默认加上-v参数。
-v 显示版本信息。
-i 显示发行版的id。
-d 显示该发行版的描述信息。
-r 显示当前系统是发行版的具体版本号。
-c 发行版代号。
-a 显示上面的所有信息。
-h 显示帮助信息。
安装nginx:
sudo apt-get install nginx
查看nginx版本号:
$ nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
安装php7.2:
sudo apt-get install php7.2
查看版本:
$ php7.2 -v
PHP 7.2.24-0ubuntu0.18.04.4 (cli) (built: Apr 8 2020 15:45:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.4, Copyright (c) 1999-2018, by Zend Technologies
安装mariadb:
sudo apt-get install mariadb.server
查看mariadb版本号:
mariadb -V
mariadb Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
安装php相关模块:
sudo apt-get install php7.2-fpm php7.2-mysql php7.2-gd php7.2-xml
php7.2-curl php7.2-zip
PHP(超文本预处理器)的解释器是php-cgi。php-cgi只是个CGI程序,他自己本身只能解析请求,返回结果,不对进程进行管理,所以就出现了一些能够调度php-cgi进程的程序,比如说由lighthttpd分离出来的spawn-fcgi。PHP-FPM也是这么个东西,在长时间的发展后,逐渐得到了大家的认可,现在也越来越流行。
PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,对于PHP 5.3.3之前的php来说,是一个补丁包 ,旨在将FastCGI进程管理整合进PHP包中。如果你使用的是PHP5.3.3之前的PHP的话,就必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。
启动:
sudo systemctl restart nginx
sudo systemctl restart mariadb
sudo systemctl restart php7.2-fpm
查看php-fpm进程:
wangqd@wangqd:~$ ps uax | grep php
root 16687 0.0 0.9 353284 19708 ? Ss 03:59 0:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
www-data 16704 0.0 0.4 355584 9912 ? S 03:59 0:00 php-fpm: pool www
www-data 16705 0.0 0.4 355584 9912 ? S 03:59 0:00 php-fpm: pool www
wangqd 16907 0.0 0.0 13136 1048 pts/0 S+ 04:03 0:00 grep --color=auto php
配置Nginx:
php启动成功后下面配置Nginx,让Nginx接收到的php请求转交给php服务器进行解析
nginx配置文件:
cd /etc/nginx/
sudo cp sites-available/default sites-available/default.bak
sudo vim sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
include snippets/fastcgi-php.conf;#取消该行注释
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;#取消该行注释
}
}
有些博主的教程中关于上面配置文件的修改内容可能与笔者的不同,主要是fastcgi_pass 127.0.0.1:9000;和fastcgi_pass unix:/run/php/php7.2-fpm.sock;该取消哪一行的注释,关于这个下面会解释。
不知道取消哪一行注释的请看
/etc/php/7.2/fpm/pool.d/www.conf
两种情况,取消与之对应的注释即可
# listen = /run/php/php7.0-fpm.sock
# listen = 127.0.0.1:9000
重启nginx:
sudo systemctl restart nginx
测试:
sudo vim /home/web1.0/info.php
<?php
phpinfo();
?>
修改nginx前端文件地址:
sudo vim /etc/nginx/sites-available/default
# root /var/www/html;
root /home/web1.0;
访问地址:
http://192.168.234.128/info.php
配置MariaDB:
wangqd@wangqd:/etc/nginx/conf.d$ systemctl restart mariadb
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mariadb.service'.
Authenticating as: wangqd
Password:
==== AUTHENTICATION COMPLETE ===
查看服务器:
wangqd@wangqd:/etc/nginx/conf.d$ ss -tenlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 127.0.0.1:3306 0.0.0.0:* uid:111 ino:71776 sk:9 <->
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* ino:70299 sk:a <->
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:* uid:101 ino:24310 sk:3 <->
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* ino:43557 sk:7 <->
LISTEN 0 128 [::]:80 [::]:* ino:70300 sk:b v6only:1 <->
LISTEN 0 128 [::]:22 [::]:* ino:43568 sk:8 v6only:1 <->
初始化数据库:
wangqd@wangqd:/etc/nginx/conf.d$ 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] n
... skipping.
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!
登录MariaDB:
wangqd@wangqd:/etc/nginx/conf.d$ sudo mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
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)]>
配置php.ini使php通过php7.2-mysql中间件连接MairaDB数据库:
将配置文件中extension=mysqli这一行取消注释就可以了,然后重新启动php-fpm
wangqd@wangqd:~$ sudo vim /etc/php/7.2/fpm/php.ini
870 ;
871 extension=mysqli
872 ;
重启php7.2-fpm:
sudo systemctl restart php7.2-fpm
测试php是否可使用数据库:
wangqd@wangqd:~$ sudo vim /home/web1.0/info.php
<?php
$host = "localhost";
$user = "root";
$passwd = "redhat";
$conn = new mysqli($host,$user,$passwd);
if (!$conn){
die("连接数据库失败");
}
echo "连接数据库成功";
?>
安装并启动Redis Redis端口:6379:
wangqd@wangqd:~$ sudo apt-get install redis -y
wangqd@wangqd:~$ sudo systemctl start redis
wangqd@wangqd:~$ ss -tenlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 127.0.0.1:3306 0.0.0.0:* uid:111 ino:71776 sk:9 <->
LISTEN 0 128 127.0.0.1:6379 0.0.0.0:* uid:112 ino:76211 sk:c <->
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* ino:70299 sk:a <->
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:* uid:101 ino:24310 sk:3 <->
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* ino:43557 sk:7 <->
LISTEN 0 128 [::1]:6379 [::]:* uid:112 ino:76212 sk:d v6only:1 <->
LISTEN 0 128 [::]:80 [::]:* ino:70300 sk:b v6only:1 <->
LISTEN 0 128 [::]:22 [::]:* ino:43568 sk:8 v6only:1 <->
wangqd@wangqd:~$ ps -ef|grep redis
redis 5604 1 0 07:23 ? 00:00:00 /usr/bin/redis-server 127.0.0.1:6379
wangqd 5886 2970 0 07:25 pts/1 00:00:00 grep --color=auto redis
安装php连接redis中间件:php7.2-redis
wangqd@wangqd:~$ sudo apt-get install php7.2-redis -y
配置php支持redis
找到php.ini这个配置文件,添加下面配置,找到extension=mysqli下面加入这一条: extension=redis.so
869 ; For example:
870 ;
871 extension=mysqli
872 extension=redis.so
873
重启php7.2-fpm服务:
sudo systemctrl restart php7.2-fpm
测试php是否可以使用redis:
vim /home/web1.0/info.php
<?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();
?>
输出:
Connection to server successfully
Server is running: +PONG
或者:
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->set('test','hello world!');
echo $redis->get('test');
?>
输出:hello world!
远程异地服务器:
redis-cli -h 127.0.0.1 -p 6379
输出和上面一样则表示连接成功