HTTP文件服务器搭建

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地址测试 ########################################

在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Linux上搭建HTTP文件服务器,可以按照以下步骤操作: 1. 安装Apache服务器:使用命令行工具,输入sudo apt-get install apache2,安装Apache服务器。 2. 配置Apache服务器:在安装完成后,需要对Apache服务器进行配置,以便它可以正常工作。可以编辑/etc/apache2/apache2.conf文件来进行配置。 3. 创建文件夹:在Apache服务器的根目录下创建一个文件夹,用于存放要共享的文件。 4. 设置文件夹权限:使用命令行工具,输入sudo chmod -R 777 /path/to/folder,设置文件夹的权限,以便其他用户可以访问该文件夹。 5. 启动Apache服务器:使用命令行工具,输入sudo service apache2 start,启动Apache服务器。 6. 测试文件服务器:在浏览器中输入服务器的IP地址或域名,即可访问文件服务器,浏览和下载共享的文件。 以上就是在Linux上搭建HTTP文件服务器的基本步骤。 ### 回答2: 搭建Linux下的HTTP文件服务器的过程非常简单,以下是详细步骤: 1. 首先,需要在Linux系统上安装Apache服务器。在终端中输入以下命令: sudo apt-get update sudo apt-get install apache2 2. 安装后,启动Apache服务器。 sudo systemctl start apache2 3. 现在,将需要共享的文件复制到Apache的根目录(/var/www/html/)中。可以将文件复制到该目录下的任何子目录中。但需要注意的是,Apache服务器必须具有对所有共享文件的读取权限。 4. 在浏览器中,访问IP地址或主机名(例如:http://192.168.1.1)。如果想要直接访问根目录,可以使用下面的命令: sudo mkdir /var/www/html/myfolder sudo chown -R www-data:www-data /var/www/html/myfolder 5. 现在,在浏览器中,可以通过以下URL来访问文件服务器http://<服务器IP地址>/<共享的目录或文件名> 例如,http://192.168.1.1/myfolder/index.html。 6. 为了使其他用户也可以访问共享文件夹,需要打Apache服务器的访问权限。可以通过编辑Apache的配置文件来完成此操作: sudo nano /etc/apache2/apache2.conf 7. 在文件的末尾添加以下内容以启用跨域请求: <Directory /var/www/html/myfolder> Options Indexes FollowSymLinks AllowOverride All Require all granted Header add Access-Control-Allow-Origin "*" </Directory> 8. 保存该文件并重启Apache服务器: sudo systemctl restart apache2 现在,其他人可以访问共享文件夹并下载其中的文件了。此外,还可以向共享目录上传文件,这需要“上传”权限。如果想要实现更严格的安全控制,可以考虑使用基于身份验证和授权的选项(如基于密码的身份验证和SSL证书)来加强安全性。 ### 回答3: 随着互联网技术的不断发展,文件共享逐渐成为了日常工作中不可或缺的一环。当然,搭建http文件服务器是一种非常重要的方法。而linux操作系统,因其稳定、高效和源的特点,被越来越多的人选择。本文将详细介绍如何在linux系统中搭建http文件服务器。 1. 安装httpd 首先应该安装httpd软件包。在linux中,最常用的httpd软件包是Apache。通过以下命令进行安装: sudo apt-get install apache2 在执行上述命令之后,系统会自动安装Apache,同时在/var/www/html/目录下创建一个默认的测试页面index.html。 2. 创建文件共享目录 接下来我们需要创建一个共享目录。可以在/var/www目录下创建文件夹share。 sudo mkdir /var/www/share 该文件夹将用于储存需要共享的文件。注意,为了确保共享的文件可以被Apache访问,在share文件夹下也要创建子文件夹: sudo mkdir /var/www/share/files sudo mkdir /var/www/share/download 3. 设置权限 为了确保Apache用户可以访问共享目录,需要给予适当的权限。可以通过以下命令将Apache用户添加到共享文件夹的访问权限组中: sudo chown -R www-data:www-data /var/www/share sudo chmod -R 775 /var/www/share 在执行上述命令之后,Apache用户将被授予/var/www/share目录的读写权限。 4. 配置Apache 对于Apache通过http协议向外提供文件访问服务,需要进行相关配置。 编辑Apache配置文件: sudo nano /etc/apache2/sites-available/000-default.conf 在打的编辑器中,添加以下内容: DocumentRoot /var/www/share <Directory /var/www/share> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> 保存退出:Ctrl+X,然后Y 重启Apache: sudo service apache2 restart 5. 网页访问 最后,可以在浏览器中输入http://localhost/share进行测试。如果Apache服务运行正常,将会显示share文件夹中的文件。 以上步骤即可成功搭建linux下的http文件服务器,通过这种方式可以方便地实现本地文件共享,适用于单个用户或小团队办公环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值