Lukáš Bařinka
© 2021
REV 2.10
a2*a2query
Query configuration status
Zjištění konfigurace
a2query -c [CONF]
a2query -m [MOD]
a2query -s [SITE]
a2enconf apache2-doc
\ as a last character at the end of line# character\#${VAR}
Define directives take precedence/etc/apache2/envvarsapache2ctl -k restart -D …=……/envvars:
export APACHE_MANUAL=/man
Alias in …/conf-enabled/apache2-doc.conf:
${APACHE_MANUAL}
${VAR}
Define/etc/apache2/envvarsapache2ctl -k restart -D …=……/envvars přidat:
export APACHE_MANUAL=/man
…/conf-enabled/apache2-doc.conf upravit cestu v Aliasu na:
${APACHE_MANUAL}
<Directory> <DirectoryMatch> <Files> <FilesMatch><Location> <LocationMatch> <Proxy><VirtualHost><Directory> <DirectoryMatch> <Files> <FilesMatch><Location> <LocationMatch> <Proxy><VirtualHost>.htaccess file<Directory> container.htaccess configuration directives
AccessFileName Context: server, vrtualhost | Default: .htaccessAllowOverride Context: only <Directory> | None – ignoring .htaccess, All, Directive_type/ Unless is forbidden.htaccess souborů
AccessFileName Kontext: celý server, virtualhost | Default: .htaccessAllowOverride Kontext: pouze <Directory> | None – ignoruje .htaccess, All, Typ_direktivy/ Pokud to není zakázáno.htaccess detailsAllowOverrideList Individual directives that are allowed in .htaccess files (extends AllowOverride)
AllowOverride None
AllowOverrideList Redirect RedirectMatch
AllowOverrideList is set to None and AllowOverride is set to None.htaccess files are completely ignoredAllowOverrideList Povoluje konkrétní direktivy (doplňuje AllowOverride)
AllowOverride None
AllowOverrideList Redirect RedirectMatch
AllowOverrideList je nastavená na None a zároveň AllowOverride je nastavená na None.htaccess soubory jsou úplně ignorovány<IfDefine name> … </IfDefine>
Parameter is defined on the httpd command line using -D name
Enables / disables server features
<IfModule module_name> … </IfModule>
When module is loaded (static/dynamic)
<IfVersion [<>=]x.y.z> … </IfVersion>
According to server version (mod_version)
<If condition> … </If>, <ElseIf>, <Else>
Cannot be nested
<IfDefine název> … </IfDefine>
Při definici názvu pomocí přepínače -D název
Povoluje / zakazuje vlastnosti serveru
<IfModule jméno_modulu> … </IfModule>
Při načtení modulu do serveru
<IfVersion [<>=]x.y.z> … </IfVersion>
Podle verze serveru (mod_version)
<If podmínka> … </If>, <ElseIf>, <Else>
Nelze do sebe vnořovat
Instead of DEFINE command line option, the configuration is controlled by symbolic links in *-enabled/*.conf in Debian
The Init script does not use -D … options
V Debianu se místo DEFINE konfigurace ovládá symbolickými linky v *-enabled/*.conf
Init skript nemá prostor pro zadávání -D …
in, =~, !~
Číselné, řetězcové, souborové, in, =~, !~
%{VARIABLE} variable syntaxzápis proměnnéHTTP_* HTTP parametersparametry HTTP protokoluREQUEST_* request informationinformace o požadavkuREMOTE_* client informationinformace o klientoviSERVER_* server informationinformace o serveruTIME_* current date and timeaktuální datum a čas
# %{ funcname : funcargs }
Header set foo-checksum "expr=%{md5:foo}"
# funcname ( args )
<If "md5('foo') == 'abc…'">
in, =~, !~
%{VARIABLE} zápis proměnnéHTTP_* parametry HTTP protokoluREQUEST_* informace o požadavkuREMOTE_* informace o klientoviSERVER_* informace o serveruTIME_* aktuální datum a čas
# %{ funcname : funcargs }
Header set foo-checksum "expr=%{md5:foo}"
# funcname ( args )
<If "md5('foo') == 'abc…'">
List of variables, operators and functions is in /manual/en/expr.html
Seznam proměnných, operátorů a funkcí je v /manual/en/expr.html
# Force text/plain if requesting a file with the query string contains 'forcetext'
<If "%{QUERY_STRING} =~ /forcetext/">
ForceType text/plain
</If>
# Check an environment variable for a regular expression, negated.
<If "! reqenv('REDIRECT_FOO') =~ /bar/">
Header set matched true
</If>
# Check result of URI mapping by running in Directory context with -f
<Directory "/var/www">
AddEncoding x-gzip gz
<If "-f '%{REQUEST_FILENAME}.unzipme' && ! %{HTTP:Accept-Encoding} =~ /gzip/">
SetOutputFilter INFLATE
</If>
<Directory>
# …/conf-enabled/apache2-doc.conf
<Directory …>
…
Require all denied
<If "%{TIME_SEC} =~ /[02468]$/">
Require all granted
</If>
…
</Directory>
while sleep 1; do
date
curl -I http://localhost/manual 2>/dev/null | head -1
done
<Directory pattern> in order of matching length from the shortest to the longest
E.g. for directory /home/*/public_html:
//home/home/user/home/user/public_html.htaccess files<Directory ~ ERE> and <DirectoryMatch ERE> in order of appearance in configuration<Directory vzor> v pořadí podle délky shody od nejkratší shody po nejdelší
Např. pro adresář /home/*/public_html:
//home/home/user/home/user/public_html.htaccess<Directory ~ ERE> a <DirectoryMatch ERE> v pořadí podle uvedení v konfiguraci<If> a <VirtualHost> are the last ones To be able to differentiate virtualhosts setup from main server setup<If> a <VirtualHost> kontejnery Aby bylo možné pro virtualhost upravit nastavení main serveru<Directory /> is granted for all by default/ !<Directory /> je povolen po všechny/ zakázat !<Directory …> containers processing<Files …> In order of appearance in configuration
<Files pattern><Files ~ ERE><FilesMatch ERE><Directory> container
<Location pattern><Location ~ ERE><LocationMatch ERE><Directory …><Files …> V pořadí podle uvedení v konfiguraci
<Files vzor><Files ~ ERE><FilesMatch ERE><Directory>
<Location vzor><Location ~ ERE><LocationMatch ERE>?, *, []/ character is not expanded, must be present explicitly. , ?, *, +, |, ^, $, ...?, *, []/ se nenahrazuje, musí být explicitně uveden. , ?, *, +, |, ^, $, ...Paths containing // characters can be, or can be not equal to /
Cesty obsahující znaky // mohou, ale nemusejí být shodné s /
/adr1//adr2 → /adr1/adr2//adr !~ ^/adrServerRoot (core)
Root directory with server installation (--prefix)
Kořenový adresář s instalací serveru (--prefix)
ServerRoot "/usr/lib64/apache2"
Listen (mpm)
IP address/domain-name, TCP port (can be used several times)
IP adresa/jméno, TCP port serveru (je možno použít vícekrát)
Listen 80
ServerName (core)
Server name for response (e.g. during redirection)
Jméno serveru pro odpovědi (např. při přesměrování)
ServerName www.example.com
User / Group (mpm)
User / Group identity inside operating system when started by root
Identita uživatele / skupiny pro běh serveru v případě, že je server spuštěn rootem
User apache
Group apache
DocumentRoot (core)
Webspace root (/) to filesystem mapping
Mapování kořene webu (/) na filesystém
DocumentRoot "/var/www/localhost/htdocs"
mod_so)
Dynamické načítání modulů (mod_so)
LoadModule dir_module modules/mod_dir.so
mod_dir)
Výchozí stránka pro adresář (mod_dir)
DirectoryIndex index.html index.htm
mod_autoindex)
Automaticky generovaný index (mod_autoindex)
Options +Indexes
IndexOptions FoldersFirst IgnoreCase
IndexIgnore README .htaccess *.bak *~
AddIcon (IMG,/icons/image.xbm) .gif .jpg .xbm
Options -FollowSymlinks
Links does not change filename when compared to request filename.
That allows to circumvent conditions inside <Directory> containers.
It's recommended to forbid FollowSymlinks for security reasons.
U linků se nemění název, vždy se porovnává ten z dotazu.
Umožňuje to tak obejít podmínku v <Directory>.
Pro větší bezpečnost je vhodné zakázat FollowSymlinks.
core)
Nastavení znakové sady (core)
AddDefaultCharset utf-8
core)
Logování (core)
ErrorLog /var/log/apache2/error_log
LogLevel error
HostnameLookups off
mod_log_config)
Logování – vlastní (mod_log_config)
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common
Require directive
Direktiva Require
Require host address
Require ip ip.address
Require forward-dns hostname
Require local
Require all denied|granted
Require not …
<RequireAll>, <RequireAny>, <RequireNone> containers
Kontejnery <RequireAll>, <RequireAny>, <RequireNone>
<RequireAll>
Require all granted
Require not ip 10.252.46.165
</RequireAll>
More Require directives outside <RequireAll/Any/None> containers act like inside <RequireAny> container.
Pokud je více direktiv Require mimo kontejnery <RequireAll/Any/None>,
platí mezi nimi logické OR. Tedy jako by byly v kontejneru <RequireAny>
Authorization (mod_access_compat)
Autorizace (mod_access_compat)
147.32.80.90147.32.8010.0.0.0/8mypc.example.orgexample.org.netall| Match | Allow,Deny | Deny,Allow |
|---|---|---|
| only Allow | allowed | allowed |
| only Deny | denied | denied |
| none | denied | allowed |
| both | denied | allowed |
| Shoda | Allow,Deny | Deny,Allow |
|---|---|---|
| pouze Allow | povoleno | povoleno |
| pouze Deny | zakázáno | zakázáno |
| neshoda | zakázáno | povoleno |
| obojí | zakázáno | povoleno |
The goal is to change basic configuration to serve the content from chosen directory. Some directories should be accessible and others will be inaccessible. Errors should be stored in log file.
Cílem je upravit základní konfiguraci pro poskytování obsahu ze zvoleného adresáře. Některé podadresáře budou přístupné a jiné nepřístupné Chyby se objeví v logu.
DocumentRoot and enable directory listsDocumentRoot into WSDocumentRoot a umožnit adresářové výpisyDocumentRoot do WS
systemctl enable apache2
systemctl start apache2
journalctl -xe
netstat -tapln
systemctl enable apache2
systemctl start apache2
journalctl -xe
netstat -tapln
It is important to set VirtualBox network type to Bridge to access webserver from outside of VirtualBox machine.
Pro přístup do VirtualBoxu zvenčí je potřeba nastavit síť na Bridge.
curl command
curl -i localhost # headers as a part of output (GET)
curl -I localhost # headers only (HEAD)
grep -i -R 'documentroot' . # -i = case-insensitive -R = recursive
systemctl command
systemctl restart apache2.service
apachectl -t
alias a='apachectl -t && systemctl restart apache2 && echo Restart OK'
curl
curl -i localhost # hlavičky součástí výstupu (GET)
curl -I localhost # jenom hlavičky (HEAD)
grep -i -R 'documentroot' . # -i = case-insensitive -R = recursive
systemctl
systemctl restart apache2.service
apachectl -t
alias a='apachectl -t && systemctl restart apache2 && echo Restart OK'
/var/www/main directory as a web server root (for http://localhost/ URL)AddDefaultCharset to be UTF-8 for that directory (discover whether is that already set elsewhere)telnet or curl to diagnose the problem/var/www/main/private directory/var/www/main bude přístupný jako kořen webu (tedy jako http://localhost/)AddDefaultCharset na hodnotu UTF-8 (zjistěte, zda již někde není nastaveno)/var/www/main/private/var/www/main/users directory in case there is no index file available.htaccess files usage in all subdirectories of /var/www/main/users/ and allow to overwrite Options and Limit
This step will allow to process .htaccess file in Alice directory. Does it change the situation anyhow?
/var/www/main/users povolte vypsání obsahu adresáře, pokud není k dispozici index/var/www/main/users/ povolte použití .htaccess souborů a přenastavení Options a Limit
Tento krok povolí zpracování .htaccess souboru Alice, změní se nějak situace?
/var/www/main/users/alice/data using .htaccess filebak or txt suffixeshttp://localhost/users/alice/data//var/www/main/users/alice/data pomocí souboru .htaccessbak nebo txthttp://localhost/users/alice/data/txt or bak suffixes inside /var/www/main/users/alice/data directory using .htaccess file.htaccess povolte zobrazení souborů s příponou txt a bak v adresáři /var/www/main/users/alice/dataIs there a problem displaying a file?
Nezobrazuje se soubor, který by měl?
tail -f /var/log/apache2/*
[…] [authz_core:error] [pid …] [client …] AH01630: client denied by server configuration: …
.htaccess files inside DocumentRoot directory
Hledejte .htaccess soubor v DocumentRoot
find /var/www/main -name .htaccess
/var/www/main/users/alice/data/.htaccess
cat /var/www/main/users/alice/data/.htaccess
Options +Indexes
<FilesMatch "\.(bak|txt)$">
Order deny,allow
Allow from all
</FilesMatch>
Do not mix legacy authorization (Allow/Deny) with
contemporary authorization model (Require)!
Nemixujte legacy autorizaci (Allow/Deny) a
současný přístup (Require)!
/vhosts web space into /var/www/vhosts directory using Alias directiveAlias namapujte do struktury webu na URL /vhosts adresář /var/www/vhostsAlias directive documentation: http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias
Alias what(webspace) where(filesystem)
grep command
grep -ri errorlog /etc/apache2
c() { grep -rin -C2 --color=always "$@" /etc/apache2 | cut -d/ -f4; }
mod_info module must be loaded, then use <F3>: ErrorLog
http://localhost/server-info
Alias najděte v dokumentaci: http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias
Alias co(webspace) kam(filesystem)
grep
grep -ri errorlog /etc/apache2
c() { grep -rin -C2 --color=always "$@" /etc/apache2 | cut -d/ -f4; }
mod_info) [<F3>: ErrorLog]
http://localhost/server-info
ServerNameServerAliasDocumentRoot_default_ one will be used, else main server configuration will be used
ServerNameServerAliasDocumentRoot_default_, pokud není ani ten,
použije se nastavení hlavního serveru
dum1, dum2, dum3 with IP addresses 10.0.-X-.1 to 10.0.-X-.3
The -X- mark in IP address must correspond to 4th IP address octet of your default network interface
Vytvořte síťové rozhraní dum1, dum2, dum3 s IP adresami 10.0.-X-.1 až 10.0.-X-.3
Značka -X- v IP adrese musí odpovídat 4. oktetu IP adresy výchozího síťového rozhraní
ip=$( ip route get 1.2 | awk '{ print $7 }' | cut -d. -f4 )
for n in {1..3}; do
ip link add dum$n type dummy
ip addr add 10.0.$ip.$n/24 dev dum$n
ip link set dum$n up
done
/etc/hosts file contains following records
The -X- mark in IP address must correspond to 3th IP address octet of your dumX network interface
Zkontrolujte, zda soubor /etc/hosts obsahuje následujcící záznamy
Značka -X- v IP adrese musí odpovídat 3. oktetu IP adresy rozhraní dumX
10.0.-X-.1 www.mycorp.net
10.0.-X-.1 www.aliascorp.net
10.0.-X-.2 www.othercorp1.net
10.0.-X-.2 www.othercorp2.net
10.0.-X-.2 www.othercorp3.net
10.0.-X-.3 www.nocorp.net
vi /etc/hosts
The goal is to configure IP-based virtualhosting. So, there will be different content served for different IP addresses. You should create separate configuration file for this task.
Cílem je vytvořit konfiguraci pro IP-based virtualhosting. Tedy pro různé IP adresy bude webserver poskytovat různý obsah. Pro tuto konfiguraci vytvořte samostatný soubor.
www.mycorp.net domain_default_)www.mycorp.net_default_)Include directive inside main configuration file
It is possible to create that file inside sites-available directory and then enable that site using a2ensite … and add Listen …:80 directive into ports.conf file Include directive)
10.0.-X-.1 IP address for server www.mycorp.net and set its alias to www.aliascorp.net/var/www/vhosts/mycorpInclude jej načtěte do hlavního konfiguračního souboru
Je možné místo Include použít příkaz a2ensite … a přidat Listen …:80 do souboru ports.conf (konfigurace virtualhosta by měla být v adresáři sites-available)
10.0.-X-.1 vyhraďte pro server se jménem www.mycorp.net a aliasem www.aliascorp.net/var/www/vhosts/mycorp/var/log/apache2/mycorp_error_log file/var/log/apache2/mycorp_error_logDocumentRoot to /var/www/vhosts/default
The default DocumentRoot is /var/www/html in Debian distribution
DocumentRoot na /var/www/vhosts/default
V Debianu je výchozí DocumentRoot ve /var/www/html
The goal is to configure name-based virtualhosting. So, the server serves different content for the same IP address. Create separate configuration file for this task.
Cílem je vytvořit konfiguraci pro name-based virtualhosting. Tedy pro stejnou IP adresu bude webserver poskytovat různý obsah. Pro tuto konfiguraci vytvořte samostatný soubor.
www.othercorp1.net virtualhostwww.othercorp2.net virtualhostwww.othercorp1.netwww.othercorp2.netInclude directive inside main configuration file
It is possible to use a2ensite … as well in this case instead of Include directive
ServerName is www.othercorp1.netDocumentRoot is /var/www/vhosts/othercorp1ServerName is www.othercorp2.netDocumentRoot is /var/www/vhosts/othercorp2www.othercorp2.netDocumentRoot je /var/www/vhosts/othercorp2The goal is to create configuration that includes IP-based and name-based virtualhosting at the same time. For this task use already created files.
Cílem je vytvořit konfiguraci pro souběh IP-based a name-based virtualhostingu. Pro tuto konfiguraci použijte již vytvořené soubory.