关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

搭建LNMP实现分离(一)

发布时间:2023-06-30 18:01:31

      实验目标:使用一台linux服务器先搭建LNMP平台,安装两个论坛后实现LNMP分离。

实验拓扑图如下:

 

nginx理论

nginx日志格式:log_format

示例:

vim /etc/nginx/nginx.conf

http {

log_format  main  '$remote_addr - $remote_user [$time_iso8601] "$request" ' #定义日志输出格式main

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main; #调用日志格式main

}

nginx日志格式的变量

$remote_addr #记录客户端的ip地址

$remote_user #记录客户端的用户名

$time_local #通用的时间格式

$time_iso8601 #iso8601时间格式

$request #请求的方法和请求的HTTP协议

$status #请求状态码

$body_bytes_sent #服务器回应的字节数,不包含头部大小

$bytes_sent #服务器回应的总字节数

$msec #日志写入时间,单位为秒,精度为毫秒

$http_referer #记录链接访问源地址

$http_user_agent #记录客户端浏览器信息

$http_x_forwarded_for #代理服务器ip

$request_length #请求包的长度(请求头+请求正文)

$request_time #请求花费的时间,单位为秒,精度为毫秒

(经验: 1秒=1000毫秒(ms), 1毫秒=1/1000秒(s);1秒=1000000 微秒)

nginx的location

语法规则: location [=|~|~*|^~] /uri/ { … }

下列以优先级从高到低排序

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。

~ 开头表示区分大小写的正则匹配                    

~* 开头表示不区分大小写的正则匹配             

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则

/ 通用匹配,任何请求都会匹配到。

示例:测试匹配符的优先级

cd /etc/nginx/conf.d/

vim test.conf

添加:

server {

        listen 80;

        server_name test.benet.com;

        location / {

                default_type text/html;

                return 200 "location /";

        }

        location =/ {

                default_type text/html;

                return 200 "location =/";

        }

        location ~ / {

                default_type text/html;

                return 200 "location ~ /";

        }

        location ~* / {

                default_type text/html;

                return 200 "location ~* /";

        }

}

        保存退出

        客户端修改hosts文件,测试访问

真实企业场景配置:*

#通用匹配,任何请求都会匹配到。

location / {

}

#严格区分大小写,匹配.php结尾

location ~ \.php$ {

fastcgi_pass http://127.0.0.1:9000;

}

#严格区分大小写,匹配.jsp结尾

location ~ \.jsp$ {

proxy_pass http://127.0.0.1:8080;

}

#不区分大小写匹配

location ~* "\.(sql|bak|tgz|tar.gz|.git)$ {

default_type text/html;

return 403 "启用访问控制";

}

(注意:以上主要用于网页的动、静分离)

安装LNMP

1. 安装nginx

所需安装包如下:

安装并启动:

1. [root@nginx ~]# rpm -ivh /media/nginx-rpm/*.rpm --nodeps --force 2. 3. [root@nginx ~]# systemctl start nginx 4. 5. [root@nginx ~]# systemctl enable nginx 6. 7. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

   

2. 安装mysql(mariadb)

所需安装包如下:

安装mysql并启动

1. [root@nginx ~]# rpm -ivh /media/mysql5.6-rpm/*.rpm --nodeps --force 2. 3. [root@nginx ~]# systemctl start mysqld 4. 5. [root@nginx ~]# systemctl enable mysqld 6. 7. 创建mysql密码 8. 9. [root@nginx ~]# mysqladmin -uroot password 10. 11. New password: //输入新密码 12. 13. Confirm new password: //再次输入新密码

   

3. 安装PHP

所需安装包如下:

 

安装php并启动

1. [root@nginx ~]# rpm -ivh /media/php-rpm/*.rpm --nodeps --force 2. 3. [root@nginx ~]# systemctl start php-fpm 4. 5. [root@nginx ~]# systemctl enable php-fpm

   

应用安装

        本次php可以搭建两个应用wordpress和wecenter,两个app搭建一个论坛即可。如搭建两个app需要测试机本地解析域名,通过域名访问虚拟主机

搭建wordpress

注意:下面操作需要在同一台服务器操作,注意看服务器名称。

1. 下载并解压wordpree包到/下并解压授权。

1. [root@nginx ~]# cp -rp /media/wordpress-4.9.4-zh_CN.zip / 2. 3. [root@nginx ~]# cd / 4. 5. [root@nginx /]# unzip wordpress-4.9.4-zh_CN.zip 6. 7. [root@nginx /]# chmod -R 777 /wordpress

   

2. 创建虚拟主机配置文件

1. [root@nginx /]# vim /etc/nginx/conf.d/blog.conf 2. 3. server { 4. 5. listen 80; 6. 7. server_name www.blog.com; 8. 9. root /wordpress; 10. 11. index index.php index.html; 12. 13. 14. 15. location ~ \.php$ { 16. 17. root /wordpress; 18. 19. fastcgi_pass 127.0.0.1:9000; //php服务器地址,如分离需要指定PHP服务器。 20. 21. fastcgi_index index.php; 22. 23. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //与上一行是同一行,注意有空格 24. 25. include fastcgi_params; 26. 27. } 28. 29. } 30. 31. [root@nginx /]# nginx -t 32. 33. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 34. 35. nginx: configuration file /etc/nginx/nginx.conf test is successful 36. 37. [root@nginx /]# systemctl restart nginx //修改配置文件后需要重启或重载服务。

   

3. 创建blog数据库和管理用户

1. [root@nginx ~]# mysql -uroot -p123 2. 3. //省略部分内容 4. 5. mysql> create database blog; 6. 7. Query OK, 1 row affected (0.00 sec) 8. 9. 10. 11. mysql> grant all on blog.* to lisi@localhost identified by '123456'; 12. 13. Query OK, 0 rows affected (0.00 sec) 14. 15. 16. 17. mysql> exit 18. 19. Bye

   

4. 通过客户端服务器验证

        注意下面使用测试机1.10访问。

        因为只搭建了第一个app,所以直接访问IP即可,http://192.168.1.4,后台网址为http://192.168.1.4/wp-admin。根据下图点击(现在就开始!)。

 

        修改以下三项内容后点击提交。

        点击现在安装。

        创建站点标题,管理用户名及密码,如密码过于简单需要勾选确认使用弱密码,输入邮箱后点击安装即可。

        完成后使用用户密码登录即可。

        登录后可以看到论坛板块,根据需求修改添加即可。




/template/Home/leiyu/PC/Static