楚堂 發表於 2020-12-17 18:10:00

在linux下使用Apache搭建文件服务器

<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>一.关于文件服务器</li><li>二.使用Apache搭建文件服务器</li><li>三.测试文件服务器是否可用</li></ul></div><p></p>
<h1 id="一关于文件服务器">一.关于文件服务器</h1>
<p>​        在一个项目中,如果想把公共软件或者资料共享给项目组成员,可以搭建一个简易的文件服务器来实现,只要是在局域网内的成员都可以通过浏览器或者wget命令来下载和访问资料。可以达到信息共享,软件版本一致的效果。本文讲述在linux环境下使用Apache服务搭建文件服务器。</p>
<h1 id="二使用apache搭建文件服务器">二.使用Apache搭建文件服务器</h1>
<p>1.Apache服务在linux环境下的程序叫做httpd,所以首先安装httpd服务,如果配置好了yum源的话,直接使用yum命令安装,如果没有配置好yum源的话,可以参考博客“linux 配置本地yum源,配置国内yum源,配置epel源”进行配置,网址为:https://www.cnblogs.com/renshengdezheli/p/13949693.html</p>
<pre><code class="language-shell"># yum -y install httpd
</code></pre>
<p>2.启动httpd服务</p>
<pre><code class="language-shell">#启动httpd服务
# systemctl start httpd

#查看httpd服务状态
# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-12-17 16:26:05 CST; 7s ago
   Docs: man:httpd(8)
         man:apachectl(8)
Main PID: 98576 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
         ├─98576 /usr/sbin/httpd -DFOREGROUND
         ├─98577 /usr/sbin/httpd -DFOREGROUND
         ├─98578 /usr/sbin/httpd -DFOREGROUND
         ├─98579 /usr/sbin/httpd -DFOREGROUND
         ├─98580 /usr/sbin/httpd -DFOREGROUND
         └─98581 /usr/sbin/httpd -DFOREGROUND

Dec 17 16:26:05 node5 systemd: Starting The Apache HTTP Server...
Dec 17 16:26:05 node5 httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.110.184. Set the 'ServerName' directive globally to su...ss this message
Dec 17 16:26:05 node5 systemd: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

#查看Apache版本
# httpd -version
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 16 2020 16:18:20
</code></pre>
<p>3.查看IP地址,访问Apache页面</p>
<pre><code class="language-shell">#可以看到本机IP地址为192.168.110.184
# ifconfig
ens33: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;mtu 1500
      inet 192.168.110.184netmask 255.255.255.0broadcast 192.168.110.255
      ether 00:0c:29:11:c4:4atxqueuelen 1000(Ethernet)
      RX packets 24682bytes 13301526 (12.6 MiB)
      RX errors 0dropped 4overruns 0frame 0
      TX packets 15119bytes 2166095 (2.0 MiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0

lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt;mtu 65536
      inet 127.0.0.1netmask 255.0.0.0
      inet6 ::1prefixlen 128scopeid 0x10&lt;host&gt;
      looptxqueuelen 1(Local Loopback)
      RX packets 2402bytes 221903 (216.7 KiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 2402bytes 221903 (216.7 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
</code></pre>
<p>在浏览器里访问http://192.168.110.184/,如果出现如下界面说明Apache服务安装成功</p>
<p><img src="https://gitee.com/zhelilize/blogimg/raw/master/img/image-20201217172039462.png"></p>
<p>4.创建共享目录/opt/soft,把需要共享的文件都放在这个目录</p>
<pre><code class="language-shell"># mkdir /opt/soft

#此命令把系统所有的tar.gz的压缩包都放在共享目录里
# find / -name "*.tar.gz" -exec mv {} /opt/soft \;

# ls /opt/soft/
amhello-1.0.tar.gz            elasticsearch-6.2.2.tar.gz         FastDFS_v5.08.tar.gz      kibana-6.2.2-linux-x86_64.tar.gznginx-1.19.3.tar.gz         ntp-4.2.6p5.tar.gz   tomcat-native.tar.gz
apache-tomcat-8.0.51.tar.gz   fastdfs_client_java._v1.25.tar.gzfindfile.tar.gz             libopts-40.0.15.tar.gz            nginx-1.8.0.tar.gz          rarlinux-3.8.0.tar.gz饼干.txt
commons-daemon-native.tar.gzfastdfs-nginx-module_v1.16.tar.gzjdk-8u172-linux-x64.tar.gznginx-1.10.0.tar.gz               ngx_cache_purge-2.3.tar.gztoday_db.tar.gz
</code></pre>
<p>5.因为访问Apache页面默认读取的是/var/www/html/页面,所以把共享目录链接到/var/www/html/下就可以了</p>
<pre><code class="language-shell"># ln -s /opt/soft /var/www/html/file

# ll /var/www/html/file
lrwxrwxrwx 1 root root 9 Dec 17 16:29 /var/www/html/file -&gt; /opt/soft
</code></pre>
<p>6.重启Apache服务,查看页面</p>
<pre><code class="language-shell"># systemctl restart httpd
</code></pre>
<p>使用浏览器访问http://192.168.110.184/file/,如果出现如下界面,就说明文件服务器搭建好了</p>
<p><img src="https://gitee.com/zhelilize/blogimg/raw/master/img/image-20201217165158511.png"></p>
<p>7.通过网页我们发现中文是乱码,可以修改配置文件使中文正常显示</p>
<pre><code class="language-shell">#在Apache配置文件的末尾追加一行
# echo "IndexOptions Charset=UTF-8" &gt;&gt; /etc/httpd/conf/httpd.conf

# systemctl restart httpd
</code></pre>
<p>再次访问网页http://192.168.110.184/file/,发现页面的中文正常显示了</p>
<p><img src="https://gitee.com/zhelilize/blogimg/raw/master/img/image-20201217165302948.png"></p>
<h1 id="三测试文件服务器是否可用">三.测试文件服务器是否可用</h1>
<p>1.在windows上使用浏览器访问http://192.168.110.184/file/,如果页面可以打开,并且点击软件会自动下载,说明通过windows下载文件成功。</p>
<p>2.在局域网内的另外一台linux机器上测试是否可以下载文件</p>
<pre><code class="language-shell">#首先在node8机器上使用root账户测试下载文件
#使用wget命令下载文件
# wget http://192.168.110.184/file/饼干.txt
--2020-12-17 16:53:00--http://192.168.110.184/file/%E9%A5%BC%E5%B9%B2.txt
Connecting to 192.168.110.184:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1181 (1.2K)
Saving to: ‘饼干.txt’

100%[=======================================================================================================================================================================&gt;] 1,181       --.-K/s   in 0s      

2020-12-17 16:53:00 (130 MB/s) - ‘饼干.txt’ saved

# wget http://192.168.110.184/file/today_db.tar.gz
--2020-12-17 16:53:26--http://192.168.110.184/file/today_db.tar.gz
Connecting to 192.168.110.184:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 767
Saving to: ‘today_db.tar.gz’

100%[=======================================================================================================================================================================&gt;] 767         --.-K/s   in 0s      

2020-12-17 16:53:26 (268 MB/s) - ‘today_db.tar.gz’ saved

#发现文件能正常下载
# ls 饼干.txt today_db.tar.gz
today_db.tar.gz饼干.txt

#使用node8机器上的普通账户file1测试下载文件
# useradd file1

# echo "123456" | passwd --stdin file1
Changing password for user file1.
passwd: all authentication tokens updated successfully.

# su - file1
$ pwd
/home/file1
$ ls
$ wget http://192.168.110.184/file/饼干.txt
--2020-12-17 17:44:10--http://192.168.110.184/file/%E9%A5%BC%E5%B9%B2.txt
Connecting to 192.168.110.184:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1181 (1.2K)
Saving to: ‘饼干.txt’

100%[=======================================================================================================================================================================&gt;] 1,181       --.-K/s   in 0s      

2020-12-17 17:44:10 (254 MB/s) - ‘饼干.txt’ saved

$ wget http://192.168.110.184/file/today_db.tar.gz
--2020-12-17 17:44:20--http://192.168.110.184/file/today_db.tar.gz
Connecting to 192.168.110.184:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 767
Saving to: ‘today_db.tar.gz’

100%[=======================================================================================================================================================================&gt;] 767         --.-K/s   in 0s      

2020-12-17 17:44:20 (216 MB/s) - ‘today_db.tar.gz’ saved

#发现能正常下载文件
$ ls
today_db.tar.gz饼干.txt
</code></pre>
<p>自此文件服务器搭建成功,功能正常。</p>


</div>
<div id="MySignature" role="contentinfo">
    致力于一条龙式的为您解决问题<br><br>
来源:https://www.cnblogs.com/renshengdezheli/p/14151106.html
頁: [1]
查看完整版本: 在linux下使用Apache搭建文件服务器