LNMP+Redis工作機制:當用戶通過瀏覽器訪問網(wǎng)站時宛徊,并使用賬號密碼進行登陸時,此時會向Redis發(fā)出查詢請求逻澳,若Redis緩存中沒有相關(guān)信息闸天,則php會查詢mysql數(shù)據(jù)庫中的相關(guān)信息,然后將相關(guān)信息緩存在redis中斜做;在下次此用戶訪問時苞氮,php無需再從mysql數(shù)據(jù)庫中讀取數(shù)據(jù),直接從redis中讀取緩存并將數(shù)據(jù)返回瓤逼,這樣就可以減少數(shù)據(jù)庫的讀取壓力笼吟。
查看系統(tǒng)版本內(nèi)核等信息:
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和特定版本的相關(guān)信息。如果使用該命令時不帶參數(shù)贷帮,則默認加上-v參數(shù)戚揭。
-v 顯示版本信息。
-i 顯示發(fā)行版的id撵枢。
-d 顯示該發(fā)行版的描述信息民晒。
-r 顯示當前系統(tǒng)是發(fā)行版的具體版本號。
-c 發(fā)行版代號诲侮。
-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相關(guān)模塊:
sudo apt-get install php7.2-fpm php7.2-mysql php7.2-gd php7.2-xml
php7.2-curl php7.2-zip
PHP(超文本預(yù)處理器)的解釋器是php-cgi沟绪。php-cgi只是個CGI程序刮便,他自己本身只能解析請求,返回結(jié)果绽慈,不對進程進行管理,所以就出現(xiàn)了一些能夠調(diào)度php-cgi進程的程序恨旱,比如說由lighthttpd分離出來的spawn-fcgi。PHP-FPM也是這么個東西坝疼,在長時間的發(fā)展后搜贤,逐漸得到了大家的認可,現(xiàn)在也越來越流行钝凶。
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請求轉(zhuǎn)交給php服務(wù)器進行解析
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;#取消該行注釋
}
}
有些博主的教程中關(guān)于上面配置文件的修改內(nèi)容可能與筆者的不同,主要是fastcgi_pass 127.0.0.1:9000;和fastcgi_pass unix:/run/php/php7.2-fpm.sock;該取消哪一行的注釋嗜诀,關(guān)于這個下面會解釋猾警。
不知道取消哪一行注釋的請看
/etc/php/7.2/fpm/pool.d/www.conf
兩種情況,取消與之對應(yīng)的注釋即可
# 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 ===
查看服務(wù)器:
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 <->
初始化數(shù)據(jù)庫:
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數(shù)據(jù)庫:
將配置文件中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是否可使用數(shù)據(jù)庫:
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("連接數(shù)據(jù)庫失敗");
}
echo "連接數(shù)據(jù)庫成功";
?>
安裝并啟動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服務(wù):
sudo systemctrl restart php7.2-fpm
測試php是否可以使用redis:
vim /home/web1.0/info.php
<?php
$redis = new Redis(); \\redis連接參數(shù)
$redis->connect('127.0.0.1', 6379); \\括號內(nèi)第一項是指的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!
遠程異地服務(wù)器:
redis-cli -h 127.0.0.1 -p 6379
輸出和上面一樣則表示連接成功