LNMP

LNMP架构简介

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
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支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。

LAMP和LNMP的区别

lamp=linux+apache+mysql+php
lnmp=linux+nginx+mysql+php

(1)在LNMP中,Nginx本身对脚本不做任何的处理,而是把请求发给fast-cgi管理进程处理fast-cgi管理进程选择cgi子进程处理结果并返回,二者是相互独立的,通过管道进程通信
(2)在LAMP中,PHP是Apache的一个模块,具有相同的生命周期,两者通过共享内存的方式通信
两者的PHP环境不相互适用
相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率
(3)LNMP方式的优点:占用VPS(Virtual Private Server 虚拟专用服务器)资源较少,Nginx配置起来也比较简单,利用fast-cgi的方式动态解析PHP脚本。
LNMP方式的缺点:php-fpm组件的负载能力有限,在访问量巨大的时候,php-fpm进程容易僵死,容易发生502 bad gateway错误。
(4)LNAMP方式的优点:由于Apache本身处理PHP的能力比起php-fpm要强,所以不容易出现类似502 bad gateway的错误。适合访问量较大的站点使用。
LNAMP方式的缺点:相比LNMP方式会多占用一些资源,另外,配置虚拟主机需要同时修改Nginx和Apache的配置文件,要稍微麻烦一些。

lnmp的工作原理

1.用户通过http协议发起请求,请求会先抵达LNMP架构中的nginx

2.nginx会根据用户的请求进行判断,这个判断是由Location完成的

3.判断用户请求的是静态页面,nginx直接进行处理

4.判断用户的请求是动态页面,nginx会将该请求交给fastcgi协议下发

5.fastcgi会将请求交给php-fpm管理进程,php-fpm管理进程接收到后会调用具体的工作进程wrapper

6.wrapper线程会调用php进行解析,如果只是解析php代码,那么直接返回结果给客户端

7.如果有查询数据库的操作,则由php连接数据库(用户,密码,IP)然后发起查询的操作

8.最终数据由mysql > php > php-fpm > fastcgi > nginx > http > user

Nginx源码安装

//关闭防火墙和selinux
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# systemctl disable --now firewalld.service
//创建用户
[root@nginx ~]# useradd -rMs /sbin/nologin nginx
//安装所需要的依赖包
[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
//下载nginx源码包,并解压
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@nginx ~]# tar -xf nginx-1.20.2.tar.gz
//准备,修张版本号
[root@nginx nginx-1.20.2]# vim src/core/nginx.h
#define NGINX_VERSION      "123456"
//开始配置
[root@nginx nginx-1.20.2]# ./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
//开启编译
[root@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@nginx nginx-1.20.2]# cd /usr/local/nginx/
[root@nginx nginx]# ls
conf  html  logs  sbin
[root@nginx nginx]#

安装完成后配置以内容

//配置环境变量
[root@nginx ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
//启动服务
[root@nginx ~]# nginx
[root@nginx ~]# ss -antl
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                           [::]:*
[root@nginx ~]#
//编写service文件,并设置服务开机自启
[root@nginx ~]# nginx -s stop
[root@nginx ~]# ss -antl
State         Recv-Q        Send-Q                Local Address:Port                 Peer Address:Port        Process
LISTEN        0             128                         0.0.0.0:22                        0.0.0.0:*
LISTEN        0             128                            [::]:22                           [::]:*
[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
[root@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 -antl
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                           [::]:*
[root@nginx ~]#

lnmp架构优化方案 lnmp优点_mysql

mysql源码安装

//关闭防火墙和selinux
[root@mysql ~]# setenforce 0
[root@mysql ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@mysql ~]# systemctl disable --now firewalld.service
//安装依赖包
[root@mysql ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

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

//下载mysql二进制包,解压包
[root@mysql ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@mysql ~]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# cd /usr/local/
[root@mysql ~]# mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@mysql local]# ls
apache  apr-util  etc    include  lib64    mysql  share
apr     bin       games  lib      libexec  sbin   src
[root@167 local]# 

//修改目录的属主属组为mysql
[root@mysql local]# chown -R mysql.mysql /usr/local/mysql/
[root@mysql local]# ll
total 0
drwxr-xr-x. 14 root  root  164 Aug  2 23:07 apache
drwxr-xr-x.  6 root  root   58 Aug  2 22:52 apr
drwxr-xr-x.  5 root  root   43 Aug  2 22:54 apr-util
drwxr-xr-x.  2 root  root   21 Jun 28 09:36 bin
drwxr-xr-x.  2 root  root    6 Jun 22  2021 etc
drwxr-xr-x.  2 root  root    6 Jun 22  2021 games
drwxr-xr-x.  2 root  root    6 Jun 22  2021 include
drwxr-xr-x.  2 root  root    6 Jun 22  2021 lib
drwxr-xr-x.  3 root  root   17 Jun 27 20:10 lib64
drwxr-xr-x.  2 root  root    6 Jun 22  2021 libexec
drwxr-xr-x.  9 mysql mysql 129 Aug  2 23:20 mysql
drwxr-xr-x.  2 root  root    6 Jun 22  2021 sbin
drwxr-xr-x.  5 root  root   49 Jun 27 20:10 share
drwxr-xr-x.  5 root  root  195 Aug  2 23:19 src
[root@mysql local]# 

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

//创建数据存放目录,并修改属主属组为mysql
[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data/
[root@mysql ~]# cd /opt/
[root@mysql opt]# ll
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug  2 23:23 data
[root@mysql opt]# 

//初始化数据库
[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data                          /
2022-10-11T07:52:34.708801Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecat                          ed. Please use --explicit_defaults_for_timestamp server option (see documentation for mor                          e details).
2022-10-11T07:52:35.339842Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T07:52:35.423240Z 0 [Warning] InnoDB: Creating foreign key constraint system ta                          bles.
2022-10-11T07:52:35.436046Z 0 [Warning] No existing UUID has been found, so we assume tha                          t this is the first time that this server has been started. Generating a new UUID: ab4e6c                          3a-4939-11ed-8965-000c297f6eed.
2022-10-11T07:52:35.437178Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.                          gtid_executed' cannot be opened.
2022-10-11T07:52:35.624110Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please                           use TLSv1.2 or higher.
2022-10-11T07:52:35.624137Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Plea                          se use TLSv1.2 or higher.
2022-10-11T07:52:35.624454Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T07:52:35.667646Z 1 [Note] A temporary password is generated for root@localhost                          : hhBAuMg3!K8x
[root@mysql ~]#


//添加配置文件
[root@mysql ~]# vim /etc/my.cnf
[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

//配置服务启动脚本
[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

//启动mysql
[root@mysql ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/167.err'.
 SUCCESS! 
[root@mysql ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         80                       *:3306                    *:*                  
LISTEN    0         128                      *:80                      *:*                  
LISTEN    0         128                   [::]:22                   [::]:*                  
[root@167 ~]# 

//修改数据库密码
[root@mysql ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
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 4
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> quit
Bye
[root@mysql ~]#

php源码安装

//关闭selinux和防火墙
[root@php ~]# setenforce 0
[root@php ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@php ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//安装依赖包
[root@php ~]# dnf -y install epel-release
[root@php ~]# yum install libxml2
[root@php ~]# yum install libxml2-devel
[root@php ~]# yum -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 readline readline-devel libxslt libxslt-devel  php-mysqlnd gcc gcc-c++ make
[root@php ~]# yum -y install autoconf freetype gd libpng libpng-devel libjpeg  zlib curl curl-devel net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-clients freetype-devel gmp-devel libzip libzip-devel sqlite-devel  
[root@php php-8.1.11]# dnf -y install https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/oniguruma-6.8.2-2.el8.x86_64.rpm
[root@php php-8.1.11]# dnf -y install https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//从php官网下载php的源码包并解压
[root@php ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@php ~]# tar -xf php-8.1.11.tar.gz -C /usr/local/src/
[root@php ~]# cd /usr/local/src/php-8.1.11/
[root@php php-8.1.11]#
//编译安装php
[root@php php-8.1.11]# ./configure --prefix=/usr/local/php \
 --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 php-8.1.11]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@php php-8.1.11]# cd /usr/local/php/
[root@php php]# ls
bin  etc  include  lib  php  sbin  var
[root@php php]#
//配置服务
[root@php php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@php php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
[root@php php]# ln -s /usr/local/php /usr/include/php
//编写service文件
[root@php php]# cat > /usr/lib/systemd/system/php.service << EOF
> [Unit]
> Description=php server daemon
> After=network.target
>
> [Service]
> Type=forking
> ExecStart=/usr/local/php/sbin/php-fpm
> ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill -9
> ExecReload=/bin/kill -HUP $MAINPID
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@php php]# systemctl daemon-reload
[root@php 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 php]# ss -antl
State         Recv-Q        Send-Q               Local Address:Port                Peer Address:Port        Process
LISTEN        0             128                        0.0.0.0:22                       0.0.0.0:*
LISTEN        0             128                      127.0.0.1:9000                     0.0.0.0:*
LISTEN        0             128                           [::]:22                          [::]:*
[root@php php]#

连接nginx和php

php

//编写测试主页
[root@php ~]# mkdir -p /usr/local/nginx/html
[root@php ~]# cat > /usr/local/nginx/html/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
[root@php ~]#
//修改配置文件
[root@php ~]# vim /usr/local/php/etc/php-fpm.d/www.conf
listen = 192.168.159.129:9000               //php端ip
listen.allowed_clients = 192.168.159.167    //nginx端ip
[root@php ~]# systemctl restart php.service

nginx

//编写测试主页
[root@nginx ~]# cat > /usr/local/nginx/html/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
[root@nginx ~]# systemctl restart nginx.service
//nginx端编辑配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm index.php;
        }
...
location ~ \.php$ {
            root           html;
            fastcgi_pass   192.168.159.129:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
//重新加载服务配置
[root@nginx ~]# systemctl reload nginx.service
[root@nginx ~]# ss -antl
State          Recv-Q         Send-Q                 Local Address:Port                   Peer Address:Port         Process
LISTEN         0              128                          0.0.0.0:22                          0.0.0.0:*
LISTEN         0              128                          0.0.0.0:80                          0.0.0.0:*
LISTEN         0              128                             [::]:22                             [::]:*
[root@nginx ~]#

lnmp架构优化方案 lnmp优点_lnmp架构优化方案_02