天天看點

LNMP網站架構

LNMP網站架構

文章目錄

    • LNMP架構簡介
      • LNMP是什麼?
      • LNMP架構的優點
      • LNMP與LAMP的差別
      • LNMP的工作原理
    • 知識拓展
      • FastCGI
      • Nginx+FastCGI
      • PHP-FPM
    • 搭建LNMP架構
      • 部署Nginx
      • 部署MySQL
      • 部署PHP
      • 連接配接nginx與php
        • nginx配置
        • php配置

LNMP架構簡介

LNMP是什麼?

LNMP是Linux系統下Nginx+MySQL+PHP的網站伺服器架構的縮寫。

​ Nginx是一個高性能的HTTP和反向代理伺服器,也是一個IMAP/POP3/SMTP代理伺服器。Mysql是一個小型關系型資料庫管理系統。PHP是一種在伺服器端執行的嵌入HTML文檔的腳本語言。這四種軟體均為免費開源軟體,組合到一起,成為一個免費、高效、擴充性強的網站服務系統。

LNMP網站架構

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工作完成

LNMP網站架構

知識拓展

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配置。

LNMP網站架構

搭建LNMP架構

環境說明:

主機名 IP位址 系統
nginx 192.168.92.130 Centos8
mysql 192.168.92.129 Centos8
php 192.168.92.132 Centos8

本次實驗部署N、M、P的安裝方式均采用源碼。

nginx版本=nginx-1.22.0 👉nginx源碼包官方下載下傳網址

mysql版本=mysql-5.7.38 👉mysql包官方下載下傳網址

php版本=php-8.1.11 👉php包官方下載下傳網址

部署Nginx

#下載下傳源碼編譯所需的依賴包
[[email protected] ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget vim make

#新增nginx使用者
[[email protected] ~]# useradd -Mrs /sbin/nologin nginx
[[email protected] ~]# id nginx
uid=995(nginx) gid=992(nginx) groups=992(nginx)

#建立日志存放目錄
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx/

#下載下傳nginx源碼包并解壓
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
[[email protected] src]# tar -xf nginx-1.22.0.tar.gz

#編譯安裝nginx
[[email protected] src]# cd nginx-1.22.0/
[[email protected] 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
[[email protected] nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

#添加環境變量
[[email protected] ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[[email protected] ~]# source /etc/profile.d/nginx.sh

#寫service檔案,用systemd方式控制nginx服務
[[email protected] ~]# 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設為開機自啟
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[[email protected] ~]# 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                             [::]:*
           
LNMP網站架構

部署MySQL

#安裝所需依賴包
[[email protected] ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

#建立mysql使用者
[[email protected] ~]# useradd -Mrs /sbin/nologin mysql
[[email protected] ~]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)

#下載下傳mysql二進制包,解壓該包
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

#修改目錄的屬主屬組為mysql
[[email protected] src]# cd /usr/local/
[[email protected] local]# mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[[email protected] local]# chown -R mysql:mysql mysql

#添加環境變量,映射頭檔案、庫檔案、man手冊
[[email protected] local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[[email protected] local]# source /etc/profile.d/mysql.sh
[[email protected] local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[[email protected] local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[[email protected] local]# ldconfig
[[email protected] local]# echo 'MANPATH /usr/local/mysql/man' >> /etc/man_db.conf

#建立資料存放目錄,并修改屬主屬組為mysql
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/

#初始化資料庫
[[email protected] ~]# /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 [email protected]: uFiisGsZw7/f

#添加配置檔案
[[email protected] ~]# 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

#配置服務啟動腳本
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[[email protected] ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

#修改資料庫密碼
[[email protected] ~]# 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
#測試新密碼
[[email protected] ~]# 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

#安裝所需的依賴包
[[email protected] ~]# dnf -y install epel-release
[[email protected] ~]# 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
[[email protected] ~]# 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源碼包并解壓
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[[email protected] src]# tar -xf php-8.1.11.tar.gz

#進行編譯安裝
[[email protected] src]# cd php-8.1.11/
[[email protected] 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
[[email protected] ~]# make -j $(cat /proc/cpuinfo |grep processor|wc -l) && make install

#配置環境變量
[[email protected] ~]# cd /usr/local/php8/
[[email protected] php8]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[[email protected] php8]# source /etc/profile.d/php8.sh
#驗證php的環境變量是否配置成功
[[email protected] php8]# which php
/usr/local/php8/bin/php
#檢視php的版本資訊
[[email protected] 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

#映射頭檔案
[[email protected] php8]# ln -s /usr/local/php8/include /usr/include/php
[[email protected] php8]# ll /usr/include/php
lrwxrwxrwx 1 root root 23 Oct 11 16:43 /usr/include/php -> /usr/local/php8/include

#配置php-fpm
[[email protected] php8]# cd /usr/local/src/php-8.1.11/
[[email protected] php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-8.1.11]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[[email protected] 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檔案
[[email protected] ~]# 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

[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[[email protected] ~]# 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配置

[[email protected] ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
   phpinfo();
?>
EOF
#修改配置檔案
[[email protected] ~]# 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;
        }
...............

#重載服務,生效配置
[[email protected] ~]# systemctl reload nginx.service

#因預先存在index.html,更名後才能直接通路index.php
[[email protected] ~]# cd /usr/local/nginx/html/
[[email protected] html]# mv index.html{,.bak}
[[email protected] html]# ls
50x.html  index.html.bak  index.php
           

php配置

[[email protected] ~]# 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。取消該行注釋(;)
[[email protected] ~]# systemctl restart php.service

#nginx端與php兩邊都存在該檔案才能通路成功
[[email protected] ~]# mkdir -p /usr/local/nginx/html
[[email protected] ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
   phpinfo();
?>
EOF
           
LNMP網站架構

繼續閱讀