部署LNMP环境、构建LNMP平台、地址重写

工具—远程桌面查看器--VNC  172.40.50.118:8

51CTO博客、百度

nginx的status

###################################################

opration  linux高级运维

day 03

LVS[没有健康检查的功能]

while :

do

ping 192.168.2.100

if [ $?  -ne 0  ];then

sed  -i  ‘/2.100/  s/(.*)/#\1/’  nginx.conf

sleep  1

done

who

pgrep -t nginx

kill -9 24158

pstree -ap  | grep nginx

killall -9  nginx

配置 rsync 自动将虚拟机 3 上的页面自动同步到虚拟机 4,确保两台主机的页面一致:

#####################################

动态网站

Nginx[静态,动态]

页面是htmlmp3,mp4,txt,doc,pdf

动态脚本语言:shell,PHP简单、专门针对网站、只能做网站、javajsp)、Python豆瓣网

LNMP== ( Linux操作系统(底层)  +  Nginx网站服务软件(web+

 MariaDBmysql数据库  +  PHP|Python|Perl网站开发语言 )

192.168.4.5

软件包列表:LNMP

实验1:部署LNMP环境

nginx[web服务,接收用户的请求] 源码包

gccopenssl-develpcre-develzlib-devel

php  [解释器]

yum -y localinstall php-fpm-5.4.16<tab> [服务、监听9000端口]  rpm

mariadb [数据库客户端]

mariadb-server  [数据库服务器]

mariadb-devel   [依赖包]

php-mysql  [php连接mysql的扩展库文件]

vim  1.php

<?php      /开头

$i  = 33;    /变量都有个$

$j = 44;

if  ($i > $j ){

echo “i  is bigger”;

}

else{

echo “ j is bigger”;

}

echo $i;      /echo 默认不换行

echo  “hi”;     /每行以分号结尾

?>   /结尾

#php  1.php

启动所有服务:

nginx 启动[80]

systemctl start php-fpm 启动[9000]

systemctl start mariadb        启动[3306]

netstat -antpu |grep 80 | 9000 |3306

##########################################

实验2Nginx动静分离  (Nginx + FastCGI)

    nginx[静态]

root html

nginx[动态]

nginx转发给PHP:9000

判断,如果用户访问的是静态页面,则找到,直接返回

如果用户访问的是动态页面,则转发给9000

location匹配用户的地址栏,相当于一个if。支持正则表达式【~+正则】

# vim /usr/local/nginx/conf/nginx.conf

……

location ~ \.php${                /不论目录

root  html;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

include fastcgi.conf;

}

vim  /usr/local/nginx/html/test.php

<?php

phpinfo();

?>

# nginx -s reload

# curl http://192.168.4.6/test.php

常见问题:

1.出现下载页面 【配置文件中没有php转发】

nginx没有做动静分离的location和对应的fastcgi_pass

2.出现空白页面----检查php页面文件的语法格式

a) tailf nginx/logs/error.log 没有错误

b)   tailf /var/logphp-fpm/www-error.log

有错误提示!

3.File not found. 【转发设置不正确】防火墙[SELinux]

4.An error occurred(看日志error.log Connection refused)【转发给9000后,无响应,php未启动】

检查php-fpm是否启动成功,ip是否正确

##########################################

fastcgi是一种常驻型(long-live)的CGI

CGI解释器进程保持在内存中,进行维护与调度。目前支持语言有PHPC/C++JavaPerlPythonRuby

fastcgi缺点

内存消耗大,应为是多进程

php配置文件(很耗内存)/etc/php-fpm.d/www.conf

# grep -v "^;"  /etc/php-fpm.d/www.conf | grep -v "^$" /etc/php-

# ps aux | grep fpm

最少5个进程,最多50php进程

每个进程最少7-25兆内存

mairadb性能更好的execl65536

cp  /opt/lnmp_soft/php_scripts/mysql.php   /usr/local/nginx/html/

LNMP    1.nginx接受用户的请求

2.判断请求的是静态还是动态的页面 location

如果请求是静态页面,则nginx找到该页面直接给用户

如果请求的是php页面,则找到该页面,交给9000执行

执行到中间的某一行,可能需要连接数据库

new mysqli('localhost','root','','mysql')

手机上的数据库sqllite

##########################################

地址重写

  rewrite  正则   跳转后的URL [选项];

案例1:访问a.html跳转到b.html

vim /usr/local/nginx/conf/nginx.conf

... ...

server {

listen 80;

server_name localhost;

location / {

root  html;

rewrite  /a.html  /b.html  redirect;    

}

#echo "BB" > /usr/local/nginx/html/b.html

#nginx -s reload

#curl http://192.168.4.6/a.html

案例2:访问192.168.4.5跳转到www.tmooc.cn

#curl http://192.168.4.6/a.html

vim /usr/local/nginx/conf/nginx.conf

... ...

server {

listen 80

server_name localhost;

location / {

root  html;

rewrite   ^/     http://www.tmooc.cn;

}

附加:

访问旧的网站页面,跳转到新的网站相同页面

rewrite   ^/(.*)     http://www.jd.com/$1;

 保留和粘贴

案例3:不同浏览器访问相同页面返回结果不同

IE  (msie)  http://192.168.4.5/test.html

firefox  http://192.168.4.5/test.html

UC   http://192.168.4.5/test.html

nginx【内置变量】

vim /usr/local/nginx/conf/nginx.conf

  server {

... ...

if  ($http_user_agent   ~*   curl){        /*表示包含curl,不区分大小写

rewrite    ^/(.*)    /curl/$1;

}   

#cd  /usr/local/nginx/html

#echo "1" >test.html

#mkdir  curl

#echo "2" >curl/test.html

#nginx -s reload

firefox http://192.168.4.5/test.html

curl http://192.168.4.5/test.html

案例4:如果用户访问的页面不存则转到首页

vim /usr/local/nginx/conf/nginx.conf

 server {

... ...

if  (!-e   $request_filename){

rewrite   ^/    http://192.168.4.5;

}  

#nginx -s reload

rewrite  正则   URL   [选项]

rewrite  选项:

last 停止执行其他rewrite

break 停止执行其他rewrite,并结束请求

redirect 临时重定向

permament 永久重定向

 

 

 

 

Top 

 

案例1:部署LNMP环境 

案例2:构建LNMP平台 

案例3:地址重写 

1 案例1:部署LNMP环境

1.1 问题

安装部署Nginx、MariaDB、PHP环境

安装部署NginxMariaDBPHPPHP-FPM

启动NginxMariaDBFPM服务;

并测试LNMP是否工作正常。

1.2 方案

RHEL7系统中,源码安装Nginx,使用RPM包安装MariaDB、PHP、PHP-FPM软件。

操作过程中需要安装的软件列表如下:

nginx

mariadbmariadb-servermariadb-devel

phpphp-fpmphp-mysql

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装软件

1)使用yum安装基础依赖包

[root@svr5 ~]# yum -y groupinstall "Development tools"  "Additional Development"

[root@svr5 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel

.. ..

2)源码安装Nginx

[root@svr5 ~]# useradd –s /sbin/nologin  nginx

[root@svr5 ~]# tar -zxvf nginx-1.8.0.tar.gz

[root@svr5 ~]# cd nginx-1.8.0

[root@svr5 nginx-1.8.0]# ./configure   \

> --prefix=/usr/local/nginx    \

> --user=nginx   --group=nginx \

> --with-http_ssl_module

[root@svr5 ~]# make && make install

.. ..

3)安装MariaDB

Mariadb在新版RHEL7光盘中包含有该软件,配置yum源后可以直接使用yum安装:

[root@svr5 ~]# yum –y install   mariadb   mariadb-server   mariadb-devel

4)php和php-fpm

[root@svr5 ~]# yum –y  install  php

[root@svr5 ~]# tar  –xf  lnmp_soft-2017-03-28.tar.gz

[root@svr5 ~]# cd  lnmp_soft

[root@svr5 ~]# yum –y  localinstall php-fpm-5.4.16-36.el7_1.x86_64.rpm

[root@svr5 ~]# yum –y  install  php-mysql

步骤二:启动服务

1)启动Nginx服务

这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。

[root@svr5 ~]# service httpd stop                //如果该服务存在则关闭该服务

[root@svr5 ~]# chkconfig httpd off

[root@svr5 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

[root@svr5 ~]# netstat -utnlp | grep :80

tcp    0    0 0.0.0.0:80        0.0.0.0:*        LISTEN        32428/nginx         

2)启动MySQL服务

[root@svr5 ~]# systemctl start mariadb

[root@svr5 ~]# systemctl status mariadb

[root@svr5 ~]# systemctl enable mariadb

3)启动PHP-FPM服务

[root@svr5 ~]# systemctl start php-fpm

[root@svr5 ~]# systemctl status php-fpm

[root@svr5 ~]# systemctl enable php-fpm

2 案例2:构建LNMP平台

2.1 问题

沿用练习一,通过调整Nginx服务端配置,实现以下目标:

配置Fast-CGI支持PHP网页

创建PHP测试页面,测试使用PHP连接数据库的效果

2.2 方案

使用2台RHEL7虚拟机,其中一台作为LNMP服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.100),如图-1所示。

-1

Nginx结合FastCGI技术即可支持PHP页面架构,因此本案例,需要延续练习一的实验内容,通过修改Nginx及php-fpm配置文件实现对PHP页面的支持。

php-fpm需要修改的常见配置如下:

listen = 127.0.0.1:9000

pm.max_children = 32

pm.start_servers = 15

pm.min_spare_servers = 5

pm.max_spare_servers = 32

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:创建并修改php-fpm配置文件

1)查看php-fpm配置文件

[root@svr5 etc]# 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服务已经启动

[root@svr5 ~]# systemctl restart php-fpm

[root@svr5 ~]# systemctl status php-fpm

步骤二:修改Nginx配置文件并启动服务

[root@svr5 ~]# 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;

            fastcgi_index  index.php;

            #fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi.conf;

        }

[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload

步骤三:创建PHP页面,测试LNMP架构能否解析PHP页面

1)创建PHP测试页面1:

[root@svr5 ~]# vim /usr/local/nginx/html/test1.php

<?php

phpinfo();

?>

2)创建PHP测试页面,连接MariaDB数据库:

 [root@svr5 ~]# vim /usr/local/nginx/html/test2.php

<?php

$links=mysql_connect("localhost","root","密码");        

//注意:rootmysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可

if($links){

        echo "link db ok!!!";

}

else{

        echo "link db no!!!";

}

?>

3)创建PHP测试页面,连接并查询MariaDB数据库:

[root@svr5 ~]# vim /usr/local/nginx/html/test3.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>");

}

?>

4)客户端使用浏览器访问服务器PHP首页文档,检验是否成功:

[root@client ~]# firefox http://192.168.4.5/test1.php

[root@client ~]# firefox http://192.168.4.5/test2.php

[root@client ~]# firefox http://192.168.4.5/test3.php

3 案例3:地址重写

3.1 问题

沿用练习一,通过调整Nginx服务端配置,实现以下目标:

所有访问/image目录下资源的请求,重定向至/picture目录;

所有访问www.tarena.com的访问重定向至bbs.tarena.com

实现curl访问不同的页面。

3.2 方案

关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:

rewrite regex replacement flag

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改配置文件

1)修改Nginx服务配置:

[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  www.tarena.com;

location / {

    root   html;

index  index.html index.htm;

rewrite ^/ http://bbs.tarena.com/;            

//地址重写,访问www.tarena.com将被重定向至bbs.tarena.com

rewrite ^/image/(.*)$ /picture/$1 break;    

//所有访问/image目录下资源的请求,重定向至/picture目录

if ($http_user_agent ~* url) {                    //识别客户端curl浏览器

rewrite ^(.*)$ /curl/$1 break;

}

}

}

2)重新加载配置文件

[root@svr5 ~]# /usr/local/nginx/sbin/nginx  -s  reload

3)创建网页目录以及对应的页面文件:

[root@svr5 ~]# mkdir  -p  /usr/local/nginx/html/curl/picture

[root@svr5 ~]# echo "I am is curl page" > /usr/local/nginx/html/curl/test.html

[root@svr5 ~]# cp /usr/share/backgrounds/gnome/Road.jpg \

> /usr/local/nginx/curl/picture/test.jpg

步骤二:客户端测试

客户端使用浏览器测试各个页面的访问是否被重定向。

[root@svr5 ~]# curl http://192.168.4.5/test.html

[root@svr5 ~]# firefox http://192.168.4.5/test.html

[root@svr5 ~]# firefox http://192.168.4.5/images/test.jpg

 

猜你喜欢

转载自www.cnblogs.com/fuzhongfaya/p/8952649.html