LNMP
- L:
Linux
操作系统 - N:
Nginx
网站服务软件 - M:
MySQL
,MariaDB
数据库 - 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工作原理:
工作流程:
- Web Server启动时载入FastCGI进程管理器
- FastCGI进程管理器初始化,启动多个解释器进程
- 当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个解释器
- FastCGI进程完成处理后返回结果,将标准输出和错误信息从同一个连接返回Web Server
FastCGI简介
FastCGI技术目前支持的语言有PHP
、C/C++
、Java
、Perl
、Python
、Ruby
等
FastCGI的缺点
内存消耗大
- 因为是多进程,所以比CGI多线程消耗更多的服务器内存,PHP-CGI解释器每进程消耗7至25M内存,将这个数字乘以50或者100就是很大的内存数
- Nginx+PHP(FastCGI)服务器在3万并发连接下,开10个Nginx进程,消耗150M内存(1015M)开64个php-cgi进程消耗1280M内存(20M64)
步骤:
- 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 //最多允许几个进程处于空闲状态
- 修改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
- 创建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>");
}
?>
- 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
永久重定向