NGINX-LNMP

作者dmxsp

8月 17, 2022

1.什么是LNMP

LNMP 是一套技术的组合, L=Linux、 N=Nginx、 M=MySQL、 P=PHP

2.常见的架构

LAMP:LInux Apache MySQL(MariaDB) PHP (Fastcgi)

LNMP/LEMP:LInux Nginx MySQL(MariaDB) PHP(Fastcgi)

LNMT:Linux Nginx MySQL Tomcat/Jboss/Resin/(java)Weblogic(配合Oracle数据库)

LNMP:Linux Nginx MySQL Python (Uwsgi)

LNM???:Linux Nginx MySQL 开发

3.LNMP原理

用户发出请求后,请求到达nginx
如果是静态请求,nginx自己处理
如果是动态请求nginx 根据 请求类型(php) 通过fastcgi_pass 动态请求交给 php-fpm(fastcgi)
php-fpm收到请求后,传递给php解释器(与数据库交互)
解释后把结果传递给用户(解释器–>php-fpm—>nginx—>用户)
完成动态请求过程

php-fpm.conf 配置控制php-fpm进程
php.ini 控制解释器

4.什么是FastCGI

FastCGI是一个可伸缩地、高速地在http server和动态脚本语言间通信的接口(FastCGI接口在Linux下是socket,这个socket可以是文件socket,也可以是ip socket),主要优点是把动态语言和HTTP Server分离开来。多数流行的HTTP server都支持FastCGI包括Nginx和Lighttpd等,同时FastCGI也被许多脚本语言所支持,其中就有PHP。FastCGI接口方式采用C/S结构,可以将http服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当http服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让http服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能

5.Nginx下FastCGI运行原理

Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是IP socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另外一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据,接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx,最后Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程

6.静态vs动态

静态资源

服务器端的文件从服务器端传给浏览器之后,如果在浏览器上看到的页面内容和服务器端一模一样,就是属于静态资源,这里面包含HTML、CSS、JS、图片等等,不需要查数据库也不需要程序处理,直接就能够显示的页面(*.htm或者是*.htm)

动态资源

服务器端会把程序动态运行起来,运行完以后会把程序的执行结果发还给客户端 ,客户端看到的只是一个结果,看不到源程序。客户端请求的动态资源,先把请求交给web的一个存储点,web存储点连接数据库,数据库处理数据之后,将数据交给web服务器,web服务器返回给客户端解析渲染处理。交互内容(评论,发布文章,订单,金钱) url连接中 包含特殊符号 & 符号 或 ? 符号(*.jsp、*.asp/*.aspx、*.php)

区别

静态资源一般都是设计好的html页面,而动态资源依靠设计好的程序来实现按照需求的动态响应或者从数据库中读数据

静态资源的交互性差,不好更改,而动态资源可以根据需求获取内容

在服务器的运行状态不同,静态资源不需要与数据库参于程序处理,动态资源需要一个或多个数据库的参与运算

7.环境准备

nginx

安装nginx

[root@web01 ~]$ yum install nginx -y
[root@web01 ~]$ systemctl start nginx
[root@web01 ~]$ systemctl enable nginx

写web配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.blog.com.conf
server {
listen 80;
server_name dmxsp.blog.com;
root /code/blog;
location / {
index index.html index.php;
}

location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

fastcgi_param:修改或设置 nginx 向php发送的数据包
SCRIPT_FILENAME:修改后的格式,这是PHP内置变量
$document_root:站点目录(网站) root
$fastcgi_script_name:uri 用户请求uri

检查语法

[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

[root@web01 ~]$ systemctl reload nginx.service

创建目录

[root@web01 ~]$ mkdir -p /code/blog

mysql数据库

安装数据库

[root@web01 ~]$ yum install mariadb-server -y

[root@web01 ~]$ systemctl start mariadb.service
[root@web01 ~]$ systemctl enable mariadb.service

检查是否正常启动(进程、端口)

[root@web01 ~]$ ss -lntup | grep mysql
tcp LISTEN 0 50 *:3306 *:* users:((“mysqld”,pid=101700,fd=14))

[root@web01 ~]$ ps -ef | grep mysql
mysql 101535 1 0 16:17 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe –basedir=/usr
mysql 101700 101535 0 16:17 ? 00:00:00 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –plugin-dir=/usr/lib64/mysql/plugin –log-error=/var/log/mariadb/mariadb.log –pid-file=/var/run/mariadb/mariadb.pid –socket=/var/lib/mysql/mysql.sock

登录mysql,并设置密码

[root@web01 ~]$ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

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)]>

ctrl+d:退出

[root@web01 ~]$ mysqladmin -u root password ‘123’

进入mysql两种方式

命令行

[root@web01 ~]$ mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB Server

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)]> Bye

交互式

[root@web01 ~]$ mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server

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)]> Bye

查看所有数据库

MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————–+
4 rows in set (0.00 sec)

查看表

MariaDB [(none)]> show tables from mysql;

进入目录

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]>

详细信息

MariaDB [mysql]> show tables;

查看表中数据

MariaDB [mysql]> select user,host from mysql.user;
+——+———–+
| user | host |
+——+———–+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web01 |
| root | web01 |
+——+———–+
6 rows in set (0.01 sec)

select 字段(列)   from 数据库.表

php环境准备

配置源

[root@web01 ~]$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@web01 ~]$ rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装

[root@web01 ~]$ yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache php72w-pecl-redis

启动

[root@web01 ~]$ systemctl start php-fpm.service
[root@web01 ~]$ systemctl enable php-fpm.service

查看端口

[root@web01 ~]$ ss -lntup | grep php
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:((“php-fpm”,pid=112914,fd=9),(“php-fpm”,pid=112913,fd=9),(“php-fpm”,pid=112912,fd=9),(“php-fpm”,pid=112911,fd=9),(“php-fpm”,pid=112910,fd=9),(“php-fpm”,pid=112909,fd=7))

[root@web01 ~]$ ps aux | grep php

修改配置文件

[root@web01 ~]$ egrep -n ‘^user|^group’ /etc/php-fpm.d/www.conf
8:user = nginx
10:group = nginx

重启

[root@web01 ~]$ systemctl reload php-fpm.service

8.检查 lnmp是否可用

nginx+php

写配置文件

[root@web01 ~]$ cat /code/blog/info.php
<?php
phpinfo();
?>

测试

php+数据库

写配置文件

[root@web01 ~]$ cat /code/blog/mysql.php
<?php
//$link_id=mysqli_connect(‘主机名’,’用户’,’密码’);

$link_id=mysqli_connect(‘localhost’,’root’,’123′) or
mysqli_error();
//$link_id=mysqli_connect(‘localhost’,’test’,”);
if($link_id){
echo “mysql+php cs !”;
}else{
echo mysqli_error();
}
//这是php单行注释
/* 这是php多行注释 */
?>

[root@web01 ~]$ cat /code/blog/mysqll.php
<?php
$servername = “localhost”;
$username = “root”;
$password = “123”;

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}
echo “连接成功”;
?>

测试

9.安装wordpress

官网

下载

[root@web01 ~]$ wget https://wordpress.org/latest.zip

[root@web01 ~]$ unzip latest.zip

修改用户

[root@web01 ~]$ chown -R nginx.nginx /code/wordpress/

写配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/blog.wordpress.com.conf
server {
listen 80;
server_name blog.wordpress.com;
root /code/wordpress;
location / {
index index.html index.php;
}

location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

检查语法

[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

[root@web01 ~]$ systemctl reload nginx.service

创建数据库

[root@web01 ~]$ mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.68-MariaDB MariaDB Server

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 |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————–+
4 rows in set (0.10 sec)

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+——————–+
5 rows in set (0.00 sec)

删除

drop  database wordpress;

web页面安装产品

查看数据库

[root@web01 ~]$ mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.68-MariaDB MariaDB Server

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 tables from wordpress;
+———————–+
| Tables_in_wordpress |
+———————–+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_termmeta |
| wp_terms |
| wp_usermeta |
| wp_users |
+———————–+
12 rows in set (0.00 sec)

10.安装wecenter

官网

下载

[root@web01 ~]$ mkdir /code/wecenter
[root@web01 ~]$ mv WeCenter_V4.0.2.zip /code/wecenter/
[root@web01 ~]$ cd /code/wecenter/
[root@web01 /code/wecenter]$ unzip WeCenter_V4.0.2.zip
[root@web01 /code/wecenter]$ rm -rf WeCenter_V4.0.2.zip

修改权限

[root@web01 /code/wecenter]$ find -type f -print0 | xargs -0 chmod 644
[root@web01 /code/wecenter]$ find -type d -print0 | xargs -0 chmod 755
[root@web01 /code/wecenter]$ chown -R nginx.nginx /code/wecenter/

写配置文件

[root@web01 /code/wecenter]$ cat /etc/nginx/conf.d/blog.wecenter.com.conf
server {
listen 80;
server_name blog.wecenter.com;
root /code/wecenter;
location / {
index index.html index.php;
}

location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

检查语法

[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

[root@web01 ~]$ systemctl reload nginx.service

创建数据库

[root@web01 /code/wecenter]$ mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 387
Server version: 5.5.68-MariaDB MariaDB Server

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)]> create database wecenter;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
| wecenter |
| wordpress |
+——————–+
6 rows in set (0.00 sec)

说明文档

[root@web01 ~]$ cat /code/wecenter/README.md

测试

11.LNMP架构演变

网站迁移

准备环境

web服务器

数据库服务器

存储服务器

迁移过程

检查web是否正常
迁移web服务器的数据(用户上传 web到存储服务器(nfs
迁移web服务器中的数据库内容(web)到数据库服务器(db)
扩容web服务器/横向扩展web服务器

静态资源迁移(共享存储nfs)

用户上传目录在哪里
先把web用户上传目录的内容复制出来
用户上传目录挂载到nfs上面
检查nfs是否可以
配置nfs共享目录
nfs挂载到web服务器
把复制走的,复制到原来的地方(用户上传目录中)
web服务器检查用户上传文件是否可用
检查服务是否开机自启动

先把web用户上传目录的内容复制出来用户上传目录在哪里

[root@web01 ~]$ ll /code/wordpress/wp-content/uploads/

先把web用户上传目录的内容复制出来(挂载nfs覆盖)

[root@web01 ~]$ mkdir -p /backer/
[root@web01 ~]$ mv /code/wordpress/wp-content/uploads/* /backer/

nfs服务器

配置文件

[root@nfs ~]$ cat /etc/exports
/dmxsp/wordpress 172.16.1.0/24(rw,all_squash)

[root@nfs ~]$ mkdir -p /dmxsp/wordpress

修改主

[root@nfs ~]$ chown nfsnobody.nfsnobody /dmxsp/wordpress/

客户端web服务器进行挂载

安装

[root@web01 ~]$ yum install -y nfs-utils rpcbind

[root@web01 ~]$ systemctl start rpcbind.service nfs
[root@web01 ~]$ systemctl enable rpcbind.service nfs

挂载

[root@web01 ~]$ mount -t nfs 172.16.1.16:/dmxsp/wordpress/ /code/wordpress/wp-content/uploads/

[root@web01 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root 47G 5.2G 42G 12% /
/dev/sda1 1014M 151M 864M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
172.16.1.16:/dmxsp/wordpress 47G 2.0G 46G 5% /code/wordpress/wp-content/uploads

把复制走的,复制到原来的地方(用户上传目录中)

[root@web01 ~]$ mv /backer/* /code/wordpress/wp-content/uploads
mv: failed to preserve ownership for ‘/code/wordpress/wp-content/uploads/2022/08/网线.png’: Operation not permitted
mv: failed to preserve ownership for ‘/code/wordpress/wp-content/uploads/2022/08/网线-300×143.png’: Operation not permitted
mv: failed to preserve ownership for ‘/code/wordpress/wp-content/uploads/2022/08/网线-150×150.png’: Operation not permitted
mv: failed to preserve ownership for ‘/code/wordpress/wp-content/uploads/2022/08/网线-768×365.png’: Operation not permitted
mv: failed to preserve ownership for ‘/code/wordpress/wp-content/uploads/2022/08’: Operation not permitted
mv: failed to preserve ownership for ‘/code/wordpress/wp-content/uploads/2022’: Operation not permitted

上传照片查看nfs服务器是否正常

迁移数据库至数据库服务器

迁移数据库
在mysqldb准备数据库环境
在web服务器备份数据库内容
备份传送到数据库服务器
把备份导入到新的数据库中
web服务器
修改程序代码,访问mysqldb的数据库

mysqldb准备数据库环境

[root@mysqldb ~]$ yum install -y mariadb-server

[root@mysqldb ~]$ systemctl start mariadb.service
[root@mysqldb ~]$ systemctl enable mariadb.service

web服务器备份数据库

[root@web01 ~]$ mysqldump -uroot -p123
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] –databases [OPTIONS] DB1 [DB2 DB3…]
OR mysqldump [OPTIONS] –all-databases [OPTIONS]
For more options, use mysqldump –help

仅导出

[root@web01 ~]$ mysqldump -uroot -p123 -A >all.sql

导出并压缩

[root@web01 ~]$mysqldump -uroot -p123 -A |gzip >all.sql.gz

备份传送到数据库服务器

[root@web01 ~]$ scp all.sql 172.16.1.26:/root

导入到新的数据库中

[root@mysqldb ~]$ mysql <all.sql

刷新mysql权限信息(如果不刷新,登录时要和导入数据的用户密码相同登录不上去)

[root@mysqldb ~]$ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server

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)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

[root@mysqldb ~]$ mysql -uroot -p123

授权远程登录

MariaDB [(none)]> select user,host from mysql.user;
+——+———–+
| user | host |
+——+———–+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web01 |
| root | web01 |
+——+———–+
6 rows in set (0.00 sec)

MariaDB [(none)]> grant all on *.* to ‘all’@’%’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+——+———–+
| user | host |
+——+———–+
| all | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
| root | web01 |
+——+———–+

删除没有用户的多余主机

MariaDB [(none)]> drop user ”@’localhost’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> drop user ”@’web01′;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+——+———–+
| user | host |
+——+———–+
| root | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
| root | web01 |
+——+———–+
5 rows in set (0.00 sec)

刷新权限

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

web连接远程mysql

[root@web01 ~]$ mysql -uroot -p123 -h 172.16.1.26
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.68-MariaDB MariaDB Server

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)]>

修改程序代码,访问mysqldb的数据库

[root@web01 ~]$ grep DB_ /code/wordpress/wp-config.php
define( ‘DB_NAME’, ‘wordpress’ );
define( ‘DB_USER’, ‘all’ );
define( ‘DB_PASSWORD’, ‘123’ );
define( ‘DB_HOST’, ‘172.16.1.26’ );
define( ‘DB_CHARSET’, ‘utf8mb4’ );
define( ‘DB_COLLATE’, ” );

测试

[root@web01 ~]$ systemctl stop mariadb.service

[root@web01 ~]$ ss -lntup | grep mysql

(目录所有者不需要全部修改,只需要修改用到的目录如:图片目录等等)

作者 dmxsp

发表回复

您的电子邮箱地址不会被公开。