LNMP服务

部署LNMP环境
安装部署Nginx、MariaDB、PHP环境
安装部署Nginx、MariaDB、PHP、PHP-FPM;
启动Nginx、MariaDB、FPM服务;
并测试LNMP是否工作正常。

步骤一:安装软件
1)使用yum安装基础依赖包
yum -y install gcc openssl-devel pcre-devel zlib-devel

2)源码安装Nginx
useradd –s /sbin/nologin nginx 创建nginx用户
tar -zxvf nginx-1.8.0.tar.gz 
cd nginx-1.8.0
./configure \
--prefix=/usr/local/nginx \ 选择路径
--user=nginx --group=nginx \ 选择用户及组
--with-http_ssl_module 添加https模块
make && make install 编译及安装

3)安装MariaDB
Mariadb在新版RHEL7光盘中包含有该软件,配置yum源后可以直接使用yum安装:
yum –y install mariadb mariadb-server mariadb-devel

4)安装php和php-fpm
yum –y install php php-mysql
rmp -ivh php-fpm-5.4.16-36.el7_1.x86_64.rpm

步骤二:启动服务
1)启动Nginx服务
注意:如果服务器上已经启动了其他监听80端口的服务软件(如httpd),
则需要先关闭该服务,否则会出现冲突。
netstat -lantpu |grep :80 查看80端口是否被占用
/usr/local/nginx/sbin/nginx 启动Nginx服务
netstat -autnlp | grep :80

2)启动MySQL服务
systemctl start mariadb
systemctl status mariadb 查看mysql状态
systemctl enable mariadb

3)启动PHP-FPM服务
systemctl start php-fpm
systemctl status php-fpm
systemctl enable php-fpm

构建LNMP平台
LNMP服务器(192.168.4.5)、客户机(192.168.4.100)
Nginx结合FastCGI技术即可支持PHP页面架构,通过修改Nginx及php-fpm配置文件实现对PHP页面的支持。
FastCGI是一种常驻(Long-Live)型的CGI,将CGI解释器进程保持在内存中,进行维护与调度。
FastCGI缺点:内存消耗大,因为它是多进程,比CGI多线程消耗更多的服务器内存
PHP-CGI解释器每进程消耗7至25兆内存
Nginx+PHP(FastCGI)服务器在3万并发连接下
开10个Nginx进程消耗150M内存
开64个php-cgi进程消耗1280M内存(每个20M)

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

php-fpm需要修改的常见配置如下:
listen = 127.0.0.1:9000 PHP端口号
pm.max_children = 50 最大进程数量
pm.start_servers = 15 最小进程数量
pm.min_spare_servers = 5 最少需要几个空闲着的进程
pm.max_spare_servers = 32最多允许几个进程处于空闲状态

2.3 步骤
1)创建并修改php-fpm配置文件
vim /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = apache
group = apache
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

2)确认php-fpm服务已经启动
systemctl restart php-fpm
systemctl status php-fpm

步骤二:修改Nginx配置文件并启动服务
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000; php-fpm IP与端口
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf; 加载Fast-CGI参数文件
}

重启Nginx服务:
/usr/local/nginx/sbin/nginx -s reload

步骤三:创建PHP页面,测试LNMP架构能否解析PHP页面
1)创建PHP测试页面1:
vim /usr/local/nginx/html/test1.php
<?php
$i="This is a test Page";
echo $i;
?>

2)创建PHP测试页面,连接并查询MariaDB数据库:
vim /usr/local/nginx/html/test2.php
<?php
$mysqli = new mysqli('localhost','root','','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>");
}
?>

3)客户端使用浏览器访问服务器PHP首页文档,检验是否成功:
firefox http://192.168.4.5/test1.php
firefox http://192.168.4.5/test2.php

地址重写
获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程。
优点:缩短URL,隐藏实际路径提高安全性
易于用户记忆,易于被搜索引擎收录

rewrite选项:
格式:rewrite regex replacement flag
flag = break:停止执行其他的重写规则,完成本次请求
last:停止执行其他重写规则,根据URL继续搜索其他
location:地址栏不改变
redirect:302临时重定向,地址栏改变,爬虫不更新URL
permanent:301永远重定向,地址栏改变,爬虫更新URL

正则表达式匹配:
~:区分大小写匹配
!~:区分大小写不匹配
~*:不区分大小写匹配

! ~*:不区分大小写不匹配
例;所有访问a.html的请求,重定向到b.html;
所有访问192.168.4.5的请求重定向至www.baidu.com;
所有访问192.168.4.5/下面子页面,重定向至www.baidu.com/下相同的页面;
实现curl访问不同的页面。

步骤一:修改配置文件(访问a.html重定向到b.html)
1)修改Nginx服务配置:
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
rewrite /a.html /b.html; 
}
}

2)重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload

3)客户端测试
firefox http://192.168.4.5/a.html

步骤二:修改配置文件(访问192.168.4.5的请求重定向至www.baidu.com)
1) 修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite ^/ http://www.baidu.com/;
location / {
root html;
index index.html index.htm;
}
}

2)重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload

3)客户端测试
firefox http://192.168.4.5

步骤三:修改配置文件(访问192.168.4.5/下面子页面,重定向至www.baidu.com/下相同的页面)
1) 修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite ^/(.*) http://www.baidu.com/$1;
location / {
root html;
index index.html index.htm;
}
}

2)重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload

3)客户端测试
firefox http://192.168.4.5

步骤三:修改配置文件(实现curl和火狐访问相同连接返回的页面不同)
1) 修改Nginx服务配置
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
if ($http_user_agent ~ curl) { 识别客户端curl浏览器
rewrite ^(.
)$ /curl/$1 break;
}
}

2)创建网页目录以及对应的页面文件:
echo "I am Normal page" > /usr/local/nginx/html/test.html
mkdir -p /usr/local/nginx/html/curl/
echo "I am is curl page" > /usr/local/nginx/html/curl/test.html
cp /usr/share/backgrounds/gnome/Road.jpg \
/usr/local/nginx/curl/test.jpg

2)重新加载配置文件
/usr/local/nginx/sbin/nginx -s reload

4)客户端测试
firefox http://192.168.4.5/test.html
curl http://192.168.4.5/test.html
curl http://192.168.4.5/test.jsp


     本文转自夜流璃雨 51CTO博客,原文链接:http://blog.51cto.com/13399294/2060047,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值