LNMP简介
LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
原理:浏览器发送http request请求到服务器(Nginx),服务器响应并处理web请求,将一些静态资源(CSS,图片,视频等)保存服务器上,然后将php脚本通过接口传输协议(网关协议)PHP-FCGI(fast-cgi)传输给PHP-FPM(进程管理程序),PHP-FPM不做处理,然后PHP-FPM调用PHP解析器进程,PHP解析器解析php脚本信息。PHP解析器进程可以启动多个,进行并发执行。然后将解析后的脚本返回到PHP-FPM,PHP-FPM再通过fast-cgi的形式将脚本信息传送给Nginx.服务器再通过Http response的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现。
流程图
lnmp平台构建
环境说明:
系统 | IP | 需要安装的服务 |
---|---|---|
centos7 redhat7 |
192.168.201.140 | nginx mysql5.7 php |
环境准备:
//关闭防火墙与SELINUX
[root@wnz ~]# systemctl stop firewalld
[root@wnz ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@wnz ~]# setenforce 0
[root@wnz ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//配置网络源
[root@wnz ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@wnz ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[root@wnz ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@wnz ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安装nginx
//创建系统用户nginx
[root@wnz ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@wnz ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ epel-release
[root@wnz ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Marked install: Development Tools
//创建日志存放目录
[root@wnz ~]# mkdir -p /var/log/nginx
[root@wnz ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@wnz ~]# cd /usr/src/
[root@wnz src]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
//解压并编译安装
[root@wnz src]# tar xf nginx-1.18.0.tar.gz
[root@wnz src]# cd nginx-1.18.0
[root@wnz nginx-1.18.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@wnz nginx-1.18.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//nginx安装后配置
//配置环境变量
[root@wnz ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@wnz ~]# source /etc/profile.d/nginx.sh
//启动nginx
[root@wnz ~]# nginx
[root@wnz ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22