Linux下使用http搭建文件服务器
####### 修改主机名
[root@localhost ~]# hostnamectl set-hostname http
[root@localhost ~]# bash
bash
####### 网卡的配置
[root@http ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static ### 原先为 dhcp 自动获取IP地址,static静态IP
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=54ca0261-559e-4cb5-83b6-4d06a5866acb
DEVICE=ens33
ONBOOT=yes ### 新创建的虚拟机网卡都是禁用的,原先为 no,修改为 yes 启动网卡
IPADDR=192.168.169.60 ### 静态IP地址的配置
PREFIX=24 ### 子网掩码 另一种写法为 ### NETMASK=255.255.255.0
GATEWAY=192.168.169.2 ### 网关默认为 2
DNS1=114.114.114.114 ### DNS域名解析
DNS2=8.8.8.8
####### 网卡的重启,使网卡配置生效
[root@http ~]# systemctl restart network
[root@http ~]# ip a ### 查看IP地址是否为刚才所配置
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:4b:3f:3b brd ff:ff:ff:ff:ff:ff
inet `192.168.169.60`/24 brd 192.168.169.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::3dc3:3652:c74b:c16e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
####### 查看 防火墙 状态,active (running) 开启状态
[root@http ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-10-16 04:13:00 CST; 1s ago
Docs: man:firewalld(1)
Main PID: 23978 (firewalld)
CGroup: /system.slice/firewalld.service
└─23978 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Oct 16 04:13:00 http systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 16 04:13:00 http systemd[1]: Started firewalld - dynamic firewall daemon.
Oct 16 04:13:00 http firewalld[23978]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure con...t now.
Hint: Some lines were ellipsized, use -l to show in full.
####### 设置 防火墙 禁用并立即停止 firewalld 服务。
[root@http ~]# systemctl disable firewalld --now
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
####### 查看 防火墙 的状态
[root@http ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
Oct 16 04:13:00 http systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 16 04:13:00 http systemd[1]: Started firewalld - dynamic firewall daemon.
Oct 16 04:13:00 http firewalld[23978]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure con...t now.
Oct 16 04:13:20 http systemd[1]: Stopping firewalld - dynamic firewall daemon...
Oct 16 04:13:22 http systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
####### 创建一个目录用于存放yum源
[root@http ~]# mkdir /opt/centos
####### 挂载镜像到 /mnt 下
[root@http ~]# mount CentOS-7-x86_64-DVD-2009.iso /mnt/
mount: /dev/loop0 is write-protected, mounting read-only
####### 复制 /mnt/ 下的所以东西到 /opt/centos
[root@http ~]# cp -rvf /mnt/* /opt/centos/
‘/mnt/CentOS_BuildTag’ -> ‘/opt/centos/CentOS_BuildTag’
‘/mnt/EFI’ -> ‘/opt/centos/EFI’
‘/mnt/EFI/BOOT’ -> ‘/opt/centos/EFI/BOOT’
‘/mnt/EFI/BOOT/BOOTIA32.EFI’ -> ‘/opt/centos/EFI/BOOT/BOOTIA32.EFI’
‘/mnt/EFI/BOOT/BOOTX64.EFI’ -> ‘/opt/centos/EFI/BOOT/BOOTX64.EFI’
####### 取消挂载
[root@http ~]# umount /mnt
####### 将原有的源仓库移到 /tmp 临时存放目录下
[root@http ~]# mv /etc/yum.repos.d/* /tmp/
####### 编写yum源文件
[root@http ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
这是一个在CentOS系统中配置的yum源配置文件。下面是对每个参数的解释:
- `name=centos`: 该源的名称为"centos"。
- `baseurl=file:///opt/centos`: 该源的基础URL指向本地文件系统上的目录,即`/opt/centos`。这意味着从这个源安装的软件包将直接从本地文件系统上获取,而不是从远程服务器上下载。
- `gpgcheck=0`: 禁用GPG签名检查。这通常用于内部或测试环境,因为在这些环境中,软件包的来源通常是可信的。
- `enabled=1`: 启用该源。默认情况下,所有源都是启用的,但可以通过设置此参数为0来禁用某个源。
请注意,这个配置文件应该放在`/etc/yum.repos.d/`目录下,以便YUM(一个在CentOS中用于管理软件包的工具)能够找到并使用它。
####### 测试yum源的配置
[root@http ~]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
(1/2): centos/group_gz | 153 kB 00:00:00
(2/2): centos/primary_db | 3.3 MB 00:00:00
repo id repo name status
centos centos 4,070
repolist: 4,070
####### 安装 http 服务
[root@http ~]# yum install -y httpd
############################### 在浏览器输入静态IP地址测试 ########################################
####### 将 httpd 欢迎界面注释掉,用于配置 Apache HTTP 服务器的欢迎页面。当用户访问服务器时,将显示此文件中定义的内容。
`注释/etc/httpd/conf.d/welcome.conf文件下的此内容,不然就会返回禁止访问报错`
[root@localhost ~]# cat /etc/httpd/conf.d/welcome.conf
#
# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL. To disable the
# Welcome page, comment out all the lines below.
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
#<LocationMatch "^/+$">
# Options -Indexes
# ErrorDocument 403 /.noindex.html
#</LocationMatch>
#
#<Directory /usr/share/httpd/noindex>
# AllowOverride None
# Require all granted
#</Directory>
#
#Alias /.noindex.html /usr/share/httpd/noindex/index.html
#Alias /noindex/css/bootstrap.min.css /usr/share/httpd/noindex/css/bootstrap.min.css
#Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
#Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
#Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
[root@http ~]# cat /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html" #这一行指定了文档根目录
<Directory "/var/www"> #用于针对指定目录(在这里是/var/www)设置访问控制规则的开始标签。以下的配置将应用于/var/www目录及其子目录。
AllowOverride None
# Allow open access:
Require all granted #表示允许所有人访问/var/www目录及其子目录,没有访问限制
</Directory>
Listen 80 #默认的监听端口,修改80后则需要指定IP:端口进行访问
##############################################################################################
### 添加一下内容
<FilesMatch "\.(?i:pdf|zip|txt|csv)$">
Header set Content-Disposition "attachment"
</FilesMatch>
当有人请求以 .pdf、.zip、.txt 或 .csv 结尾的文件时,服务器会设置 Content-Disposition 标头,强制
将文件作为附件下载,而不是在浏览器中直接打开,如果还要添加例如tar、gz等文件后缀可以下载,
则需要在括号内添加(?i:pdf|zip|txt|csv|gz|tar)相应的后缀
### 设置服务开机自启动,并重启服务
[root@http ~]# systemctl enable httpd --now
[root@http ~]# systemctl restart httpd
####### 将想要的文件或者目录存放到 /var/www/html/ 目录下,即可在网页中访问
[root@http ~]# cp -rvf /opt/centos/ /var/www/html/
############################### 在浏览器输入静态IP地址测试 ########################################