Linux服务器技术教程之四搭建LAMP架构

举报
tea_year 发表于 2025/01/13 11:42:56 2025/01/13
251 0 0
【摘要】 LAMP(Linux-Apache-MySQL-PHP)架构是一套用于开发和部署动态网站及Web应用程序的开源软件包。Linux作为操作系统,提供了稳定且高效的运行环境。Apache作为Web服务器,负责处理HTTP请求。MySQL提供关系型数据库服务,用于数据存储和管理。PHP则是一种流行的服务器端脚本语言,用于实现网站的动态功能。本文详细描述了在Linux服务器上部署LAMP架构的过程。

LAMP(Linux-Apache-MySQL-PHP)架构是一套用于开发和部署动态网站及Web应用程序的开源软件包。Linux作为操作系统,提供了稳定且高效的运行环境。Apache作为Web服务器,负责处理HTTP请求。MySQL提供关系型数据库服务,用于数据存储和管理。PHP则是一种流行的服务器端脚本语言,用于实现网站的动态功能。本文详细描述了在Linux服务器上部署LAMP架构的过程。

Apache:主要用于接收用户的请求,处理业务逻辑,返回结果给客户端(浏览器)

PHP:编程语言的一种,主要应用于Web开发。主要实现注册、登录、加入购物车、下单、支付等功能

MySQL:数据库,删库到跑路,这里的"库"就是指数据库。永久保存数据

任务


场景:程序员书写了一个php代码发给我们部署下。

  1. 测试能否直接部署到nginx中?

  2. 需要什么服务,是什么原理?

  3. 准备服务并测试。

  4. 部署代码。

1.概述

网站架构 说明与特点 性能 说明
静态网站 网站仅仅包含:html,css样式,js脚本,图片,视频 nginx直接处理静态资源 只要是web服务器即可:nginx,可以承受较高的访问量 不支持动态的功能,注册,评论,静态网站功单一 浏览器端解析(客户端解析),服务端仅仅负责发送
动态网站 动态网站一般是通过开发语言实现:java,PHP,python,Golang 动态资源页面需要,服务器进行处理,处理后把结果返回给用户 动态请求需要服务端进行处理与解析,把结果给用户

如何区分静态资源和动态资源

通过url初步判断,网站的类型

url中包含&或?一般是动态网站

2. 常见的动态网站的架构

PHP LNMP,LAMP,WNMP/WAMP(P--->PHP,W--->Windows,A--->Apache)
JAVA LNMT(Tomcat,Jetty,Weblogic,Jboss)
Python LNMP(Python,uwsgi)
Golang LNMG(Golang)

2.1 关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

2.2 关闭SELinux

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是Linux历史上最杰出的新安全子系统。

获取SELinux的状态:

getenforce

临时关闭SElinux:重启后SELinux还会自动启动

setenforce 0

永久关闭SELinux:编辑SELinux的配置文件

vim /etc/selinux/config
# 打开后设置SELINUX=disabled,按 :wq保存退出



3. LAMP/LNMP架构细节

1238843-20230221215843474-1051637031.png


3.1 数据库

db01数据库:193.168.174.51/172.16.1.51

[root@db01 ~]# yum -y install mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
 
#数据库安全配置,仅仅刚安装的时候运行,仅首次运行即可,用于设置root密码,清理用户和临时数据库
[root@db01 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 #当前数据库密码为空,按回车键
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 #输入Y
Set root password? [Y/n] Y
#输入新密码和确认密码
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 #删除数据库中的匿名用户(没有用户名的用户,容易导致安全问题)
Remove anonymous users? [Y/n] Y     
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 #是否禁止root远程登录,也可以n
Disallow root login remotely? [Y/n] Y  
 
 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 #是否删除test测试用的数据库
Remove test database and access to it? [Y/n] Y  
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 #是否更新权限信息表
Reload privilege tables now? [Y/n] Y    
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
 
Thanks for using MariaDB![root@db01 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 190
Server version: 5.5.68-MariaDB MariaDB Server
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> 
 

数据库基本概念

库:database 相当于1个execl文件

表:table 相当于是1个execl文件的1个sheet

字段: 列 某列的名字,字段名字

记录:行 某个人的数据

#查看系统所有的数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)
 
#查看库里的表
MariaDB [(none)]> show tables from mysql;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)
 
#显示mysql库的user表的user列和host列 (列字段),查看数据库的用户信息
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| test | 172.16.1.% |
| root | ::1        |
| root | localhost  |
| test | localhost  |
+------+------------+
5 rows in set (0.00 sec)
 
#\G  显示的时候好看些
select  * from test.wp_posts \G;
 
#创建数据库
MariaDB [(none)]> create database test;
 
#创建用户,进行授权
grant 所有权限   on 数据库.* to  '用户名'@'白名单'
MariaDB [(none)]> grant all on test.* to 'test'@'localhost' identified by '123456';
MariaDB [(none)]> grant all on test.* to 'test'@'172.16.1.%' identified by '123456';
 
#查看用户授权
show grants for  'test'@'172.16.1.%';  
#删除数据库
MariaDB [(none)]> drop database test; 
#删除用户
drop user 'test'@'172.16.1.%';  
#刷新用户权限表
flush privileges; 
#测试远程连接
mysql -utest -p1  -h 172.16.1.51

数据库白名单说明:

白名单表示用户可以从哪里进行登录与使用数据库.

一般localhost表示只能在数据库本地使用.

可以通过172.16.1.% 进行授权局域网访问.

其他局域网机器可以访问数据库.

只给个%表示所有人可以访问(不安全). %不包含localhost.

3.2 php

yum install -y httpd httpd-devel mariadb-devel php php-mysql php-devel
​
​
vi /etc/httpd/conf/httpd.conf
#创建index.php
cd /var/www/html
​
<?php
  phpinfo();
?>
​
​
#/etc/httpd/conf/httpd.conf  42行

image-20240814075653289.png

部署root@'%'的权限,创建wordpress库;

3.3 wordpress

#上传到~
unzip wordpress-4.7.3-zh_CN.zip
​
cp -rvf wordpress/* /var/www/html/
​
 chmod 777 -R /var/www/html
 
cp wp-config-sample.php wp-config.php
​
vim wp-config.php 

image-20240814075431748.png


3.4测试

登录192.168.3.7或加端口88

image-20240814075814545.png

总结

LAMP平台的四个组件,每个组件都经历了数十年之久的企业应用的考验,各自都是同类软件中的佼佼者,从而成为典型的黄金搭档,其主要的优势体现在以下几个方面:
1成本低廉:各组件都是开放源代码的软件,可以自由获得和免费使用,在技术上和许可证方面没有太严格的限制,大大降低了企业的实施成本
2.可定制:拥有大量的额外组件和可扩展功能的模块,能过满足大部分企业应用的定制要求,甚至可以自行开发、添加新功能
3.易于开发:基于LAMP平台的动态网站中,页面代码简洁,与HTML标记语言的结合度非常好,即使是非专业的程序员也能够轻松读懂乃至修改网页代码
4.方便易用:PHP、Perl等属于解释性语言,开发的各种web程序不需要编译,方便进行移植使用。整套的网站项目程序,通常只要复制到网站目录中,就可以直接访问
5.安全和稳定:得益于开源的优势,大量的程序员在关注并持续改进LAMP平台的各个组件,发现问题能够很快得到解决。LAMP架构已经经历了数十年的长期验证,安全性、稳定性方面表现的也非常优秀
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。