国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

教你使用Apache搭建Http下載服務(wù)器

瀏覽:133日期:2023-03-07 14:40:43
目錄
  • 前言
  • Apache快速上手
  • 修改端口號(hào)
  • 設(shè)置訪問(wèn)限制
    • 配置文件參數(shù)詳解
    • 限制連接量
    • 封禁ip
    • 設(shè)置賬號(hào)密碼訪問(wèn)
  • 參考

    前言

    前段時(shí)間因?yàn)槟承┰颍瑤状笾髁骶W(wǎng)盤都無(wú)法使用,正好手頭上有臺(tái)閑置的云服務(wù)器,于是就想來(lái)搭建一個(gè)文件下載服務(wù),用戶只需通過(guò)一個(gè)鏈接就能下載軟件。

    Apache快速上手

    經(jīng)過(guò)調(diào)研,發(fā)現(xiàn)Ubuntu采用Apache2這個(gè)軟件就可以快速滿足我的需求。

    安裝Apache2

    apt-get install apache2

    安裝好之后,啟動(dòng)Apache2服務(wù):

    /etc/init.d/apache2 start 

    查看啟動(dòng)狀態(tài):

    /etc/init.d/apache2 status

    然后,訪問(wèn)服務(wù)器的公網(wǎng)ip或域名,就可以看到如下界面,此時(shí)說(shuō)明Apache正常工作:

    最后在/var/www/html路徑下,刪除index.html,上傳自己想要被下載的文件,再次訪問(wèn),就可以進(jìn)行下載了。
    (注:如果是云服務(wù)器,還需要在安全組開放80和443端口號(hào))
    同時(shí),也可以通過(guò)域名/文件名的方式直接給別人一個(gè)鏈接,進(jìn)行下載。

    如果有一臺(tái)單獨(dú)的服務(wù)器用于臨時(shí)文件的分享,這樣很快就搞定了。
    下面來(lái)繼續(xù)進(jìn)行深入研究,考慮更現(xiàn)實(shí)的場(chǎng)景。

    修改端口號(hào)

    Apache2默認(rèn)采用的是80端口號(hào),因此直接通過(guò)公網(wǎng)ip或域名就能訪問(wèn)。現(xiàn)實(shí)中,很多服務(wù)器本身就部署了許多其它服務(wù),80端口號(hào)往往被占用,因此就需要將Apache2改成其它訪問(wèn)端口。

    修改端口,首先需要修改/etc/apache2/ports.conf這個(gè)文件:
    這里吧80改成其它不沖突的端口號(hào),我這里以1024為例

    #Listen 80Listen 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

    注:這個(gè)文件中還有一個(gè)DocumentRoot,修改該參數(shù)可以調(diào)整文件系統(tǒng)的根路徑。

    修改完成之后,重啟apache2:

    /etc/init.d/apache2 restart

    此時(shí),就可以通過(guò)訪問(wèn)域名:1024的形式訪問(wèn)到同樣內(nèi)容,例如我的服務(wù)器訪問(wèn)url為http://xdxsb.top:1024

    設(shè)置訪問(wèn)限制

    個(gè)人服務(wù)器很容易遭到別人的攻擊,如果有人開好多線程來(lái)反復(fù)請(qǐng)求下載,這就將導(dǎo)致流量帶寬消耗巨大,甚至?xí)尫?wù)器宕機(jī)。因此,長(zhǎng)期提供下載服務(wù)的服務(wù)器必須設(shè)置訪問(wèn)限制。

    配置文件參數(shù)詳解

    訪問(wèn)限制主要涉及到/etc/apache2/apache2.conf這個(gè)配置文件,首先來(lái)對(duì)該文件進(jìn)行解讀。
    這個(gè)文件內(nèi)容如下:

    # 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

    參數(shù)解釋:

    • PidFile:記錄服務(wù)器啟動(dòng)進(jìn)程號(hào)的文件

    • Timeout:接收和發(fā)送前超時(shí)秒數(shù)

    • KeepAlive:是否允許穩(wěn)固的連接(每個(gè)連接有多個(gè)請(qǐng)求),設(shè)為"Off"則停用

    • MaxKeepAliveRequests:在穩(wěn)固連接期間允許的最大請(qǐng)求數(shù),設(shè)為0表示無(wú)限制接入

    • KeepAliveTimeout:在同一個(gè)連接上從同一臺(tái)客戶上接收請(qǐng)求的秒數(shù)

    • User/Group:運(yùn)行的用戶和組

    • HostnameLookups:指定記錄用戶端的名字還是IP地址
      例如,本指令為on時(shí)記錄主機(jī)名,如www.apache.org;為off時(shí)記錄IP地址,204.62.129.132。默認(rèn)值為off,這要比設(shè)為on好得多,因?yàn)槿绻O(shè)為on則每個(gè)用戶端請(qǐng)求都將會(huì)至少造成對(duì) nameserver 進(jìn)行一次查詢。

    • ErrorLog:錯(cuò)誤日志文件定位

    • LogLevel:控制記錄在錯(cuò)誤日志文件中的日志信息
      可選值:debug,info,notice,warn,error,crit,alert,emerg

    • Directory:在標(biāo)簽對(duì)里面可以設(shè)置各文件夾屬性

      • Options:控制在特定目錄中將使用哪些服務(wù)器特性

        • All:除MultiViews之外的所有特性,這是默認(rèn)設(shè)置
        • ExecCG:允許使用mod_cgi執(zhí)行CGI腳本
        • FollowSymLinks:服務(wù)器允許在此目錄中使用符號(hào)連接,如果此配置位于配置段中,則會(huì)被忽略
        • Includes:允許使用mod_include提供的服務(wù)器端包含
        • IncludesNOEXEC:允許服務(wù)器端包含,但禁用"#exec cmd"和"#exec cgi",但仍可以從ScriptAlias目錄使用"#include virtual"虛擬CGI腳本
        • Indexes:如果一個(gè)映射到目錄的URL被請(qǐng)求,而此目錄中又沒(méi)有DirectoryIndex(例如:index.html),那么服務(wù)器會(huì)返回由mod_autoindex生成的一個(gè)格式化后的目錄列表
        • MultiViews:允許使用mod_negotiation提供內(nèi)容協(xié)商的"多重視圖"(MultiViews)
        • SymLinksIfOwnerMatch:服務(wù)器僅在符號(hào)連接與其目的目錄或文件的擁有者具有相同的uid時(shí)才使用它。 如果此配置出現(xiàn)在配置段中,則將被忽略
      • AllowOverride:確定允許存在于.htaccess文件中的指令類型
        語(yǔ)法:AllowOverride All|None|directive-type [directive-type]
        如果此指令被設(shè)置為None ,那么.htaccess文件將被完全忽略。
        directive-type可以是下列各組指令之一:

        • AuthConfig : 允許使用與認(rèn)證授權(quán)相關(guān)的指令
        • FileInfo : 允許使用控制文檔類型的指令、控制文檔元數(shù)據(jù)的指令、mod_rewrite中的指令、mod_actions中的Action指令
        • Indexes : 允許使用控制目錄索引的指令
        • Limit : 允許使用控制主機(jī)訪問(wèn)的指令
      • Order:控制默認(rèn)的訪問(wèn)狀態(tài)與Allow和Deny指令生效的順序

        • Deny,Allow : Deny指令在Allow指令之前被評(píng)估。默認(rèn)允許所有訪問(wèn)。任何不匹配Deny指令或者匹配Allow指令的客戶都被允許訪問(wèn)
        • Allow,Deny : Allow指令在Deny指令之前被評(píng)估。默認(rèn)拒絕所有訪問(wèn)。任何不匹配Allow指令或者匹配Deny指令的客戶都將被禁止訪問(wèn)
        • Mutual-failure : 只有出現(xiàn)在Allow列表并且不出現(xiàn)在Deny列表中的主機(jī)才被允許訪問(wèn)。這種順序與"Order Allow,Deny"具有同樣效果
      • Allow:控制哪些主機(jī)可以訪問(wèn)服務(wù)器的該區(qū)域。可以根據(jù)主機(jī)名、IP地址、 IP地址范圍或其他環(huán)境變量中捕獲的客戶端請(qǐng)求特性進(jìn)行控制。
        語(yǔ)法:Allow from all|host|env=env-variable [host|env=env-variable]

      • Deny:控制哪些主機(jī)被禁止訪問(wèn)服務(wù)器的該區(qū)域。可以根據(jù)主機(jī)名、IP地址、 IP地址范圍或其他環(huán)境變量中捕獲的客戶端請(qǐng)求特性進(jìn)行控制。
        語(yǔ)法:Deny from all|host|env=env-variable [host|env=env-variable]

      • Require:訪問(wèn)限制
        - all granted:表示允許所有主機(jī)訪問(wèn)
        - all denied:表示拒絕所有主機(jī)訪問(wèn)
        - local:表示僅允許本地主機(jī)訪問(wèn)
        - [not] host <主機(jī)名或域名列表>:表示允許或拒絕指定主機(jī)或域名訪問(wèn)
        - [not] ip <IP地址或網(wǎng)段列表>:表示允許或拒絕指定的IP地址或網(wǎng)段訪問(wèn)

    • AccessFileName:設(shè)置分布式配置文件的名字,默認(rèn)為.htaccess
      如果為某個(gè)目錄啟用了分布式配置文件功能,那么在向客戶端返回其中的文檔時(shí),服務(wù)器將在這個(gè)文檔所在的各級(jí)目錄中查找此配置文件

    • <FilesMatch"^.ht">:拒絕對(duì).ht開頭文件的訪問(wèn),以保護(hù).htaccess文件

    • LogFormat:定義訪問(wèn)日志的格式

    限制連接量

    通過(guò)上面對(duì)配置文件參數(shù)的研究,注意到MaxKeepAliveRequests這個(gè)參數(shù)限制了對(duì)于單個(gè)連接最大的訪問(wèn)量為100,因此無(wú)需擔(dān)心單線程腳本反復(fù)頻繁請(qǐng)求的問(wèn)題。
    那么對(duì)于高并發(fā)的請(qǐng)求,Apache是否有默認(rèn)設(shè)置的策略呢?也是有的,從2.0開始,apache引入了MPM(Multi-Processing Module,多進(jìn)程處理模塊),MPM有prefork, worker和event這三種模式[4],可以通過(guò)下面的命令查看當(dāng)前apache所采用的模式:

    apachectl -V | grep -i mpm

    默認(rèn)采用的應(yīng)該是event模式。

    這個(gè)模式的配置文件位于/etc/apache2/mods-availablempm_event.conf
    默認(rèn)參數(shù):

    <IfModule mpm_event_module>StartServers     2MinSpareThreads  25MaxSpareThreads  75ThreadLimit      64ThreadsPerChild  25MaxRequestWorkers150MaxConnectionsPerChild   0</IfModule>

    參數(shù)解釋:

    • StartServers:?jiǎn)?dòng)時(shí)進(jìn)程數(shù)
    • MinSpareThreads:最小空閑線程數(shù)
    • MaxSpareThreads:最大空閑線程數(shù)
    • ThreadLimit:每個(gè)進(jìn)程可以啟動(dòng)的線程數(shù)量上限值
    • ThreadsPerChild:每個(gè)進(jìn)程可以啟動(dòng)的線程數(shù)量
    • MaxRequestWorkers:線程數(shù)量最大值
    • MaxConnectionsPerChild:最大連接數(shù)限制

    我這里沒(méi)去調(diào)整,先用JMeter來(lái)進(jìn)行一個(gè)多線程并發(fā)測(cè)試:
    JMeter下載地址:https://jmeter.apache.org/download_jmeter.cgi

    解壓之后,運(yùn)行apache-jmeter-5.5/bin/jmeter.bat,即可啟動(dòng)。

    創(chuàng)建一個(gè)線程組,我這設(shè)置了1000個(gè)線程數(shù),時(shí)間設(shè)為0.01秒

    再設(shè)置HTTP請(qǐng)求,填寫請(qǐng)求域名,端口號(hào),文件路徑

    測(cè)試結(jié)果顯示,請(qǐng)求失敗率為16%,因此我這小破服務(wù)器,基本上1000個(gè)并發(fā)請(qǐng)求都難以滿足。

    封禁ip

    如果面對(duì)惡意攻擊,那最快解決問(wèn)題的辦法無(wú)疑是封禁它的ip,這里嘗試一下封禁本機(jī)ip,看看是否有效。

    首先查詢本機(jī)ip,直接在百度搜索ip,即可查詢到公網(wǎng)ip,注意這里一定要是公網(wǎng)ip,通過(guò)ipconfig查詢出的是內(nèi)網(wǎng)ip。

    修改配置文件:

    vim /etc/apache2/apache2.conf

    修改/var/www/文件內(nèi)容:

    <Directory /var/www/>Options Indexes FollowSymLinksAllowOverride None<RequireAll>Require all grantedRequire not ip 111.11.81.152</RequireAll></Directory>

    重啟服務(wù):

     /etc/init.d/apache2 restart

    再次訪問(wèn),發(fā)現(xiàn)權(quán)限受限,說(shuō)明設(shè)置成功。

    拓展:如果只允許某個(gè)固定ip訪問(wèn),那么可以這樣修改:

    <Directory /var/www/>Options Indexes FollowSymLinksAllowOverride None# Require all grantedRequire ip 111.11.82.8</Directory>

    設(shè)置賬號(hào)密碼訪問(wèn)

    對(duì)于某些私密文件,可以進(jìn)一步配置賬號(hào)密碼進(jìn)行身份驗(yàn)證。

    首先創(chuàng)建一個(gè)文件夾用來(lái)保存用戶信息:

    mkdir -p /usr/local/conf

    然后創(chuàng)建用戶:

    htpasswd -c /usr/local/conf/.usr zstar

    zstar是我創(chuàng)建的用戶名

    輸入密碼后,Apache會(huì)以密文方式存儲(chǔ)密碼,可以通過(guò)下面的方式查看用戶名和密碼密文:

    cat /usr/local/conf/.usr

    再次修改配置文件:

    vim /etc/apache2/apache2.conf

    修改內(nèi)容:

    <Directory /var/www/>Options Indexes FollowSymLinksAllowOverride AllAuthName "apache"AuthType BasicAuthUserFile "/usr/local/conf/.usr"Require user zstar</Directory>

    重啟服務(wù):

     /etc/init.d/apache2 restart

    再次訪問(wèn),成功彈出登錄驗(yàn)證,說(shuō)明配置成功。

    參考

    [1]ubuntu搭建http服務(wù)器用于下載ubuntu文件:https://blog.csdn.net/yy1695990107/article/details/116976994
    [2]Apache2 httpd.conf 配置詳解:https://blog.csdn.net/assassinice/article/details/78854139
    [3]Apache的訪問(wèn)控制:https://blog.csdn.net/m0_54434140/article/details/122249712
    [4]apache2三種模式及參數(shù)調(diào)優(yōu):https://blog.csdn.net/zhihui1017/article/details/54959194

    到此這篇關(guān)于教你使用Apache搭建Http下載服務(wù)器的文章就介紹到這了,更多相關(guān)Apache搭建Http下載服務(wù)器內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

    標(biāo)簽: Linux Apache
    主站蜘蛛池模板: 国产在线一区在线视频 | 欧美日韩一区二区三区视频在线观看 | 亚洲综合第一欧美日韩中文 | 国产精品成久久久久三级 | a级毛片在线免费看 | 精品久久久久中文字幕日本 | www.日本三级| 成人做爰视频www视频 | 精品欧美一区二区三区在线观看 | 亚洲一区二区中文字幕 | 久久精品国产精品亚洲20 | 日本精品1在线区 | 欧美在线观看高清一二三区 | 国产一级毛片午夜福 | 国产成人亚洲精品久久 | 成人欧美视频在线观看播放 | cao美女 | 久久视频6免费观看视频精品 | 国产午夜毛片v一区二区三区 | 日本欧美一区二区 | 久久久国产在线 | 国产亚洲综合精品一区二区三区 | hd最新国产人妖ts视频 | 亚洲欧美激情精品一区二区 | 在线はじめてのおるすばん | 欧美视频三区 | 丝袜美腿在线不卡视频播放 | 久久网免费 | 亚洲国产成人久久一区www | 毛片在线视频在线播放 | 久久精品国产只有精品2020 | 波多野结衣福利视频 | 亚洲怡红院在线 | 亚洲欧美日韩国产综合 | 亚洲欧美日韩三级 | 国产欧美日韩一区二区三区 | 亚洲成在线 | 国产精品免费看 | 韩国精品一区视频在线播放 | 一区在线免费观看 | 在线观看精品视频网站www |