<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
前段時間因為某些原因,幾大主流網路硬碟都無法使用,正好手頭上有臺閒置的雲伺服器,於是就想來搭建一個檔案下載服務,使用者只需通過一個連結就能下載軟體。
經過調研,發現Ubuntu採用Apache2這個軟體就可以快速滿足我的需求。
安裝Apache2
apt-get install apache2
安裝好之後,啟動Apache2服務:
/etc/init.d/apache2 start
檢視啟動狀態:
/etc/init.d/apache2 status
然後,存取伺服器的公網ip或域名,就可以看到如下介面,此時說明Apache正常工作:
最後在/var/www/html
路徑下,刪除index.html
,上傳自己想要被下載的檔案,再次存取,就可以進行下載了。
(注:如果是雲伺服器,還需要在安全組開放80和443埠號)
同時,也可以通過域名/檔名
的方式直接給別人一個連結,進行下載。
如果有一臺單獨的伺服器用於臨時檔案的分享,這樣很快就搞定了。
下面來繼續進行深入研究,考慮更現實的場景。
Apache2預設採用的是80埠號,因此直接通過公網ip或域名就能存取。現實中,很多伺服器本身就部署了許多其它服務,80埠號往往被佔用,因此就需要將Apache2改成其它存取埠。
修改埠,首先需要修改/etc/apache2/ports.conf
這個檔案:
這裡吧80改成其它不衝突的埠號,我這裡以1024為例
#Listen 80 Listen 1024 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
然後修改/etc/apache2/sites-available/000-default.conf
#<VirtualHost *:80> <VirtualHost *:1024> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating
注:這個檔案中還有一個DocumentRoot
,修改該引數可以調整檔案系統的根路徑。
修改完成之後,重啟apache2:
/etc/init.d/apache2 restart
此時,就可以通過存取域名:1024
的形式存取到同樣內容,例如我的伺服器存取url為http://xdxsb.top:1024
個人伺服器很容易遭到別人的攻擊,如果有人開好多執行緒來反覆請求下載,這就將導致流量頻寬消耗巨大,甚至會讓伺服器宕機。因此,長期提供下載服務的伺服器必須設定存取限制。
存取限制主要涉及到/etc/apache2/apache2.conf
這個組態檔,首先來對該檔案進行解讀。
這個檔案內容如下:
# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
#
#
# Summary of how the Apache 2 configuration works in Debian:
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's
# default Apache2 installation attempts to make adding and removing modules,
# virtual hosts, and extra configuration directives as flexible as possible, in
# order to make automating the changes and administering the server as easy as
# possible.
# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
# /etc/apache2/
# |-- apache2.conf
# | `-- ports.conf
# |-- mods-enabled
# | |-- *.load
# | `-- *.conf
# |-- conf-enabled
# | `-- *.conf
# `-- sites-enabled
# `-- *.conf
#
#
# * apache2.conf is the main configuration file (this file). It puts the pieces
# together by including all remaining configuration files when starting up the
# web server.
#
# * ports.conf is always included from the main configuration file. It is
# supposed to determine listening ports for incoming connections which can be
# customized anytime.
#
# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
# directories contain particular configuration snippets which manage modules,
# global configuration fragments, or virtual host configurations,
# respectively.
#
# They are activated by symlinking available configuration files from their
# respective *-available/ counterparts. These should be managed by using our
# helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
# their respective man pages for detailed information.
#
# * The binary is called apache2. Due to the use of environment variables, in
# the default configuration, apache2 needs to be started/stopped with
# /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
# work with the default configuration.
# Global configuration
#
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE! If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the Mutex documentation (available
# at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
#ServerRoot "/etc/apache2"
#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
#Mutex file:${APACHE_LOCK_DIR} default
#
# The directory where shm and other runtime files will be stored.
#
DefaultRuntimeDir ${APACHE_RUN_DIR}
#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}
#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5
# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog ${APACHE_LOG_DIR}/error.log
#
# LogLevel: Control the severity of messages logged to the error_log.
# Available values: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the log level for particular modules, e.g.
# "LogLevel info ssl:warn"
#
LogLevel warn
# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
# Include list of ports to listen on
Include ports.conf
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^.ht">
Require all denied
</FilesMatch>
#
# The following directives define some format nicknames for use with
# a CustomLog directive.
#
# These deviate from the Common Log Format definitions in that they use %O
# (the actual bytes sent including headers) instead of %b (the size of the
# requested file), because the latter makes it impossible to detect partial
# requests.
#
# Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
# Use mod_remoteip instead.
#
LogFormat "%v:%p %h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" vhost_combined
LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.
# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
引數解釋:
PidFile:記錄伺服器啟動程序號的檔案
Timeout:接收和傳送前超時秒數
KeepAlive:是否允許穩固的連線(每個連線有多個請求),設為"Off"則停用
MaxKeepAliveRequests:在穩固連線期間允許的最大請求數,設為0表示無限制接入
KeepAliveTimeout:在同一個連線上從同一臺客戶上接收請求的秒數
User/Group:執行的使用者和組
HostnameLookups:指定記錄使用者端的名字還是IP地址
例如,本指令為on時記錄主機名,如www.apache.org;為off時記錄IP地址,204.62.129.132。預設值為off,這要比設為on好得多,因為如果設為on則每個使用者端請求都將會至少造成對 nameserver 進行一次查詢。
ErrorLog:錯誤紀錄檔檔案定位
LogLevel:控制記錄在錯誤紀錄檔檔案中的紀錄檔資訊
可選值:debug,info,notice,warn,error,crit,alert,emerg
Directory:在標籤對裡面可以設定各資料夾屬性
Options:控制在特定目錄中將使用哪些伺服器特性
AllowOverride:確定允許存在於.htaccess檔案中的指令型別
語法:AllowOverride All|None|directive-type [directive-type]
如果此指令被設定為None ,那麼.htaccess檔案將被完全忽略。
directive-type可以是下列各組指令之一:
Order:控制預設的存取狀態與Allow和Deny指令生效的順序
Allow:控制哪些主機可以存取伺服器的該區域。可以根據主機名、IP地址、 IP地址範圍或其他環境變數中捕獲的使用者端請求特性進行控制。
語法:Allow from all|host|env=env-variable [host|env=env-variable]
Deny:控制哪些主機被禁止存取伺服器的該區域。可以根據主機名、IP地址、 IP地址範圍或其他環境變數中捕獲的使用者端請求特性進行控制。
語法:Deny from all|host|env=env-variable [host|env=env-variable]
Require:存取限制
- all granted:表示允許所有主機存取
- all denied:表示拒絕所有主機存取
- local:表示僅允許本地主機存取
- [not] host <主機名或域名列表>:表示允許或拒絕指定主機或域名存取
- [not] ip <IP地址或網段列表>:表示允許或拒絕指定的IP地址或網段存取
AccessFileName:設定分散式組態檔的名字,預設為.htaccess
如果為某個目錄啟用了分散式組態檔功能,那麼在向用戶端返回其中的檔案時,伺服器將在這個檔案所在的各級目錄中查詢此組態檔
<FilesMatch"^.ht">:拒絕對.ht開標頭檔案的存取,以保護.htaccess檔案
LogFormat:定義存取紀錄檔的格式
通過上面對組態檔引數的研究,注意到MaxKeepAliveRequests
這個引數限制了對於單個連線最大的存取量為100,因此無需擔心單執行緒指令碼反覆頻繁請求的問題。
那麼對於高並行的請求,Apache是否有預設設定的策略呢?也是有的,從2.0開始,apache引入了MPM(Multi-Processing Module,多程序處理模組),MPM有prefork, worker和event這三種模式[4],可以通過下面的命令檢視當前apache所採用的模式:
apachectl -V | grep -i mpm
預設採用的應該是event模式。
這個模式的組態檔位於/etc/apache2/mods-availablempm_event.conf
預設引數:
<IfModule mpm_event_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
引數解釋:
我這裡沒去調整,先用JMeter來進行一個多執行緒並行測試:
JMeter下載地址:https://jmeter.apache.org/download_jmeter.cgi
解壓之後,執行apache-jmeter-5.5/bin/jmeter.bat
,即可啟動。
建立一個執行緒組,我這設定了1000個執行緒數,時間設為0.01秒
再設定HTTP請求,填寫請求域名,埠號,檔案路徑
測試結果顯示,請求失敗率為16%,因此我這小破伺服器,基本上1000個並行請求都難以滿足。
如果面對惡意攻擊,那最快解決問題的辦法無疑是封禁它的ip,這裡嘗試一下封禁本機ip,看看是否有效。
首先查詢本機ip,直接在百度搜尋ip
,即可查詢到公網ip,注意這裡一定要是公網ip,通過ipconfig
查詢出的是內網ip。
修改組態檔:
vim /etc/apache2/apache2.conf
修改/var/www/
檔案內容:
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None <RequireAll> Require all granted Require not ip 111.11.81.152 </RequireAll> </Directory>
重啟服務:
/etc/init.d/apache2 restart
再次存取,發現許可權受限,說明設定成功。
拓展:如果只允許某個固定ip存取,那麼可以這樣修改:
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None # Require all granted Require ip 111.11.82.8 </Directory>
對於某些私密檔案,可以進一步設定賬號密碼進行身份驗證。
首先建立一個資料夾用來儲存使用者資訊:
mkdir -p /usr/local/conf
然後建立使用者:
htpasswd -c /usr/local/conf/.usr zstar
zstar
是我建立的使用者名稱
輸入密碼後,Apache會以密文方式儲存密碼,可以通過下面的方式檢視使用者名稱和密碼密文:
cat /usr/local/conf/.usr
再次修改組態檔:
vim /etc/apache2/apache2.conf
修改內容:
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All AuthName "apache" AuthType Basic AuthUserFile "/usr/local/conf/.usr" Require user zstar </Directory>
重啟服務:
/etc/init.d/apache2 restart
再次存取,成功彈出登入驗證,說明設定成功。
[1]ubuntu搭建http伺服器用於下載ubuntu檔案:https://blog.csdn.net/yy1695990107/article/details/116976994
[2]Apache2 httpd.conf 設定詳解:https://blog.csdn.net/assassinice/article/details/78854139
[3]Apache的存取控制:https://blog.csdn.net/m0_54434140/article/details/122249712
[4]apache2三種模式及引數調優:https://blog.csdn.net/zhihui1017/article/details/54959194
到此這篇關於教你使用Apache搭建Http下載伺服器的文章就介紹到這了,更多相關Apache搭建Http下載伺服器內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45