目录
lnmp简介:
LNMP (linux、nginx、mysql、php)
LNMP原理分析:
做lnmp的原因是:nginx只能做静态页面,不能做动态页面。现在的网页都是动静结合的页面。单一使用nginx,代码无法解析,php是做解释器,mysql是存数据用的。
安装所需要的软件包:
- nginx
- mariadb、mariadb-server、mariadb-devel
- php、php-fpm、php-mysql
备注:mariadb (数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(其他客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器)、php-mysql(php的数据库扩展包)
案例一:部署LNMP环境
安装软件:
1、yum 安装基础依赖包;
[root@porxy ~]#yum install -y gcc pcre-devel openssl-devel
// gcc是c语言的解释器,pcre-devel是正则表达式,openssl-devel是用来加密和解密的。
2、源码安装Nginx;
[root@porxy ~]# useradd -s /sbin/nologin nginx //创建普通用户
[root@porxy ~]# tar -xf nginx-1.20.1.tar.gz
[root@porxy nginx-1.20.1]# ./configure \
> --user=nginx \ //指定用户
> --group=nginx \ //指定组
> --with-http_ssl_module \ //开启SSL加密功能
> --prefix=/usr/local/nginx //指定安装路径
[root@porxy nginx-1.20.1]# make && make install //编译(把英文转换成二进制的程序)
检查是否安装成功:
[root@porxy nginx]# ls //查看nginx下有没有东西
conf html logs sbin
[root@porxy ~]# netstat -ltunp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 51891/nginx: master
3、安装mariadb包
[root@porxy ~]# yum install -y mariadb mariadb-server mariadb-devel
4、安装php及php-mysql、php-fpm
[root@porxy ~]# yum install -y php php-mysql php-fpm
5、启动服务:
首先暂停httpd服务,因为nginx占用80端口,
1)启动nginx:
[root@porxy ~]#systemctl stop httpd //暂停httpd服务
[root@porxy ~]#/usr/local/nginx/sbin/nginx //启动nginx服务
[root@porxy ~]# netstat -tunlp | grep :80 //查看端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 51891/nginx: master
2)启动MYSQL:
[root@porxy ~]# systemctl start mariadb
[root@porxy ~]# systemctl status mariadb
[root@porxy ~]# netstat -ltunp | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 67670/mysqld
3)启动php:
[root@porxy ~]# systemctl start php-fpm
[root@porxy ~]# systemctl status php-fpm
[root@porxy ~]# netstat -ltunp | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 67761/php-fpm: mast
理论知识:
用户访问nginx(起的是web服务器的作用),socket相当于php-fpm,这时用的是多进程,php可以调用多了进程(具体起多少进程,取决于php主配置文件)。
多进程与单进程的好处:
单进程打开很多网页时,只能一个一个打开;多进程可以同时打开;
多进程的缺点是:对内存的消耗较大。
nginx配置动静分离:
用户通过Nginx访问数据,
如果用户访问的是静态数据,则到/usr/local/nginx/html目录寻找到后,直接返回。
如果访问的是动态脚本,则到usr/local/nginx/html目录寻找到后,转发给php(或者是9000端口)。
location使用俩匹配用户的地址栏(支持正则),从/开始匹配
案例二:构建LNMP平台
1)修改nginx配置文件并启动文件
[root@porxy ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.35.134;
location / { #处理静态信息
root html;
index index.php index.html index.htm;
}
location /favicon.ico {
log_not_found off;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ { #处理动态信息
root html;
fastcgi_pass 127.0.0.1:9000; #将请求转发
fastcgi_index index.php;
include fastcgi.conf; #加载其他配置文件
}
}
[root@porxy ~]# /usr/local/nginx/sbin/nginx -s reload
//此时没有报错就说明语法没有错误
2)、创建php页面,并验证
[root@porxy ~]# vim /usr/local/nginx/html/test.php
<?php
$i=33;
echo $i;
?>
:x
[root@porxy ~]# php /usr/local/nginx/html/test.php //执行这个脚本,结果是不换行的。
33[root@porxy ~]#
客户端验证:
[root@client ~]# curl http://192.168.35.5/test.php
33[root@client ~]#
浏览器验证:
http://192.168.35.134/test.php
3)、创建php测试页面,连接并查询MariaDB数据库
[root@porxy ~]# vim /usr/local/nginx/html/mysql.php //mysql.php脚本开发会给。所以脚本略
客户端验证:
[root@client ~]# curl http://192.168.35.134/mysql.php
总结lnmp常见错误:
错误查询位置:
Nginx的默认访问日志为: /usr/log/nginx/logs/access.log
Nginx的默认错误日志为: /usr/log/nginx/logs/error.log
PHP的默认错误日志为: /var/log/php-fpm/www-error.log
错误类型:
1、其中一个服务没有启动
2、没有安装php-mysql
3、脚本编写错误
如果动态网站访问错误,查看错误日志,寻找错误信息。