apache文件服务器(Ubuntu)
环境:Ubuntu18.4
需求:
搭建一个资源共享的文件下载站,支持多用户。
长这样:
传输文件的协议有很多,例如:http、ftp、smb、iSCSI等等,我们选择最简单的http协议。
一、安装
首先要获取root权限。
安装apache服务器
apt install apache2
关闭防火墙
ufw disable
访问该机ip即可看到测试页面
此时,apache安装完成。
二、配置
配置文件是 /etc/apache2/sites-enabled/000-default.conf
vi /etc/apache2/sites-enabled/000-default.conf
目前可能需要修改参数
参数 | 说明 |
---|---|
DocumentRoot | 需要提供下载的资源就存放在这个目录 |
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
第一行的80即是访问端口。
默认就可以。将/var/www/html/index.html删除或更名。此时将文件放在DocumentRoot目录下,就可以实现效果。
三、多用户
配置
目前已经实现了一个共享的文件下载服务器,Linux是一个多用户的操作系统,现在为每一位用户都建一个私人的下载站。
进行简单的配置即可。
vi /etc/apache2/mods-available/userdir.conf
userdir.conf:
<IfModule mod_userdir.c>
UserDir public_html
UserDir disabled root
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>
</IfModule>
将此文件以及 /etc/apache2/mods-available/userdir.load
做个符号链接到 /etc/apache2/mods-enabled 目录下
ln -s ../mods-available/userdir.conf userdir.conf
ln -s ../mods-available/userdir.load userdir.load
创建用户
useradd -m httpdUser
# 添加用户并为用户创建登录目录
passwd httpUser
# 为用户设置密码
cd /home/httpUser
mkdir public_html
systemctl restart apache2
# 重启服务
现在,就可以通过 http://ip/~httpUser/ 来访问了