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工作完成
知识拓展
FastCGI
CGI全称是"通用网关接口"(Common Gateway Interface),web服务器与你的或其它机器上的程序进行"交谈"的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等。
FastCGI是从CGI发展改进而来的。传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后将结果返回给HTTP服务器。这在处理高并发访问时几乎是不可用的。另外传统的CGI接口方式安全性也很差,现在已经很少使用了。
FastCGI接口方式采用C/S结构,可以将HTTP服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当HTTP服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。
Nginx+FastCG
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配置。
Nginx源码安装
环境说明
主机名 | IP地址 | 系统 |
---|---|---|
nginx | 192.168.137.132 | centos8 |
首先在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 ~]# cd nginx-1.20.2/
[root@Nginx nginx-1.20.2]#
//准备安装
##在配置前我们其实可以隐藏一下nginx版本号,以防版本泄露
[root@Nginx nginx-1.20.2]# vim src/core/nginx.h
define NGINX_VERSION "666" //可以修改这个参数
//开始配置
[root@Nginx nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \ //指定安装路径
> --user=nginx \ //指定用户和组
> --group=nginx \
> --with-debug \ //开启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]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@Nginx nginx]# source /etc/profile.d/nginx.sh
//[root@Nginx nginx]# nginx
[root@Nginx 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 [::]:*
//关闭服务,编写service文件,并设置服务开机自启
[root@Nginx nginx]# nginx -s stop
[root@Nginx nginx]# ss -anlt
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
[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
[root@Nginx ~]# systemctl daemon-reload
[root@Nginx ~]# systemctl enable --now 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 [::]:*
访问测试
源码安装MySQL
主机名 | IP 地址 | 系统 |
---|---|---|
MySQL | 192.168.137.136 | centos8 |
源码安装PHP
//关闭防火墙与selinux
[root@mysql ~]# setenforce 0
[root@mysql ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@mysql ~]# systemctl disable --now firewalld.service
//配置yum源
[root@mysql ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@mysql ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@mysql ~]# dnf clean all
[root@mysql ~]# dnf makecache
//安装依赖包
[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建组和用户
[root@mysql ~]# groupadd -r -g 306 mysql
[root@mysql ~]# useradd -rMs /sbin/nologin -g 306 -u 306 mysql
//下载MySQL软件包(由于我提前下好了就不演示了)
//解压软件至/usr/local/
[root@mysql ~]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# cd /usr/local/
[root@mysql ~]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
//添加环境变量
[root@mysql ~]# ls /usr/local/mysql
/usr/local/mysql
[root@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql ~]# . /etc/profile.d/mysql.sh
[root@mysql ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data/
[root@mysql ~]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 11 14:10 data
//初始化数据库
[root@mysql local]# cd mysql/
[root@mysql mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T06:17:47.651273Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T06:17:47.988813Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T06:17:48.101172Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T06:17:48.121576Z 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: 6d671acc-492c-11ed-96d4-000c29b5a221.
2022-10-11T06:17:48.122931Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T06:17:48.662374Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T06:17:48.755008Z 1 [Note] A temporary password is generated for root@localhost: nIk:=XkQs0l:
//请注意,这个命令的最后会生成一个临时密码,此处密码是 nIk:=XkQs0l:
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
//添加环境变量,映射头文件、库文件、man手册
[root@mysql ~]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@mysql ~]# source /etc/profile.d/mysql.sh
[root@mysql ~]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@mysql ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql ~]# ldconfig
[root@mysql ~]# echo 'MANPATH /usr/local/mysqcat l/man' >> /etc/man_db.conf
//生成配置文件
[root@mysql ~]# vim /etc/my.cnf
[root@mysql ~]# cat /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/mysql.err'.
SUCCESS!
[root@mysql ~]# ps -ef|grep mysql
root 15740 1 0 14:36 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 15928 15740 5 14:36 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mysql.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 15958 1855 0 14:36 pts/1 00:00:00 grep --color=automysql
//修改密码
//使用临时密码登录
[root@mysql ~]# mysql -uroot -pnIk:=XkQs0l:
[root@mysql ~]# mysql -uroot -pwjh123
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.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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
报错
[root@mysql ~]# mysql -uroot -pnIk:=XkQs0l:
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
//解决问题
[root@mysql ~]# dnf provides libncurses.so.5
[root@mysql ~]# dnf -y install ncurses-compat-libs
源码安装php
//下载php
[root@PHP ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
//下载依赖包
[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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip libzip-devel epel-release
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 ~]# tar xf php-8.1.11.tar.gz
[root@PHP ~]# cd php-8.1.11/
[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]# 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
//编写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 -anlt
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 [::]:*
连接nginx和php
php端
编写测试主页,这里的目录对应的是nginx存放主页的目录,也可以自定义目录
[root@PHP ~]# mkdir -p /usr/local/nginx/html
[root@PHP ~]# cat > /usr/local/nginx/html/index.php << EOF
<?php
phpinfo();
?>
//修改配置文件
[root@PHP ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
listen = 192.168.137.130:9000 //php服务监听的端口,(也就是服务开放端口)
listen.allowed_clients = 192.168.137.132 //nginx端ip,(允许访问的ip)
//重启服务生效配置
[root@PHP ~]# systemctl restart php.service
nginx端
//编写测试主页
[root@Nginx ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[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; //这里加上index.php访问页
}
……
location ~ \.php$ { //匹配以.php结尾的文件
root html; //在/usr/local/nginx/html目录下
fastcgi_pass 192.168.137.130: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 daemon-reload
[root@Nginx ~]# ss -anlt
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 [::]:*