Linux常见服务器配置(三):MariaDB数据库管理和WEB服务器
MariaDB 数据库管理
介绍 MariaDB
数据库介绍
数据库是以一定方式存储在计算机中的数据集合,其核心目的是方便数据的存储、管理、查询和维护。它是现代应用系统中的核心组件之一。数据库通常通过数据库管理系统(DBMS)来进行操作。
数据库的优势:
数据集中管理,避免冗余
数据共享性强,支持多用户访问
数据一致性与完整性高
可扩展性好,便于后期维护
提供强大的查询语言(如 SQL)
数据库种类
数据库按照数据模型可分为以下几类:
类别 | 简介 | 举例 |
---|---|---|
关系型数据库 | 使用表格(关系)来存储数据,数据之间通过外键关联 | MySQL、PostgreSQL、Oracle |
非关系型数据库 | 不以表结构存储数据,适合存储海量和非结构化数据 | MongoDB、Redis、Cassandra |
文档型数据库 | 数据以 JSON、XML 等格式的文档形式存储 | MongoDB |
键值型数据库 | 每个数据项由键值对构成,访问速度快 | Redis、Riak |
图数据库 | 用于存储复杂网络关系,如社交图谱、推荐系统 | Neo4j、ArangoDB |
时序数据库 | 主要用于处理时间序列数据,如监控、日志 | InfluxDB、Prometheus |
关系数据库
关系数据库是基于关系模型构建的数据库系统,使用二维表(行和列)来组织数据,每个表对应一个关系。
关系数据库的特点
数据以表格形式存储
使用结构化查询语言(SQL)进行操作
支持事务(ACID)
数据间可通过主键和外键建立关联
MariaDB 介绍
MariaDB 是 MySQL 的一个分支,由 MySQL 的创始人于 Oracle 收购后创建,以保持其开源性和社区控制。
MariaDB 的特点
完全开源,兼容 MySQL
使用与 MySQL 相同的 SQL 语法和 API
提供了更强的存储引擎支持(如 Aria、ColumnStore)
性能优化方面优于 MySQL(例如并行查询)
适用于中小型到大型系统
部署 MariaDB
安装 MariaDB
# 安装[root@server ~ 09:57:54]# yum install -y mariadb-server[root@client ~ 09:58:00]# yum install -y mariadb# 启动服务[root@server ~ 09:59:47]# systemctl enable mariadb --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.# 授予所有权限基于所有数据库中所有表给yuxb从任何客户端用户密码为123MariaDB [(none)]> grant all privileges on *.* to yuxb@'%' identified by '123';Query OK, 0 rows affected (0.00 sec)
加固 MariaDB
交互式提示您进行更改,包括
为root帐户设置密码。
禁止root帐户从本地主机外部访问数据库。
删除匿名用户帐户。
删除用于演示的test数据库。
[root@server ~ 10:00:34]# mysql_secure_installation
连接 MariaDB
MariaDB客户端可以通过两种方式连接到服务器:
# 登录# quit退出[root@server ~ 10:01:57]# mysql -u root -pEnter password: Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 11Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]>[root@server ~ 10:02:54]# mysql -u root -p123Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 12Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]> # client端链接[root@client ~ 09:58:29]# mysql -u yuxb -p123 -h 10.1.8.10Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 14Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]>
配置 MariaDB
默认情况下,MariaDB 侦听系统中所有网络地址上3306/TCP端口。
MariaDB 配置文件
主配置文件 /etc/my.cnf
辅助配置文件 /etc/my.conf.d/*
[root@client ~ 10:22:53]# vim /etc/my.cnf.d/client.cnf[client]user=yuxbpassword=123host=10.1.8.10port=3306
MariaDB 中 SQL
描述 SQL
SQL是用于访问和操作关系型数据库的标准语言。它是一种声明式语言,用户只需描述“做什么”,而不必关心“怎么做”。
SQL 主要用于
查询数据
插入数据
更新数据
删除数据
创建和管理数据库对象(如表、视图、索引、用户等)
控制访问权限
连接数据库
[root@server ~ 10:31:37]# mysql -uroot -p123Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 19Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]>
数据库操作
查询数据库列表
MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema |+--------------------+3 rows in set (0.00 sec)MariaDB [(none)]> create database wordpress;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || wordpress |+--------------------+4 rows in set (0.00 sec)
删除数据库
MariaDB [mysql]> drop database wordpress ;Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema |+--------------------+3 rows in set (0.00 sec)
表操作
环境准备
# 导入备份[root@server ~ 10:36:23]# mysql -uroot -p123 inventory < inventory.dump
查询表
查询表列表
MariaDB [(none)]> use inventory; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed MariaDB [inventory]> show tables; +---------------------+ | Tables_in_inventory | +---------------------+ | category | | manufacturer | | product | +---------------------+ 3 rows in set (0.00 sec)
查询表结构
MariaDB [inventory]> desc product; +-----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | NO | | NULL | | | price | double | NO | | NULL | | | stock | int(11) | NO | | NULL | | | id_category | int(11) | NO | | NULL | | | id_manufacturer | int(11) | NO | | NULL | | +-----------------+--------------+------+-----+---------+----------------+ 6 rows in set (0.00 sec)
输出显示,表格中有六列(属性):
Field 列,显示该属性名称。
Type 列,显示该属性的数据必须采用的格式。例如,stock属性必须是最多11位数字的整数。
Null 列,指示此属性是否可以为null。
Default 列,指示如果未指定该属性,则是否设置了默认值。
Key 列,显示属性ID是primary key。 主键是表中一行的唯一标识符。 该属性的任何其他行都不能具有相同的值。
Extra列,提供该列额外信息,对于id字段标记为auto_increment。 这意味着每次将新项目插入表中时,该条目的属性值都会增加。 这样可以更轻松地使数字主键保持唯一。
查询表中数据
查询表中所有记录所有字段
MariaDB [inventory]> select * from product; +----+-------------------+---------+-------+-------------+-----------------+ | id | name | price | stock | id_category | id_manufacturer | +----+-------------------+---------+-------+-------------+-----------------+ | 1 | ThinkServer TS140 | 539.88 | 20 | 2 | 4 | | 2 | ThinkServer RD630 | 2379.14 | 20 | 2 | 4 | | 3 | RT-AC68U | 219.99 | 10 | 1 | 3 | | 4 | X110 64GB | 73.84 | 100 | 3 | 1 | +----+-------------------+---------+-------+-------------+-----------------+ 4 rows in set (0.00 sec)
查询表中所有记录特定字段
MariaDB [inventory]> select name from product; +-------------------+ | name | +-------------------+ | ThinkServer TS140 | | ThinkServer RD630 | | RT-AC68U | | X110 64GB | +-------------------+ 4 rows in set (0.00 sec)
WHERE子句
MariaDB [inventory]> select name from product where id =1; +-------------------+ | name | +-------------------+ | ThinkServer TS140 | +-------------------+ 1 row in set (0.00 sec)MariaDB [inventory]> select * from product where id=1; +----+-------------------+--------+-------+-------------+-----------------+ | id | name | price | stock | id_category | id_manufacturer | +----+-------------------+--------+-------+-------------+-----------------+ | 1 | ThinkServer TS140 | 539.88 | 20 | 2 | 4 | +----+-------------------+--------+-------+-------------+-----------------+ 1 row in set (0.01 sec)
条件操作数
Operator | Description |
---|---|
= | Equal |
<> | Not equal. Note: In some versions of SQL, this operator may be written as != |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
BETWEEN | Between an inclusive range |
LIKE | Search for a pattern |
IN | Specify multiple possible values for a column |
多表查询
# 示例1:产品类型是Servers的产品名称和价格 MariaDB [inventory]> SELECT product.name,product.price-> FROM product,category-> WHERE product.id_category = category.id -> AND category.name='Servers'; +-------------------+---------+ | name | price | +-------------------+---------+ | ThinkServer TS140 | 539.88 | | ThinkServer RD630 | 2379.14 | +-------------------+---------+ 2 rows in set (0.00 sec)# 示例2:查询厂商是Lenovo的产品名称和价格 MariaDB [inventory]> SELECT product.name,product.price-> FROM product,manufacturer -> WHERE product.id_manufacturer = manufacturer.id-> AND manufacturer.name='Lenovo'; +-------------------+---------+ | name | price | +-------------------+---------+ | ThinkServer TS140 | 539.88 | | ThinkServer RD630 | 2379.14 | +-------------------+---------+ 2 rows in set (0.00 sec)
函数
# 示例1:查询产品价格平均值 MariaDB [inventory]> SELECT avg(price) FROM product; +-------------------+ | avg(price) | +-------------------+ | 803.2125000000001 | +-------------------+ 1 row in set (0.00 sec)# 示例2:查询产品价格最大值 MariaDB [inventory]> SELECT max(price) FROM product; +------------+ | max(price) | +------------+ | 2379.14 | +------------+ 1 row in set (0.00 sec)# 示例3:查询产品价格最小值 MariaDB [inventory]> SELECT min(price) FROM product; +------------+ | min(price) | +------------+ | 73.84 | +------------+ 1 row in set (0.00 sec)# 示例4:查询产品存量 MariaDB [inventory]> SELECT sum(stock) FROM product; +------------+ | sum(stock) | +------------+ | 150 | +------------+ 1 row in set (0.00 sec)# 示例5:查询产品价格最小值的那个产品信息 MariaDB [inventory]> SELECT min(price) FROM product; +------------+ | min(price) | +------------+ | 73.84 | +------------+ 1 row in set (0.00 sec)MariaDB [inventory]> SELECT * FROM product WHERE price=73.84; +----+-----------+-------+-------+-------------+-----------------+ | id | name | price | stock | id_category | id_manufacturer | +----+-----------+-------+-------+-------------+-----------------+ | 4 | X110 64GB | 73.84 | 100 | 3 | 1 | +----+-----------+-------+-------+-------------+-----------------+ 1 row in set (0.00 sec)MariaDB [inventory]> SELECT * FROM product WHERE price=(SELECT min(price) FROM product); +----+-----------+-------+-------+-------------+-----------------+ | id | name | price | stock | id_category | id_manufacturer | +----+-----------+-------+-------+-------------+-----------------+ | 4 | X110 64GB | 73.84 | 100 | 3 | 1 | +----+-----------+-------+-------+-------------+-----------------+ 1 row in set (0.00 sec)# 示例6:查询Lenovo厂商提供了几种产品 MariaDB [inventory]> SELECT count(product.name)-> FROM product,manufacturer -> WHERE product.id_manufacturer = manufacturer.id-> AND manufacturer.name='Lenovo'; +---------------------+ | count(product.name) | +---------------------+ | 2 | +---------------------+ 1 row in set (0.00 sec)# GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组。 MariaDB [inventory]> SELECT id_category,sum(stock) FROM product GROUP BY id_category; +-------------+------------+ | id_category | sum(stock) | +-------------+------------+ | 1 | 10 | | 2 | 40 | | 3 | 100 | +-------------+------------+ 3 rows in set (0.00 sec)
创建表
MariaDB [inventory]> CREATE TABLE staff(-> id INT(11) NOT NULL,-> name VARCHAR(100) NOT NULL,-> age INT(11) DEFAULT 10,-> id_department INT(11) -> ); Query OK, 0 rows affected (0.01 sec)MariaDB [inventory]> show tables; +---------------------+ | Tables_in_inventory | +---------------------+ | category | | manufacturer | | product | | staff | +---------------------+ 4 rows in set (0.00 sec)
插入记录
MariaDB [inventory]> INSERT INTO staff (id,name,age,id_department)-> VALUES (1,'yuxb1',28,10); Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> INSERT INTO staff (id,name,age) VALUES (2,'yuxb2',20); Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> INSERT INTO staff (id,name) VALUES (3,'yuxb3'); Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff; +----+-------+------+---------------+ | id | name | age | id_department | +----+-------+------+---------------+ | 1 | yuxb1 | 28 | 10 | | 2 | yuxb2 | 20 | NULL | | 3 | yuxb3 | 10 | NULL | +----+-------+------+---------------+ 3 rows in set (0.01 sec)
更新记录
MariaDB [inventory]> UPDATE staff SET age=30 WHERE id=3; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0MariaDB [inventory]> UPDATE staff SET age=30-> ; Query OK, 2 rows affected (0.00 sec) Rows matched: 3 Changed: 2 Warnings: 0MariaDB [inventory]> select * from staff; +----+-------+------+---------------+ | id | name | age | id_department | +----+-------+------+---------------+ | 1 | yuxb1 | 30 | 10 | | 2 | yuxb2 | 30 | NULL | | 3 | yuxb3 | 30 | NULL | +----+-------+------+---------------+ 3 rows in set (0.00 sec)
删除记录
MariaDB [inventory]> DELETE FROM staff WHERE id=3 ; Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> DELETE FROM staff ; Query OK, 2 rows affected (0.00 sec)MariaDB [inventory]> select * from staff; Empty set (0.01 sec)
删除表
MariaDB [inventory]> DROP TABLE staff ; Query OK, 0 rows affected (0.00 sec)MariaDB [inventory]> show tables; +---------------------+ | Tables_in_inventory | +---------------------+ | category | | manufacturer | | product | +---------------------+ 3 rows in set (0.00 sec)
管理 MariaDB 用户
创建用户账户
MariaDB [(none)]> create user laowang@'%' identified by '123'-> ; Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> select host,user,password from user where user = 'laowang'; +------+---------+-------------------------------------------+ | host | user | password | +------+---------+-------------------------------------------+ | % | laowang | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | +------+---------+-------------------------------------------+
控制用户权限
[root@server ~ 13:50:23]# mysql -u laowang -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 27 Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]> MariaDB [(none)]> select user(); +-------------------+ | user() | +-------------------+ | laowang@localhost | +-------------------+ 1 row in set (0.00 sec)MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec)# 什么也弄不了 MariaDB [(none)]> use mysql; ERROR 1044 (42000): Access denied for user 'laowang'@'%' to database 'mysql' MariaDB [(none)]> create database inventory; ERROR 1044 (42000): Access denied for user 'laowang'@'%' to database 'inventory' MariaDB [(none)]>
查询用户权限
MariaDB [(none)]> show grants for root@localhost; +----------------------------------------------------------------------------------------------------------------------------------------+ | Grants for root@localhost | +----------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +----------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
授予用户权限
MariaDB [(none)]> grant select,update,delete,insert on inventory.category to laowang@'%'; Query OK, 0 rows affected (0.00 sec)
验证权限
[root@server ~ 14:13:51]# mysql -u laowang -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 44 Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]> use inventory; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed MariaDB [inventory]> select * from category; +----+------------+ | id | name | +----+------------+ | 1 | Networking | | 2 | Servers | | 3 | Ssd | +----+------------+ 3 rows in set (0.00 sec)MariaDB [inventory]>
回收用户权限
MariaDB [(none)]> revoke select,update,delete,insert on inventory.category from laowang@'%'; Query OK, 0 rows affected (0.00 sec)
删除用户
如果删除了当前连接的帐户,则在关闭连接之前不会删除该帐户,关闭连接之后才会删除该帐户。
MariaDB [(none)]> drop user laowang@'%'; Query OK, 0 rows affected (0.00 sec)
更改用户密码
# root用户修改普通用户账户密码 MariaDB [mysql]> UPDATE user SET password=PASSWORD('mypass') where user='laowang' and host='%'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0# 或者 MariaDB [(none)]> SET PASSWORD FOR 'laowang'@'%' = PASSWORD('mypass');# 普通用户修改自己账户密码 MariaDB [(none)]> SET PASSWORD = PASSWORD('mypass'); MariaDB [(none)]> FLUSH PRIVILEGES;
备份和恢复
备份方式
物理备份
# 先停止服务 [root@server ~ 14:45:18]# systemctl stop mariadb [root@server ~ 14:45:37]# systemctl status mariadb ● mariadb.service - MariaDB database serverLoaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)Active: inactive (dead) since 五 2025-08-08 14:45:37 CST; 4s agoProcess: 1557 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)Process: 1555 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)Process: 1472 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)Main PID: 1555 (code=exited, status=0/SUCCESS)8月 08 10:00:00 server.yuxb.cloud mariadb-prepare-db-dir[1472]: The latest information about Ma.... 8月 08 10:00:00 server.yuxb.cloud mariadb-prepare-db-dir[1472]: You can find additional informa...: 8月 08 10:00:00 server.yuxb.cloud mariadb-prepare-db-dir[1472]: http://dev.mysql.com 8月 08 10:00:00 server.yuxb.cloud mariadb-prepare-db-dir[1472]: Consider joining MariaDB's stro...: 8月 08 10:00:00 server.yuxb.cloud mariadb-prepare-db-dir[1472]: https://mariadb.org/get-involved/ 8月 08 10:00:00 server.yuxb.cloud mysqld_safe[1555]: 250808 10:00:00 mysqld_safe Logging to '/...'. 8月 08 10:00:00 server.yuxb.cloud mysqld_safe[1555]: 250808 10:00:00 mysqld_safe Starting mysq...ql 8月 08 10:00:02 server.yuxb.cloud systemd[1]: Started MariaDB database server. 8月 08 14:45:34 server.yuxb.cloud systemd[1]: Stopping MariaDB database server... 8月 08 14:45:37 server.yuxb.cloud systemd[1]: Stopped MariaDB database server. Hint: Some lines were ellipsized, use -l to show in full.# 备份 [root@server ~ 14:45:41]# cp -r /var/lib/mysql{,.back} [root@server ~ 14:46:39]# systemctl start mariadb# 删除用户 [root@server ~ 14:46:56]# mysql -uroot -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]> drop user root@localhost; Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> drop user root@127.0.0.1; Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> quit Bye# 会发现登不上了 [root@server ~ 14:48:41]# mysql -uroot -p123 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)# 开始恢复 # 改权限的时候用下面两个直接就能修改 # chmod 660 /var/lib/mysql/mysql/user.* # chown mysql:mysql /var/lib/mysql/mysql/user.* [root@server ~ 14:48:42]# systemctl stop mariadb [root@server ~ 14:49:19]# cd /var/lib/mysql [root@server mysql 14:49:36]# ls aria_log.00000001 ibdata1 ib_logfile1 mysql aria_log_control ib_logfile0 inventory performance_schema [root@server mysql 14:49:37]# /bin/cp /var/lib/mysql.back/mysql/user.* mysql [root@server ~ 14:50:47]# cd /var/lib/mysql.back/mysql/ [root@server mysql 14:51:11]# chmod 660 user.* [root@server mysql 14:51:22]# cd .. [root@server mysql.back 14:51:27]# chown mysql:mysql mysql/user.* [root@server ~ 14:52:06]# systemctl start mariadb# 验证 [root@server ~ 14:52:11]# mysql -uroot -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]>
WEB 服务器
WEB 服务器简介
Web 服务器(Web Server)是指一种接收客户端(如浏览器)发送的 HTTP 请求,并返回网页内容或资源的程序或设备。它是万维网(WWW)的核心组成部分。
Web 服务器的主要功能
功能 | 说明 |
---|---|
处理请求 | 接收来自浏览器的 HTTP 请求,如访问网页、提交表单等。 |
响应请求 | 将 HTML 文件、图片等资源返回给客户端。 |
支持动态内容 | 配合 CGI、PHP、Java、Python 等脚本语言,生成动态网页。 |
虚拟主机 | 在一台服务器上运行多个网站,每个网站独立访问。 |
安全控制 | 支持访问控制、SSL/TLS 加密传输等,提高数据安全性。 |
日志记录 | 记录访问日志,便于运维分析和排错。 |
常见web服务器
Apache
Apache HTTP Server 是最早、最流行的开源 Web 服务器之一,由 Apache 软件基金会维护。
特点:
支持模块化扩展(如 PHP、SSL、URL 重写等);
支持
.htaccess
文件进行目录级配置;跨平台(支持 Linux、Windows 等);
功能强大但资源消耗相对较高;
应用场景:适用于大多数网站,尤其是中小型 Web 应用。
Nginx
Nginx(Engine X) 是一个轻量级、高性能的 Web 服务器和反向代理服务器。
特点:
异步非阻塞架构,适合高并发场景;
占用内存少,处理速度快;
可用于负载均衡、反向代理、缓存加速等;
配置简单,稳定性高;
应用场景:高流量网站、API 网关、动静分离部署。
Lighttpd
Lighttpd(Lighty) 是一个专为高性能环境设计的小巧型 Web 服务器。
特点:
占用资源极低,适合嵌入式系统或小型服务器;
支持 FastCGI、SCGI、URL 重写等;
配置简洁,适合静态网站;
应用场景:资源受限环境、小型网站或轻量级应用。
Tomcat
Apache Tomcat 是一个开源的 Java Web 应用服务器,由 Apache 基金会开发。
特点:
原生支持 Servlet 和 JSP;
可以处理动态 Java Web 内容;
本质上是一个 Java 容器(并非传统意义上的 Web Server);
应用场景:基于 Java 开发的网站或系统(如 Spring Boot 应用)。
IIS
Internet Information Services(IIS) 是微软开发的 Web 服务器,仅适用于 Windows 系统。
特点:
与 Windows 系统和 .NET Framework 深度集成;
图形化界面配置,易于管理;
支持 ASP.NET、PHP 等语言;
应用场景:企业或政府内部网站、基于 Microsoft 技术栈的项目。
Nginx
Nginx是一款高性能的HTTP和反向代理服务器,能够选择高效的epoll、kqueue、eventport最为网络I/O模型,在高连接并发的情况下,能够支持高达5万个并发连接数的响应,而内存、CPU等系统资源消耗却非常低,运行非常稳定。
安装 nginx
# 安装 nginx [root@server ~ 15:14:12]# yum install -y nginx# 启动 nginx [root@server ~ 15:14:30]# systemctl enable nginx --now Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.# 准备主页 [root@server ~ 15:15:13]# mv /usr/share/nginx/html/index.html{,.ori} [root@server ~ 15:15:35]# echo Hello World From Nginx > /usr/share/nginx/html/index.html# 防火墙 [root@server ~ 15:17:49]# firewall-cmd --add-service=http --permanent success [root@server ~ 15:17:51]# firewall-cmd --reload success# 测试 10.1.8.10# windows客户端修改C:\Windows\System32\drivers\etc\hosts
虚拟主机
同一个web服务器提供多个站点。
根据名称
[root@server ~ 15:14:11]# cd /etc/nginx/conf.d/ [root@server conf.d 15:51:45]# ls [root@server conf.d 15:51:46]# vim vhost-web1.conf [root@server conf.d 15:55:48]# vim vhost-web2.conf # 这个模板是在/etc/nginx里面的 nginx.conf [root@server conf.d 15:55:56]# cat vhost-web1.conf server {listen 80;listen [::]:80;server_name web1.yuxb.cloud;root /website/web1.yuxb.cloud;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf; }
[root@server nginx 15:56:52]# mkdir -p /website/web1.yuxb.cloud [root@server nginx 15:57:41]# mkdir -p /website/web2.yuxb.cloud [root@server nginx 15:57:44]# echo hello web1 > /website/web1.yuxb.cloud/index.html [root@server nginx 15:58:09]# echo hello web2 > /website/web2.yuxb.cloud/index.html [root@server nginx 15:58:16]# systemctl restart nginx.service # 验证 # 浏览器输入 http://web1.yuxb.cloud/ http://web2.yuxb.cloud/
配置SSL/TLS
生成证书
# 生成私钥 [root@server ~ 16:51:38]# openssl genrsa -out www.key 2048 Generating RSA private key, 2048 bit long modulus .........+++ ................+++ e is 65537 (0x10001)# 生成请求文件csr [root@server ~ 16:51:57]# openssl req -new -key www.key -out www.csr -subj "/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.yuxb.cloud/emailAddress=yuxb@yuxb.cloud"[root@server ~ 17:24:35]# openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt Signature ok subject=/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.yuxb.cloud/emailAddress=yuxb@yuxb.cloud Getting Private key
配置站点
[root@server conf.d 17:08:09]# vim vhost-ssl.conf [root@server conf.d 17:13:36]# cat vhost-ssl.conf server {listen 443 ssl http2;listen [::]:443 ssl http2;server_name www.yuxb.cloud;root /usr/share/nginx/html;ssl_certificate "/etc/pki/nginx/server.crt";ssl_certificate_key "/etc/pki/nginx/private/www.key"; }[root@server ~ 17:10:29]# mkdir -p /etc/pki/nginx/ [root@server ~ 17:13:40]# mkdir -p /etc/pki/nginx/private/[root@server ~ 17:16:39]# cp www.crt /etc/pki/nginx/www.crt [root@server ~ 17:13:52]# cp www.key /etc/pki/nginx/private/www.key[root@server conf.d 17:37:29]# ls -l /etc/pki/nginx/www.crt -rw-r--r-- 1 root root 1265 8月 8 17:35 /etc/pki/nginx/www.crt [root@server conf.d 17:39:26]# ls -l /etc/pki/nginx/private/www.key -rw-r--r-- 1 root root 1679 8月 8 17:13 /etc/pki/nginx/private/www.key[root@server ~ 17:35:02]# systemctl restart nginx[root@server ~ 17:39:13]# systemctl start firewalld.service [root@server ~ 17:40:13]# firewall-cmd --add-service=https --permanent success [root@server ~ 17:40:15]# firewall-cmd --reload success# 配置解析 [root@server ~ 17:45:51]# vim /etc/hosts [root@server ~ 17:46:48]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.1.8.10 www.yuxb.cloud# 测试 [root@server ~ 17:46:22]# curl http://www.yuxb.cloud/ hello web1 [root@server ~ 17:46:20]# curl -k https://www.yuxb.cloud/ Hello World From Nginx# 去浏览器输入验证 https://www.yuxb.cloud/