LNMP搭建

LNMP

  1. L:Linux操作系统
  2. N:Nginx网站服务软件
  3. M:MySQL,MariaDB数据库
  4. P:网站开发语言(PHP,Perl,Python)

部署LNMP环境

一、装包

安装:nginx(web服务器),mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(其他客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器服务)、php-mysql(PHP的数据库扩展包)。

二、启服务

(1)重启相关服务

/usr/local/nginx/sbin/nginx
systemctl start  mariadb       
systemctl status mariadb           
systemctl enable mariadb           
systemctl restart php-fpm
systemctl enable php-fpm

(2)配置防火墙

firewall-cmd --set-default-zone=trusted
setenforce 0

三、构建LNMP平台

配置Fast-CGI支持PHP网页
创建PHP测试页面,测试使用PHP连接数据库的效果

Nginx结合FastCGI技术即可支持PHP页面架构

FastCGI工作原理:

工作流程:

  1. Web Server启动时载入FastCGI进程管理器
  2. FastCGI进程管理器初始化,启动多个解释器进程
  3. 当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个解释器
  4. FastCGI进程完成处理后返回结果,将标准输出和错误信息从同一个连接返回Web Server
    在这里插入图片描述
FastCGI简介

FastCGI技术目前支持的语言有PHPC/C++JavaPerlPythonRuby

FastCGI的缺点
内存消耗大

  • 因为是多进程,所以比CGI多线程消耗更多的服务器内存,PHP-CGI解释器每进程消耗7至25M内存,将这个数字乘以50或者100就是很大的内存数
  • Nginx+PHP(FastCGI)服务器在3万并发连接下,开10个Nginx进程,消耗150M内存(1015M)开64个php-cgi进程消耗1280M内存(20M64)

步骤:

  1. php-fpm配置文件
vim /etc/php-fpm.d/www.conf

[www]
listen = 127.0.0.1:9000            //PHP端口号
pm.max_children = 32                //最大进程数量
pm.start_servers = 15                //最小进程数量
pm.min_spare_servers = 5            //最少需要几个空闲着的进程
pm.max_spare_servers = 32            //最多允许几个进程处于空闲状态
  1. 修改Nginx配置文件并启动服务
vim /usr/local/nginx/conf/nginx.conf

location / {
        root   html;
        index  index.php  index.html   index.htm;
#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
 }
location  ~  \.php$  {
        root           html;
        fastcgi_pass   127.0.0.1:9000;    #将请求转发给本机9000端口,PHP解释器
        fastcgi_index  index.php;
        #fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi.conf;
}
nginx -s reload
  1. 创建PHP页面,测试LNMP架构能否解析PHP页面
  • 测试PHP
vim /usr/local/nginx/html/test.php

<?php
$i=33;
echo $i;
?>
  • 创建PHP测试页面,连接并查询MariaDB数据库。
vim /usr/local/nginx/html/test2.php

<?php
$mysqli = new mysqli('localhost','root','密码','mysql');
//注意:root为mysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可
//localhost是数据库的域名或IP,mysql是数据库的名称
if (mysqli_connect_errno()){
    die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
    printf("Host:%s",$row[0]);
    printf("</br>");
    printf("Name:%s",$row[1]);
    printf("</br>");
}
?>
  1. LNMP常见问题
    Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
    Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
    PHP默认错误日志文件为/var/log/php-fpm/www-error.log
    如果动态网站访问失败,可用参考错误日志,查找错误信息。
四、 地址重写

什么是地址重写

获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程

地址重写的好处

  • 缩短URL,隐藏实际路径,提高安全性
  • 易于用户记忆和键入
  • 易于被搜索引擎收录

1. 重定向首页文件

(1)

server {
        listen       80;
        server_name  www.a.com;
        rewrite /a.html /b.html;   #将a的访问重定向到b
        #auth_basic "Input Password:"; #认证提示符
        #auth_basic_user_file "/usr/local/nginx/pass"; #认证密码文件
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
              }
        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;
              }
 }

当用户访问www.a.com/a.html的时候就会重定向到b.html
(2)

.... ....
        server_name  www.a.com;
        rewrite /a.html /b.html redirect;   #将a的访问重定向到b
        #auth_basic "Input Password:"; #认证提示符
        #auth_basic_user_file "/usr/local/nginx/pass"; #认证密码文件
.... ....

加redirect的目的是,访问的时候地址栏会显示重定向以后的地址,不加就会隐藏重定向的真实地址
2.域名重定向

.... ....
    server_name  www.a.com;
    #rewrite /a.html /b.html redirect;
    rewrite ^/  http://www.baidu.com/;
.... ....

用户访问www.a.com的时候会跳转到百度

注:每次修改完nginx配置文件都记得nginx -s reload重新加载配置文件

3.重定向到相同的子界面

.... ....
    server_name  www.a.com;
    #rewrite /a.html /b.html redirect;
    rewrite /(.*)  http://www.baidu.com/$1; 
.... ....

#访问www.a.com的任意子界面,跳转到www.baidu.com的相同的子界面,如访问http://www.a.com/aaa则会跳转到http://www.baidu.com/aaa

4.根据访问的浏览器不同,返回不同的页面

vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
}
#这里,~符号代表正则匹配,*符号代表不区分大小写
      if ($http_user_agent ~* firefox) {            #识别客户端firefox浏览器
               rewrite ^(.*)$ /firefox/$1;    #两个文件夹下的文件的名称保持一致
      }
}

5.地址重写格式【总结】
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permament 永久重定向

文中所需软件包在

Github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值