avatar

刘刚刚的blog

采菊东篱下,悠然见南山🦥

  • 首页
  • 大模型应用
  • 常用软件/工具
  • Halo
  • 关于
Home <linux就该这么学> 第10章 使用Apache服务部署静态网站
文章

<linux就该这么学> 第10章 使用Apache服务部署静态网站

Posted 2021-06-29 Updated 2024-12- 17
By Administrator
44~57 min read

虽然Nginx越来越流行,Apache仍是目前市场占有率最高的网站服务器。


安装、使用、配置

安装并启动

[root@localhost dev]# dnf install httpd
...
Complete!
[root@localhost dev]# systemctl restart httpd
[root@localhost dev]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

此时,访问机器的ip,即可看到网站的默认页面。在以下两种情况会出现默认界面:

  1. 网站目录内没有文件

  2. selinux的权限不足

apache的默认文件夹是 /var/www/html

配置

httpd的主要配置文件

作用

文件名称

服务目录

/etc/httpd

主配置文件

/etc/httpd/conf/httpd.conf

网站数据目录

/var/www/html

访问日志

/var/log/httpd/access_log

错误日志

/var/log/httpd/error_log

img

tip:

Directory : 代表对文件夹来进行设置一些信息

Location: 代表队网站的链接来设置一些信息

配置httpd服务程序时最常用的参数以及用途描述

参数

作用

ServerRoot

服务目录

ServerAdmin

管理员邮箱

User

运行服务的用户

Group

运行服务的用户组

ServerName

网站服务器的域名

DocumentRoot

网站数据目录

Listen

监听的IP地址与端口号

DirectoryIndex

默认的索引页页面

ErrorLog

错误日志文件

CustomLog

访问日志文件

Timeout

网页超时时间,默认为300秒

当配置目录后,如果出现权限不足的问题,需要查看selinux的设置信息。

SELinux 安全子系统

SELinux 安全子系统,主要负责管理:

SELinux 域:限制服务的功能,让其只能做自己职责内的事情

SELinux 安全上下文:限制文件只能被所属的服务进行访问

SELinux服务有三种配置模式,具体如下。

enforcing:强制启用安全策略模式,将拦截服务的不合法请求。

permissive:遇到服务越权访问时,只发出警告而不强制拦截。

disabled:对于越权的行为不警告也不拦截。

配置文件:

vim /etc/selinux/config

# 文件内容
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

相关的常用命令

# 获取当前的运行模式
[root@localhost dev]# getenforce 
Enforcing

# 关闭SELinux
[root@localhost dev]# setenforce 0
[root@localhost dev]# getenforce 
Permissive

# 查看selinux的信息
# 其中,用户段system_u代表系统进程的身份,角色段object_r代表文件目录的角色,类型段httpd_sys_content_t代表网站服务的系统文件
[root@localhost dev]# ll -Zd  /var/www/html
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0 6 Feb  6  2019 /var/www/html
[root@localhost dev]# cd ~
[root@localhost ~]# mkdir  ./www
[root@localhost ~]# ll -Zd ~/www
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Jun 28 16:45 /root/www

semanage命令用于管理SELinux的策略,英文全称为:“SELinux manage”,语法格式为:“semanage [参数] [文件]”

semanage命令中常用参数以及作用

参数

作用

-l

查询

-a

添加

-m

修改

-d

删除

-t

selinux type of object

# 添加上下文信息
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t  ~/www
[root@localhost ~]# ll -Zd ~/www
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Jun 28 16:45 /root/www

# 使添加的上下文信息生效,-Rv参数可以对指定的目录进行递归操作
[root@localhost ~]# restorecon -Rv ~/www
Relabeled /root/www from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# ll -Zd ~/www
drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Jun 28 16:45 /root/www

Vhost 虚拟主机的配置

通过对虚拟主机的配置,当用户访问一个主机的不同ip地址时,访问的内容也是不同的。

为网卡配置多个ip地址

# 进入网卡配置文件
vim /etc/sysconfig/network-scripts/ifcfg-Profile_1


# 修改如下内容
IPADDR0=192.168.10.10
IPADDR1=192.168.10.20
IPADDR2=192.168.10.30


# 重新读取配置信息
nmcli connection reload ens160

tip:

启动网卡的方式: ifdown ens160

关闭网卡的方式: ifup ens160

基于IP的的虚拟主机配置

178 <virtualhost 192.168.10.10>
            # 网站的根目录
179         DocumentRoot /root/www/10
180         serverName www.shaoer.cloud
181         # 为目录配置权限
            <Directory /home/www/10>
182                 # 伪静态
                    allowoverride None
183                 # 允许所有请求
                    require all granted
184         </directory>
185 </virtualhost>

基于端口的虚拟主机配置

# 添加监听的端口
 45 Listen 80
 46 Listen 6111
 47 Listen 6222
 48 Listen 6333


# 添加监听的虚拟主机
134 <VirtualHost 192.168.10.10:6111>
135     DocumentRoot /home/wwwroot/6111
136     ServerName www.linuxprobe.com
137     <Directory /home/wwwroot/6111>
138     AllowOverride None
139     Require all granted
140     </Directory> 
141 </VirtualHost>

142 <VirtualHost 192.168.10.10:6222>
143     DocumentRoot /home/wwwroot/6222
144     ServerName www.linuxcool.com
145     <Directory /home/wwwroot/6222>
146     AllowOverride None
147     Require all granted
148     </Directory>
149 </VirtualHost>


# selinux 
## 查看http相关服务的端口
[root@linuxprobe ~]# semanage port -l | grep http
http_cache_port_t            tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t            udp      3130
http_port_t                  tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t          tcp      5988
pegasus_https_port_t         tcp      5989

## 解除selinux的限制
[root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6111
[root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6222
[root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6333
[root@linuxprobe ~]# semanage port -l | grep http
http_cache_port_t            tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t            udp      3130
http_port_t                  tcp      6333, 6222, 6111, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t          tcp      5988
pegasus_https_port_t         tcp      5989
[root@linuxprobe ~]# systemctl restart httpd

访问控制

# 允许使用火狐浏览器访问网站
161 <Directory "/var/www/html/server">
162     SetEnvIf User-Agent "Firefox" ff=1
163     Order allow,deny
164     Allow from env=ff
165 </Directory>

Order指令用来定义Allow或Deny指令起作用的顺序。比如“Order Allow, Deny”表示先将源主机与允许规则进行匹配,若匹配成功则允许访问请求,反之则拒绝访问请求。

运维
linux就该这么学
License:  CC BY 4.0
Share

Further Reading

Feb 5, 2025

Docker的常用命令

Docker的常用命令 # 镜像列表 docker images ​ # 容器列表 docker ps # 设置镜像源 ​ # 进入容器内部 docker exec -it {容器id} /bin/bash #或者 docker exec -it {容器id} /bin/sh # 查看镜

Dec 27, 2024

centos切换阿里云Yum源

centos默认的yum源是国外的,国内使用的时候速度会比较慢,可以选择切换为阿里yum源。 切换Yum源 # 1.下载aliyun yum源repo文件 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo

Dec 8, 2021

supervior安装及配置

supervior支持多种安装方式,本文介绍了pip安装的方式。

OLDER

<linux就该这么学>RHEL8配置软件仓库

NEWER

postgreSQL的服务管理与配置

Recently Updated

  • 文本切分-语义分割(Semantic Chunking)
  • dify 并发配置优化
  • Typing
  • 大模型返回中json_schema与json_mode的区别
  • Async

Trending Tags

Halo 运维 postgresql 设计模式 linux就该这么学 nas rag odoo python 文本切分

Contents

©2025 刘刚刚的blog. Some rights reserved.

Using the Halo theme Chirpy