搭建LNMP实现分离(一)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 搭建LNMP实现分离

     实验目标:使用一台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。根据下图点击(现在就开始!)。

 

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

       点击现在安装。

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

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

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


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
11月前
|
域名解析 网络协议 关系型数据库
|
关系型数据库 MySQL 应用服务中间件
分离部署lnmp架构
分离部署lnmp架构
121 0
分离部署lnmp架构
|
关系型数据库 应用服务中间件 数据库
|
关系型数据库 测试技术 应用服务中间件
|
关系型数据库 应用服务中间件 PHP
|
Web App开发 关系型数据库 应用服务中间件
|
关系型数据库 MySQL 应用服务中间件
|
8月前
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。
217 0
|
8天前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建