0
点赞
收藏
分享

微信扫一扫

部署LNMP

爱做梦的夏夏 2024-11-18 阅读 14

一、部署LNMP环境

环境说明

主机名 IP地址 角色

server1(已存在) eth0:192.168.88.254/24 客户端

proxy(已存在) eth0:192.168.88.5/24

eth1:192.168.99.5/24 web服务器

动态网站说明

安装部署LNMP环境实现动态网站解析

静态网站 在不同环境下访问,网站内容不会变化

动态网站 在不同环境下访问,网站内容有可能发生变化


目前的网站一般都会有动态和静态数据,默认nginx仅可以处理静态数据,用户访问任何数据都是直接返回对应的文件,如果如果访问的是一个脚本的话,就会导致直接返回一个脚本给用户,而用户没有脚本解释器,也看不懂脚本源代码

因此需要整合LNMP(Linux、Nginx、MySQL、PHP)实现动态网站效果

1)安装软件

为了不受到之前实验的影响,可以先删除nginx,重安装

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop  #如果nginx没有停止,执行命令停止,如果已经停止,无需执行

[root@proxy ~]# rm -rf /usr/local/nginx/    #删除nginx原有目录

[root@proxy ~]# cd /root/lnmp_soft/nginx-1.22.1/

[root@proxy nginx-1.22.1]# yum -y install gcc make openssl-devel pcre-devel

[root@proxy nginx-1.22.1]# ./configure --with-http_ssl_module

[root@proxy nginx-1.22.1]# make && make install  #编译安装

2)安装MariaDB,php和php-fpm

[root@proxy nginx-1.22.1]# yum -y install mariadb  mariadb-server mariadb-devel php php-mysqlnd php-fpm

mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-devel(依赖包)、php(识别php语言)、php-fpm(进程管理器服务)、php-mysqlnd(PHP的数据库扩展包)

3)启动服务

1)启动Nginx服务

[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx

[root@proxy nginx-1.22.1]# ss -antlp | grep 80

tcp   LISTEN 0      128          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=15507,fd=6),("nginx",pid=15506,fd=6))


2)启动MySQL服务

[root@proxy nginx-1.22.1]# systemctl enable --now mariadb  #加入开机自启并立即启动

[root@proxy nginx-1.22.1]# systemctl status mariadb  #查看服务状态


3)启动PHP-FPM服务

[root@proxy nginx-1.22.1]# systemctl enable --now php-fpm  #加入开机自启并立即启动

[root@proxy nginx-1.22.1]# systemctl status php-fpm          #查看服务状态


4)使用PHP测试页面

[root@proxy nginx-1.22.1]# cp /root/lnmp_soft/php_scripts/test.php /usr/local/nginx/html/   #拷贝动态网站测试页面到nginx中

使用浏览器访问192.168.88.5/test.php 则无法看到页面内容,而是会当成要下载的文件,因为无法解析php动态页面

二、配置动静分离

配置动静分离

通过调整Nginx服务端配置,实现以下目标:

配置Fast-CGI支持PHP网页解析

Fast-CGI是快速公共(通用)网关接口,可以连接如nginx等网站程序到网站的语言解释器(比如php) ,php-fpm进程使用了Fast-CGI解析动态网站页面

1)修改Nginx配置文件并启动服务

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

...

65         location ~ \.php$ {     #~是使用正则表达式匹配以.php结尾,\ 转义

66             root           html;

67             fastcgi_pass   127.0.0.1:9000;  #将请求转发给本机php-fpm的9000端口

68             fastcgi_index  index.php;       #网站默认页

69             include        fastcgi.conf;    #加载fastcgi配置文件

70         }  

2)修改 php-fpm配置文件

打开php-fpm配置文件,注意该配置文件中;(分号)是注释

[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf

38 listen = 127.0.0.1:9000     #更改php-fpm端口号(使用网络通信)

[root@proxy nginx-1.22.1]# systemctl restart php-fpm    #重启服务

[root@proxy nginx-1.22.1]# ss -antlp | grep 9000        #查看监听端口

LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*    users:(("php-fpm",pid=15808,fd=8),("php-fpm",pid=15807,fd=8),("php-fpm",pid=15806,fd=8),("php-fpm",pid=15805,fd=8),("php-fpm",pid=15804,fd=8),("php-fpm",pid=15803,fd=6))


了解:

[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf

115 pm.max_children = 50        #最大进程数量

120 pm.start_servers = 5        #最小进程数量

3)测试能否解析PHP页面

1)启动或者重加载nginx

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

再次使用浏览器访问192.168.88.5/test.php 可以看到页面内容


2)再测试连接数据库的PHP页面

可以参考lnmp_soft/php_scripts/mysql.php

[root@proxy nginx-1.22.1]# cp  /root/lnmp_soft/php_scripts/mysql.php  /usr/local/nginx/html    #拷贝动态网站测试页面到nginx中


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

http://192.168.88.5/mysql.php       #访问成功


然后修改数据库内容进行测试

[root@proxy nginx-1.22.1]# mysql   #进入数据库

MariaDB [(none)]> create user dc@localhost identified by '123';   #创建测试账户

MariaDB [(none)]> exit  #退出

浏览器访问192.168.88.5/mysql.php 可以看到新创建的用户

使用socket方式连接php-fpm

1)更改php-fpm配置文件

1)打开php-fpm配置文件

[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf

38 listen = /run/php-fpm/www.sock                #socket方式(使用进程通信)

55 listen.acl_users = apache,nginx,nobody        #添加nobody账户

[root@proxy nginx-1.22.1]# systemctl restart php-fpm    #重启服务


修改Nginx配置文件并启动服务

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

...

65         location ~ \.php$ {     #匹配以.php结尾

66             root           html;

67             fastcgi_pass   unix:/run/php-fpm/www.sock;  #将请求转发给php-fpm进程

68             fastcgi_index  index.php;

69             include        fastcgi.conf;        #加载fastcgi配置文件

70         }

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

使用浏览器访问192.168.88.5/test.php 可以看到页面内容

三、地址重写

地址重写语法

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


语法格式:

rewrite regex replacement flag

rewrite 旧地址   新地址    [选项]

1)修改配置文件(访问a.html重定向到b.html)

1)修改Nginx服务配置

[root@proxy nginx-1.22.1]# cd /usr/local/nginx/

[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf  #还原配置文件

cp: overwrite 'conf/nginx.conf'? y

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

.. ..

server {

       listen       80;

       server_name  localhost;

       rewrite  /a.html  /b.html;       #新添加地址重写,a.html重定向到b.html  

       ...

   location / {

       root   html;

       index  index.html index.htm;

   }

}

[root@proxy nginx]# echo "nginx-B~~" > /usr/local/nginx/html/b.html


2)重新加载配置文件

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


3)客户端测试

http://192.168.88.5/a.html          #内容显示的是nginx-B~~,但是地址栏没有发生变化,还是a.html页面


此时配置文件中直接写rewrite  /a.html  /b.html; 配置,在测试是其实会有些问题,比如在浏览器中访问时把192.168.88.5/a.html写成192.168.88.5/a.htmldc 或者写成 192.168.88.5/dc/a.html,访问都会正常显示b.html的页面,这是因为此时写的是只要包含a.html的都会跳转,没有进行精准匹配,可以进行以下修改,只有写a.html时才会正确跳转

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

.. ..

server {

       listen       80;

       server_name  localhost;

       rewrite  ^/a\.html$  /b.html;       #新添加地址重写,a.html重定向到b.html  

       ...

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

浏览器重新访问测试即可192.168.88.5/a.html,显示b.html页面内容

2)测试redirect选项

1)修改Nginx服务配置

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

.. ..

server {

       listen       80;

       server_name  localhost;

       rewrite ^/a\.html$  /b.html  redirect;      #新修改,redirect重定向

       ...

   location / {

       root   html;

       index  index.html index.htm;

   }

}

2)重新加载配置文件

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


3)浏览器测试,地址栏同时发生变化

http://192.168.88.5/a.html  #内容显示的是nginx-B~~,地址栏发生变化,是b.html页面

3)不同网站间跳转

修改Nginx服务配置实现访问192.168.88.5的请求重定向至www.tmooc.cn

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

.. ..

server {

       listen       80;

       server_name  localhost;

   rewrite /  http://www.tmooc.cn/;        #新修改,访问旧网站的任意内容都跳转到新网站

   location / {

       root   html;

       index  index.html index.htm;

   }

}


2)重新加载配置文件

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


3)客户端测试

http://192.168.88.5     #可以成功跳转

4)子页面重定向

修改配置文件(访问192.168.88.5/下面子页面,重定向至www.tmooc.cn/下相同的子页面)


1) 修改Nginx服务配置

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

.. ..

server {

       listen       80;

       server_name  localhost;

   rewrite  /(.*)  http://www.tmooc.cn/$1;     #新修改

   location / {

       root   html;

       index  index.html index.htm;

   }

}


2)重新加载配置文件

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


3)客户端测试

http://192.168.88.5/b.html      #成功跳转

5)实现不同浏览器跳转到不同页面

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

[root@proxy nginx]# mkdir  html/firefox

[root@proxy nginx]# echo  firefox~~  >  html/firefox/abc.html   #火狐专用页面

[root@proxy nginx]# echo  others~~  >  html/abc.html            #其他浏览器专用页面


火狐访问192.168.88.5/abc.html时可以看到html/firefox/abc.html里面内容

其他浏览器访问192.168.88.5/abc.html时可以看到html/abc.html里面内容


2) 修改Nginx服务配置

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

.. ..

server {

       listen       80;

       server_name  localhost;

       if ($http_user_agent ~* firefox) {  #如果用户使用了火狐浏览器

           rewrite (.*)  /firefox/$1;      #就进行地址重写,让用户看到火狐专用页面,否则就是其他页面;$http_user_agent是nginx的内置变量,包含了发起 HTTP 请求的客户端的用户代理(User-Agent)字符串,比如用的什么浏览器

       }

   location / {

       root   html;

       index  index.html index.htm;

}


3)重新加载配置文件

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


4)客户端测试

用火狐浏览器与其他浏览器访问相同地址192.168.88.5/abc.html,可以得到不同结果

火狐浏览器访问192.168.88.5/abc.html,得到结果firefox~~

其他浏览器访问192.168.88.5/abc.html,得到结果others~~

其他选项测试

redirect 临时重定向,状态码302

permanent 永久重定向,状态码301

last 不再读其他语句,但还会继续匹配其他location语句


1)修改Nginx服务配置

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

.. ..

server {

       listen       80;

       server_name  localhost;

       rewrite ^/a\.html$  /b.html  permanent;     #新修改

       ...

   location / {

       root   html;

       index  index.html index.htm;

   }

}

2)重新加载配置文件

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


3)浏览器测试,地址栏同时发生变化

http://192.168.88.5/a.html  #内容显示的是nginx-B~~,地址栏发生变化,是b.html页面



测试lastlast不再读其他语句

1)修改Nginx服务配置

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

.. ..

server {

       listen       80;

       server_name  localhost;

       rewrite /a.html /b.html;        #新修改

       rewrite /b.html /c.html;        #新修改

       ...

}

...

2)重新加载配置文件

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

[root@proxy nginx]# echo nginx-c~~ > html/c.html


3)浏览器测试

192.168.88.5/a.html #内容显示的是nginx-c~~



如果想要访问的是b.html的内容,可以做以下更改

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

.. ..

server {

       listen       80;

       server_name  localhost;

       rewrite /a.html /b.html last;       #新修改

       rewrite /b.html /c.html;

       ...

}

...


重新加载配置文件

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

浏览器测试访问

192.168.88.5/a.html #内容显示的是nginx-b~~



测试last会继续匹配其他location语句

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

...

   location / {                #此处为默认的location

           rewrite /a.html /b.html last;   #新添加

           root   html;

           index  index.html index.htm;

       }

   location /b.html {                #这里是新添加的location

           rewrite /b.html /c.html;

       }

...        

重新加载配置文件

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

客户端测试: http://192.168.88.5/a.html,显示为nginx-c~~


break 不再读其他语句,结束请求

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

...

   location / {

           rewrite /a.html /b.html break;        #break可以阻止后面的语句

           root   html;

           index  index.html index.htm;

       }

   location /b.html {

           rewrite /b.html /c.html;

       }

...        

重新加载配置文件

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

客户端测试: http://192.168.88.5/a.html,显示为nginx-b~~

举报

相关推荐

0 条评论