首頁 > 軟體

教你使用Apache搭建Http下載伺服器

2022-11-01 14:03:49

前言

前段時間因為某些原因,幾大主流網路硬碟都無法使用,正好手頭上有臺閒置的雲伺服器,於是就想來搭建一個檔案下載服務,使用者只需通過一個連結就能下載軟體。

Apache快速上手

經過調研,發現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:控制在特定目錄中將使用哪些伺服器特性

      • All:除MultiViews之外的所有特性,這是預設設定
      • ExecCG:允許使用mod_cgi執行CGI指令碼
      • FollowSymLinks:伺服器允許在此目錄中使用符號連線,如果此設定位於設定段中,則會被忽略
      • Includes:允許使用mod_include提供的伺服器端包含
      • IncludesNOEXEC:允許伺服器端包含,但禁用"#exec cmd"和"#exec cgi",但仍可以從ScriptAlias目錄使用"#include virtual"虛擬CGI指令碼
      • Indexes:如果一個對映到目錄的URL被請求,而此目錄中又沒有DirectoryIndex(例如:index.html),那麼伺服器會返回由mod_autoindex生成的一個格式化後的目錄列表
      • MultiViews:允許使用mod_negotiation提供內容協商的"多重檢視"(MultiViews)
      • SymLinksIfOwnerMatch:伺服器僅在符號連線與其目的目錄或檔案的擁有者具有相同的uid時才使用它。 如果此設定出現在設定段中,則將被忽略
    • AllowOverride:確定允許存在於.htaccess檔案中的指令型別
      語法:AllowOverride All|None|directive-type [directive-type]
      如果此指令被設定為None ,那麼.htaccess檔案將被完全忽略。
      directive-type可以是下列各組指令之一:

      • AuthConfig : 允許使用與認證授權相關的指令
      • FileInfo : 允許使用控制檔案型別的指令、控制檔案後設資料的指令、mod_rewrite中的指令、mod_actions中的Action指令
      • Indexes : 允許使用控制目錄索引的指令
      • Limit : 允許使用控制主機存取的指令
    • Order:控制預設的存取狀態與Allow和Deny指令生效的順序

      • Deny,Allow : Deny指令在Allow指令之前被評估。預設允許所有存取。任何不匹配Deny指令或者匹配Allow指令的客戶都被允許存取
      • Allow,Deny : Allow指令在Deny指令之前被評估。預設拒絕所有存取。任何不匹配Allow指令或者匹配Deny指令的客戶都將被禁止存取
      • Mutual-failure : 只有出現在Allow列表並且不出現在Deny列表中的主機才被允許存取。這種順序與"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>

引數解釋:

  • StartServers:啟動時程序數
  • MinSpareThreads:最小空閒執行緒數
  • MaxSpareThreads:最大空閒執行緒數
  • ThreadLimit:每個程序可以啟動的執行緒數量上限值
  • ThreadsPerChild:每個程序可以啟動的執行緒數量
  • MaxRequestWorkers:執行緒數量最大值
  • MaxConnectionsPerChild:最大連線數限制

我這裡沒去調整,先用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,注意這裡一定要是公網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!


IT145.com E-mail:sddin#qq.com