yum安裝後默認配置文件javascript
1 ~]# cat /etc/httpd/conf/httpd.conf 2 ServerRoot "/etc/httpd" 3 Listen 80 4 Include conf.modules.d/*.conf 5 User apache 6 Group apache 7 ServerAdmin root@localhost 8 ServerName www.example.com:80 9 <Directory /> 10 AllowOverride none 11 Require all denied 12 </Directory> 13 DocumentRoot "/var/www/html" 14 <Directory "/var/www"> 15 AllowOverride None 16 Require all granted 17 </Directory> 18 <Directory "/var/www/html"> 19 Options Indexes FollowSymLinks 20 AllowOverride None 21 Require all granted 22 </Directory> 23 <IfModule dir_module> 24 DirectoryIndex index.html 25 </IfModule> 26 <Files ".ht*"> 27 Require all denied 28 </Files> 29 ErrorLog "logs/error_log" 30 LogLevel warn 31 <IfModule log_config_module> 32 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 33 LogFormat "%h %l %u %t \"%r\" %>s %b" common 34 <IfModule logio_module> 35 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio 36 </IfModule> 37 CustomLog "logs/access_log" combined 38 </IfModule> 39 <IfModule alias_module> 40 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 41 </IfModule> 42 <Directory "/var/www/cgi-bin"> 43 AllowOverride None 44 Options None 45 Require all granted 46 </Directory> 47 <IfModule mime_module> 48 TypesConfig /etc/mime.types 49 AddType application/x-compress .Z 50 AddType application/x-gzip .gz .tgz 51 AddType text/html .shtml 52 AddOutputFilter INCLUDES .shtml 53 </IfModule> 54 AddDefaultCharset UTF-8 55 <IfModule mime_magic_module> 56 MIMEMagicFile conf/magic 57 </IfModule> 58 EnableSendfile on 59 IncludeOptional conf.d/*.conf
配置格式:directive value;directive 不區分字符大小寫;value 爲路徑時,是否區分大小寫,取決於文件系統。php
ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Fullcss
ServerTokens Prod #建議關閉顯示服務器版本號
Listen [ip:]port 省略ip表示本機全部IP都監聽,至少要有一個監聽,此指令可重複出現屢次html
Listen 80
Persistent Connection:鏈接創建,每一個資源獲取完成後不會斷開鏈接,而是繼續等待其它的請求完成,默認關閉持久鏈接java
KeepAlive On #啓用長鏈接功能
KeepAliveTimeout 15 #保持鏈接15秒
MaxKeepAliveRequests 100 #斷開條件
~]# httpd -M |grep mpm
mpm_prefork_module (shared) #默認prefork處理模式
~]# vim /etc/httpd/conf.modules.d/00-mpm.conf #在此文件中配置使用那種處理模塊
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
#LoadModule mpm_event_module modules/mod_mpm_event.so
配置指定實現模塊加載格式: LoadModule <mod_name> <mod_path>nginx
示例:web
~]# vim /etc/httpd/conf.modules.d/00-base.conf
LoadModule auth_basic_module modules/mod_auth_basic.so
DocumentRoot "/path" 指向的路徑爲URL路徑的起始位置正則表達式
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Require all granted #受權能夠訪問
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
基於文件系統路徑:apache
<Directory 「/path"> #控制文件夾
...
</Directory>
<File 「/path/file」> #控制指定文件
...
</File>
<FileMatch "PATTERN"> #支持正則表達式
...
</FileMatch>
基於URL路徑:django
<Location ""> ... </Location> <LocationMatch "PATTERN"> ... </LocationMatch>
1)Options [+|-]option [[+|-]option] ... :後跟1個或多個以空白字符分隔的選項列表在選項前的 +,- 表示增長或刪除指定選項,默認Options FollowSymlinks
2)AllowOverride All|None|directive-type [directive-type] ... :與訪問控制相關的哪些指令能夠放在指定目錄下的.htaccess(由AccessFileName指定)文件中,覆蓋以前的配置指令;只對<directory>語句有效
3)Order Deny,Allow :定義生效次序;寫在後面的表示默認法則,2.4版本再也不支持
例:拒絕訪問站點下全部以.conf結尾的文件
DocumentRoot "/data/website"
<Directory "/data/website">
Require all granted
</Directory>
<Files "*.conf">
Require all denied
</Files>
例:容許訪問符號連接文件所指向的源文件,可是不容許返回索引列表給用戶
<Directory "/data/website">
Require all granted
Options -Indexes +FollowSymLinks
</Directory>
不容許指定的主機訪問
DocumentRoot "/data/website"
<Directory "/data/website">
<RequireALL>
Require all granted
Require not ip 192.168.0.2 #不容許0.2的主機訪問
</RequireALL>
</Directory>
只容許指定主機訪問
DocumentRoot "/data/website"
<Directory "/data/website">
<RequireAny>
Require all denied
Require ip 192.168.0.2 #只容許0.2主機訪問
</RequireAny>
</Directory>
~]# vim /etc/httpd/conf/httpd.conf
ErrorLog "logs/error_log" #錯誤日誌記錄文件
LogLevel warn #默認warn級別的錯誤記錄
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule> CustomLog "logs/access_log" combined #默認日誌記錄方式 </IfModule>
錯誤日誌的 LogLevel 可選:debug, info, notice, warn, error,crit, alert, emerg
訪問日誌:
建議:自定義日誌記錄格式
<IfModule log_config_module>
LogFormat "%h %l %u %{%F %T}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" custlog
CustomLog "logs/access_log" custlog
</IfModule>
AddDefaultCharset utf-8 #設置默認字符集爲utf-8,默認爲AddDefaultCharset Off
<Directory "/data/website">
Require all granted
AddDefaultCharset utf-8
</Directory>
格式: Alias /URL/ "/PATH/"
Alias /web2 /data/website2
<Directory "/data/website2">
Require all granted
</Directory>
訪問:http://192.168.0.7/web2
則是訪問/data/website
這個目錄下的站點
容許帳號文件中的全部用戶登陸訪問:Require valid-user
例:基於單用戶認證
1)定義安全域
Alias /admin "/data/admin"
<Directory "/data/admin">
AuthType Basic
AuthName "please input your user and password!"
AuthUserFile "conf.d/.htuser"
Require user admin
</Directory>
2)提供帳號和密碼存儲(文本文件)
~]# htpasswd -mc /etc/httpd/conf.d/.htuser admin
~]# cat /etc/httpd/conf.d/.htuser
admin:$apr1$Yfglmncl$BC1hebCpPjn1Sn.azt/Zu.
~]# systemctl restart httpd
3)測試訪問 :http://192.168.0.7/admin/
,輸入用戶名密碼便可訪問
例:基於組帳號進行認證
1)定義安全域
Alias /admin "/data/admin"
<Directory "/data/admin">
AuthType Basic
AuthName "please input your user and password!"
AuthUserFile "conf.d/.htuser"
AuthGroupFile "conf.d/.htgroup"
Require group gadmin gadmin2
</Directory>
2)提供帳號和密碼存儲(文本文件)
~]# htpasswd -c /etc/httpd/conf.d/.htuser tom
~]# htpasswd /etc/httpd/conf.d/.htuser jerry
~]# htpasswd /etc/httpd/conf.d/.htuser maria
~]# echo 'gadmin: tom jerry' > /etc/httpd/conf.d/.htgroup
~]# echo 'gadmin2: tom maria' >> /etc/httpd/conf.d/.htgroup
3)測試訪問 :http://192.168.0.7/admin/
,輸入用戶名密碼便可訪問
~]# vim /etc/httpd/conf.d/userdir.conf
<IfModule mod_userdir.c>
#UserDir disabled
UserDir public_html
</IfModule>
<Directory "/home/user1/public_html">
AuthType Basic
AuthName "user1 home dir"
AuthUserFile "conf.d/.htuser"
Require user user1
</Directory>
~]# htpasswd -c /etc/httpd/conf.d/.htuser user1
~]# systemctl restart httpd
~]# su -user1
~]$ mkdir public_html
~]$ echo "user1 home dir" > public_html/index.html
~]$ setfacl -m u:apache:x /home/user1/
訪問:http://192.168.0.7/~user1/
站點,輸入密碼便可登陸
當客戶請求的網頁並不存在時,服務器將產生錯誤文檔,若是於打開了ServerSignature選項,錯誤文檔的最後一行將包含服務器的名字、Apache的版本等信息;若是不對外顯示這些信息,就能夠將這個參數設置爲Off;設置爲Email,將顯示ServerAdmin 的Email提示;2.4版本默認值關閉,2.2版本默認開啓
LoadModule status_module modules/mod_status.so 確認此模塊已加載
httpd]# vim conf.d/myhttp.conf
<Location "/status">
SetHandler server-status
Require all granted
</Location>
~]# systemctl restart httpd
訪問http://192.168.0.7/status
查看服務器狀態信息
注意:通常虛擬機不要與main主機混用;所以,要使用虛擬主機,通常先禁用main主機;註釋中心主機的DocumentRoot指令便可。
2.4版本基於FQDN的虛擬主機再也不須要NameVirutalHost指令
data]# mkdir website{1..3}
data]# echo '<h1>website 1</h1>' > website1/index.html
data]# echo '<h1>website 2</h1>' > website2/index.html
data]# echo '<h1>website 3</h1>' > website3/index.html
~]# vim /etc/httpd/conf/httpd.conf
#Listen 80
#DocumentRoot "/var/www/html"
三種實現方案:
~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 81
Listen 82
Listen 83
<Directory "/data">
Require all granted
</Directory>
<VirtualHost *:81>
DocumentRoot "/data/website1"
ServerName www.web1.com
ErrorLog "logs/web1_error_log"
TransferLog "logs/web1_access_log"
</VirtualHost>
<VirtualHost *:82>
DocumentRoot "/data/website2"
ServerName www.web2.com
ErrorLog "logs/web2_error_log"
TransferLog "logs/web2_access_log"
</VirtualHost>
<VirtualHost *:83>
DocumentRoot "/data/website3"
ServerName www.web3.com
ErrorLog "logs/web3_error_log"
TransferLog "logs/web3_access_log"
</VirtualHost>
~]# systemctl restart httpd
分別訪問:
http://192.168.0.7:81
和http://192.168.0.7:82
和http://192.168.0.7:83
~]# ip a a 192.168.0.11/24 dev eth0
~]# ip a a 192.168.0.12/24 dev eth0
~]# ip a a 192.168.0.13/24 dev eth0
~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 80
<Directory "/data">
Require all granted
</Directory>
<VirtualHost 192.168.0.11:80>
DocumentRoot "/data/website1"
ServerName www.web1.com
ErrorLog "logs/web1_error_log"
TransferLog "logs/web1_access_log"
</VirtualHost>
<VirtualHost 192.168.0.12:80>
DocumentRoot "/data/website2"
ServerName www.web2.com
ErrorLog "logs/web2_error_log"
TransferLog "logs/web2_access_log"
</VirtualHost>
<VirtualHost 192.168.0.13:80>
DocumentRoot "/data/website3"
ServerName www.web3.com
ErrorLog "logs/web3_error_log"
TransferLog "logs/web3_access_log"
</VirtualHost>
~]# systemctl restart httpd
分別訪問:
192.168.0.11
和192.168.0.12
和192.168.0.13
~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 80
<Directory "/data">
Require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot "/data/website1"
ServerName www.web1.com
ErrorLog "logs/web1_error_log"
TransferLog "logs/web1_access_log"
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/website2"
ServerName news.web2.com
ErrorLog "logs/web2_error_log"
TransferLog "logs/web2_access_log"
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/website3"
ServerName bbs.web3.com
ErrorLog "logs/web3_error_log"
TransferLog "logs/web3_access_log"
</VirtualHost>
~]# systemctl restart httpd
客戶端測試:
~]# vim /etc/hosts 添加如下內容 192.168.0.7 www.web1.com news.web2.com bbs.web3.com ~]# curl www.web1.com ~]# curl news.web2.com ~]# curl bbs.web3.com
LoadModule deflate_module modules/mod_deflate.so 模塊默認已經加載
# httpd -M |grep deflate
deflate_module (shared)
conf.d]# vim myhttpd.conf
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
DeflateCompressionLevel 9 #壓縮比爲 9
~]# vim /etc/httpd/conf/httpd.conf
EnableSendfile On