LNMP网站架构

LNMP网站架构


LNMP架构简介

LNMP是什么?

LNMP是Linux系统下Nginx+MySQL+PHP的网站服务器架构的缩写。

​ Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

查看源图像

LNMP架构的优点

1)作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。

2)作为负载均衡服务器:Nginx 既可以在内部直接支持Rails和PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。

3)作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。

4)Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。

LNMP与LAMP的区别

lamp=linux+apache+mysql+php

lnmp=linux+nginx+mysql+php

1)在LNMP中,Nginx本身对脚本不做任何的处理,而是把请求发给fast-cgi管理进程处理fast-cgi管理进程选择cgi子进程处理结果并返回,二者是相互独立的,通过管道进程通信。

2)在LAMP中,PHP是Apache的一个模块,具有相同的生命周期,两者通过共享内存的方式通信。

3)LNMP方式的优点:占用VPS(Virtual Private Server 虚拟专用服务器)资源较少,Nginx配置起来也比较简单,利用fast-cgi的方式动态解析PHP脚本。

4)LNMP方式的缺点:php-fpm组件的负载能力有限,在访问量巨大的时候,php-fpm进程容易僵死,容易发生502 bad gateway错误。

5)LNAMP方式的优点:由于Apache本身处理PHP的能力比起php-fpm要强,所以不容易出现类似502 bad gateway的错误。适合访问量较大的站点使用。

6)LNAMP方式的缺点:相比LNMP方式会多占用一些资源,另外,配置虚拟主机需要同时修改Nginx和Apache的配置文件,要稍微麻烦一些。

LNMP的工作原理

工作流程

1)client(http协议发起请求) —>Nginx(location判断为静态页面请求)—>html ---->client

2)client(http协议发起请求) —>Nginx(location判断为动态页面请求)—>fastcgi协议快速通用网关接口 —>php-fpm(php进程管理 —>调用wapper工作进程 —>调用php解析 —>进入MySQL —>返回过程 —>php-fpm —>fastcgi —>nginx —>http —>client工作完成

image-20221011204342542

知识拓展

FastCGI

​ CGI全称是"通用网关接口"(Common Gateway Interface),web服务器与你的或其它机器上的程序进行"交谈"的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等。
FastCGI是从CGI发展改进而来的。传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后将结果返回给HTTP服务器。这在处理高并发访问时几乎是不可用的。另外传统的CGI接口方式安全性也很差,现在已经很少使用了。
​ FastCGI接口方式采用C/S结构,可以将HTTP服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当HTTP服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。

Nginx+FastCGI

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

PHP-FPM

​ PHP-FPM(PHP FastCGI Process Manager):PHP FastCGI 进程管理器,用于管理PHP 进程池的软件,用于接受web服务器的请求。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置。

image-20221011204802337


搭建LNMP架构

环境说明

主机名IP地址系统
nginx192.168.92.130Centos8
mysql192.168.92.129Centos8
php192.168.92.132Centos8

本次实验部署N、M、P的安装方式均采用源码。

nginx版本=nginx-1.22.0 👉nginx源码包官方下载网址

mysql版本=mysql-5.7.38 👉mysql包官方下载网址

php版本=php-8.1.11 👉php包官方下载网址

部署Nginx

#下载源码编译所需的依赖包
[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget vim make

#新增nginx用户
[root@nginx ~]# useradd -Mrs /sbin/nologin nginx
[root@nginx ~]# id nginx
uid=995(nginx) gid=992(nginx) groups=992(nginx)

#新建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/

#下载nginx源码包并解压
[root@nginx ~]# cd /usr/local/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@nginx src]# tar -xf nginx-1.22.0.tar.gz

#编译安装nginx
[root@nginx src]# cd nginx-1.22.0/
[root@nginx nginx-1.22.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

#添加环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh

#写service文件,用systemd方式控制nginx服务
[root@nginx ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

#重启生效后,把nginx设为开机自启
[root@nginx ~]# systemctl daemon-reload
[root@nginx ~]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx ~]# ss -anlt
State          Recv-Q         Send-Q                 Local Address:Port                   Peer Address:Port         Process
LISTEN         0              128                          0.0.0.0:80                          0.0.0.0:*
LISTEN         0              128                          0.0.0.0:22                          0.0.0.0:*
LISTEN         0              128                             [::]:22                             [::]:*

image-20221011144823337

部署MySQL

#安装所需依赖包
[root@mysql ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

#创建mysql用户
[root@mysql ~]# useradd -Mrs /sbin/nologin mysql
[root@mysql ~]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)

#下载mysql二进制包,解压该包
[root@mysql ~]# cd /usr/local/src/
[root@mysql src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@mysql src]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

#修改目录的属主属组为mysql
[root@mysql src]# cd /usr/local/
[root@mysql local]# mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@mysql local]# chown -R mysql:mysql mysql

#添加环境变量,映射头文件、库文件、man手册
[root@mysql local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@mysql local]# source /etc/profile.d/mysql.sh
[root@mysql local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@mysql local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql local]# ldconfig
[root@mysql local]# echo 'MANPATH /usr/local/mysql/man' >> /etc/man_db.conf

#创建数据存放目录,并修改属主属组为mysql
[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data/

#初始化数据库
[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T07:00:29.339776Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T07:00:29.499824Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T07:00:29.525179Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T07:00:29.580838Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6426c548-4932-11ed-82dd-000c29e2b19f.
2022-10-11T07:00:29.581573Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T07:00:29.959069Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T07:00:29.959104Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T07:00:29.959633Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T07:00:30.038962Z 1 [Note] A temporary password is generated for root@localhost: uFiisGsZw7/f

#添加配置文件
[root@mysql ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

#配置服务启动脚本
[root@mysql ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@mysql ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

#修改数据库密码
[root@mysql ~]# mysql -uroot -p'uFiisGsZw7/f'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye
#测试新密码
[root@mysql ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

部署PHP

#安装所需的依赖包
[root@php ~]# dnf -y install epel-release
[root@php ~]# dnf -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel wget gcc gcc-c++ make
[root@php ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

#下载php源码包并解压
[root@php ~]# cd /usr/local/src/
[root@php src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@php src]# tar -xf php-8.1.11.tar.gz

#进行编译安装
[root@php src]# cd php-8.1.11/
[root@php php-8.1.11]# ./configure --prefix=/usr/local/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear --enable-pcntl \
--enable-posix
[root@php ~]# make -j $(cat /proc/cpuinfo |grep processor|wc -l) && make install

#配置环境变量
[root@php ~]# cd /usr/local/php8/
[root@php php8]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@php php8]# source /etc/profile.d/php8.sh
#验证php的环境变量是否配置成功
[root@localhost php8]# which php
/usr/local/php8/bin/php
#查看php的版本信息
[root@php php8]# php -v
PHP 8.1.11 (cli) (built: Oct 11 2022 16:38:07) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies

#映射头文件
[root@php php8]# ln -s /usr/local/php8/include /usr/include/php
[root@php php8]# ll /usr/include/php
lrwxrwxrwx 1 root root 23 Oct 11 16:43 /usr/include/php -> /usr/local/php8/include

#配置php-fpm
[root@php php8]# cd /usr/local/src/php-8.1.11/
[root@php php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-8.1.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@php php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

#编写service文件
[root@php ~]# cat > /usr/lib/systemd/system/php.service <<EOF
[Unit]
Description=php server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/php8/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

[root@php ~]# systemctl daemon-reload
[root@php ~]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@php ~]# ss -anlt
State       Recv-Q      Send-Q             Local Address:Port             Peer Address:Port      Process
LISTEN      0           128                    127.0.0.1:9000                  0.0.0.0:*
LISTEN      0           128                      0.0.0.0:22                    0.0.0.0:*
LISTEN      0           128                         [::]:22                       [::]:*

连接nginx与php

nginx配置
[root@nginx ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
   phpinfo();
?>
EOF
#修改配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.htm index.php;		#添加index.php
        }
...............
        location ~ \.php$ {					#把这一整段配置取消注释
            root           html;
            fastcgi_pass   192.168.92.132:9000;		#填写php服务器的IP
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;		#添加$document_root是匹配root指定的路径
            include        fastcgi_params;
        }
...............

#重载服务,生效配置
[root@nginx ~]# systemctl reload nginx.service

#因预先存在index.html,更名后才能直接访问index.php
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mv index.html{,.bak}
[root@nginx html]# ls
50x.html  index.html.bak  index.php
php配置
[root@php ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
listen = 192.168.92.132:9000		#php服务器本机IP
listen.allowed_clients = 192.168.92.130		#nginx端IP。取消该行注释(;)
[root@php ~]# systemctl restart php.service

#nginx端与php两边都存在该文件才能访问成功
[root@php ~]# mkdir -p /usr/local/nginx/html
[root@php ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
   phpinfo();
?>
EOF

image-20221011201047038

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值