標籤:style blog http color 使用 strong
源自《Linux 營運之道》丁一明編著 一書的總結
Apache HTTP Server項目是在Windows和UNIX等平台上可以啟動並執行跨平台開源HTTP伺服器的軟體,該項目的目標是提供安全、高效、可擴充的HTTP服務。Apache HTTP Server非常重要的是它採用了模組化設計模型,Apache模組分為靜態模組和動態模組,靜態模組是Apache是最基本的模組,是無法隨時添加和卸載的模組,靜態模組在編譯軟體時設定。動態模組是可以隨時添加和刪除的模組,這樣的設計師的企業部署Apache時可以獲得最大的靈活性。Apache模組將被編譯為動態共用對象(DSO),這些動態共用對象獨立於httpd程式,DSO模組可以在編譯Apache添加的,也可以在後期隨時通過Apache Extension Tool(apxs)工具編譯添加模組。可以使用httpd –M命令查看模組載入清單。
下載並安裝
[[email protected] tempal]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.9.tar.gz
[[email protected] tempal]# wget http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.3.tar.gz
[[email protected] tempal]# wget http://mirror.bjtu.edu.cn/apache/apr/apr-1.5.1.tar.gz
安裝依賴的軟體:
[[email protected] tempal]# yum -y install gcc autoconf automake make pcre pcre-devel openssl openssl-devel
[[email protected] tempal]# tar -xzf httpd-2.4.9.tar.gz -C /usr/src/
[[email protected] tempal]# tar -xzf apr-1.5.1.tar.gz -C /usr/src/
[[email protected] tempal]# tar -xzf apr-util-1.5.3.tar.gz -C /usr/src/
[[email protected] tempal]# cd /usr/src/apr-1.5.1/
[[email protected] apr-1.5.1]# ./configure
[[email protected] apr-1.5.1]# make && make install
[[email protected] apr-1.5.1]# cd /usr/src/apr-util-1.5.3/
[[email protected] apr-util-1.5.3]# ./configure --with-apr=/usr/local/apr/
[[email protected] apr-util-1.5.3]# make && make install
[[email protected] httpd-2.4.9]# ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite --with-mpm=worker --with-suexec-bin --with-apr=/usr/local/apr/
[[email protected] httpd-2.4.9]# make && make install
可以通過./configure –help查看指令碼支援的
選項 |
描述 |
--prefix |
指定Apache httpd程式的主安裝目錄 |
--enable-so |
開啟模組化功能,支援DSO(動態共用對象) |
--enable-ssl |
支援SSL加密 |
--enable-rewrite |
支援地址修正 |
--with-mpm |
設定Apache httpd工作模式 |
--with-suexec-bin |
支援SUID、SGID |
--withapr |
支援apr程式絕對路徑 |
啟動服務:
[[email protected] ~]# /usr/local/apache2/bin/apachectl start
[[email protected] ~]# netstat -tulnp | grep http
[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[[email protected] ~]# service iptables save
安裝後,Apache會提供名為apachectl啟動指令碼。指令碼在/usr/local/apache2/bin目錄下,該指令碼用來進行Apache httpd的啟動、關閉以及測試功能,具體的參數見下表。
參數 |
描述 |
Start |
啟動httpd程式 |
Stop |
關閉httpd程式 |
Restart |
重啟httpd程式 |
Graceful |
啟動httpd,不中斷現有的http串連請求 |
Graceful-stop |
關閉httpd,不中斷現有的http串連 |
Status |
查看httpd程式目前狀態 |
Configtest |
檢測httpd主配置文法 |
沒有修改設定檔的情況下用start啟動httpd程式,可能返回錯誤提示:
[[email protected] ~]# /usr/local/apache2/bin/apachectl start
AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
但是確實服務已經啟動了。
設定檔解析:
設定檔預設在/usr/local/apache2/conf目錄下,該目錄的主要設定檔是httpd.conf檔案。還有一些位於extra目錄下的附加設定檔。主設定檔以容器組成,容器使用<容器名稱>開始,以</容器名稱>結尾,容器的指令一般僅在容器內有效。
² SeverRoot指令:設定Apache軟體的安裝主目錄,如果採用源碼安裝,這預設路徑是/usr/local/apache2
² Listen指令:設定伺服器監聽IP和連接埠號碼。文法格式為:Listen[IP地址:]連接埠[協議],其中ip地址與協議可為可選項。可以多吃使用listen指令來開啟多個連接埠。
² LoadModule指令:在編譯時間加上—enable-so將mod_so以靜態方式編譯到Apache核心模組中,LoadModule指令的作用就是載入模組。
² LoadFile指令:通過絕對路徑載入modules目錄下的模組檔案。
² serverAdmin指令:提供一個解決問題的郵件地址。
² ServeName指令:設定伺服器原生主機名稱和連接埠,對URL地址的重新導向很重要。
² DocumentRoot指令:設定Web服務對用戶端開放可見的主目錄,也就是用戶端訪問網站的根路徑,預設為/usr/local/apache2/htdocs。
² ErrorLog指令:定位服務器錯誤記錄檔的位置,預設使用相對路徑,為ServerRoot目錄下的“logs/error_log”檔案。
² ErrorLogFormat指令:設定錯誤記錄檔的格式,Apache HTTP Server預先定義了很多格式字串可以直接引用。
² CustomLog指令:該指令設定用戶端的訪問記錄檔以及日誌格式,格式為“logs/access_log”,文法格式為CustomLog 檔案名稱 格式
² LogFormat指令:該指令描述使用者記錄檔格式,可以直接使用Apache預先設定的格式字串,一般我們會為LogFormat指令設定的日子格式格式建立別名。
² Include指令:文法格式為:Options[+|-]選項[[+|0-]選項]。選項可以設定為None,代表不啟動任何額外的功能。也可以使用如下常用選項:All,開啟除MultiViews之外的所有選項;ExecCGI:允許執行Options指定目錄下的所有CGI指令碼;FollowSymlinks:允許Options指定目錄下的檔案連結到目錄外的檔案或目錄。Indexes:如果與URL對應的Options目錄下找不到DirectoryIndex指定的首頁文檔,則Apache將會把目前的目錄的所有檔案索引出來。
² Order指令:控制預設訪問狀態以及Allow和Deny的次序,如果使用Order deny,allow,則先檢查拒絕,然後在允許。如果有衝突,允許優先,預設規則為允許。如果使用Order allow,deny,則先檢查允許,然後在拒絕。如果有衝突,拒絕優先,預設規則為拒絕。例如:
案例一,所有都拒絕:
Order deny,allow
Deny from all
案例二,除192.168.118.254以外都拒絕:
Order allow,deny
Allow from 192.168.118.254
案例三,拒絕所有:
Order allow,deny
Allow from 192.168.118.254
Deny from All
案例四:除192.168.118.254以外都拒絕:
Order deny,allow
Deny from all
Allow from 192.168.118.254
² ifDefine容器:僅在啟動Apache時測試條件為真才會被處理,測試條件需要在啟動Apache時通過httpd –D定義。文法格式為:<IfDefine>指令</IfDefine>
<IfDefine MemCache>
LoadModule mem_cache_module modules/mod_mem_cache.so
</IfDefine>
說明只有在啟動Apache時,如果使用了httpd –D Memcache,這Apache將載入mod_mem_cache模組,如果沒有則Apache將不載入這些模組。
² ifModule容器
使用IfModule容器可以封裝僅在條件滿足時才會處理的指令。文法格式:<IfModule[!]模組>指令</IfModule>
例子:
<IfModule unixd_module>
User daemon
Group daemaon
</IfModule>
僅在載入了unixd_module模組後,User daemon和Group daemon才會被Apache處理。
² Directory容器:僅應用特定的檔案系統目錄、子目錄以及目錄下的內容,文法格式:<Directory directory-path>指令</Directory>。路徑可以使用~匹配Regex。
例子:
<Directory “/usr/local/apche2/htdocs”>
Options Indexs FollowSymLinks
</Directory>
Options Indexs FollowSymLinks僅對/usr/local/apache2/htdocs目錄有效。
² <DirectoryMatch>:類似於Directory,但可以直接使用Regex匹配。無需要使用~符號才可以匹配。
² File容器:類似於Directory,但Files容器內的指令應用於特定的檔案,文法格式為:<File 檔案名稱>指令</File>
² FilesMatch容器:類似於File,但可以直接使用Regex匹配。無需要使用~符號才可以匹配。
² Location容器:僅對特定的URL有效,文法格式:<Location URL-path|URL>指令</Location>
² LocationMathch容器和VirtualHost容器
虛擬機器主機應用案例:
將主設定檔的語句開啟:
[[email protected] ~]# gedit /usr/local/apache2/conf/httpd.conf
去掉注釋
Include conf/extra/httpd-vhosts.conf
更改httpd-vhosts.conf檔案
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs/example"
ServerName www.example.com
ServerAlias web.example.com
ErrorLog "logs/example.com-error_log"
CustomLog "logs/example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs/test"
ServerName www.test.com
ServerAlias web.test.com
ErrorLog "logs/test.com-error_log"
CustomLog "logs/test.com-access_log" common
</VirtualHost>
開啟:
[[email protected] ~]# mkdir -p /usr/local/apache2/htdocs/{example,test}
[[email protected] ~]# echo "example.com">/usr/local/apache2/htdocs/example/index.html
[[email protected] ~]# echo "test.com">/usr/local/apache2/htdocs/test/index.html[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[[email protected] ~]# /usr/local/apache2/bin/apachectl restart
網站安全應用案例:
TLS(Transport Layer Security)是對SSL的擴充和最佳化,它可以提供資料安全,同時確保資料的完整性。Apache HTTP Server通過mod_ssl模組來實現對SSL/TLS的支援。
部署認證
[[email protected] ~]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...................................+++
...+++
e is 65537 (0x10001)
[[email protected] ~]# openssl req -new -x509 -key server.key -out server.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Guangzhou
Locality Name (eg, city) [Default City]:Guangzhou
Organization Name (eg, company) [Default Company Ltd]:ABC
Organizational Unit Name (eg, section) []:tech
Common Name (eg, your name or your server‘s hostname) []:web1
Email Address []:[email protected]
[[email protected] ~]# cp {server.key,server.crt} /usr/local/apache2/conf/
修改Apache設定檔,主設定檔中需要載入mod_ssl以及mod_socache_shmcb兩個模組,同時使用Include指令讀取conf/extra目錄下的http-ssl設定檔。
[[email protected] ~]# gedit /usr/local/apache2/conf/httpd.conf
去掉注釋
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
[[email protected] ~]# gedit /usr/local/apache2/conf/extra/httpd-ssl.conf
修改內容:
Listen 443
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
<VirtualHost _default_:443>
DocumentRoot "/usr/local/apache2/htdocs/secret"
ServerName www.abc.com:443
ServerAdmin [email protected]
ErrorLog "/usr/local/apache2/logs/error_log"
TransferLog "/usr/local/apache2/logs/access_log"
SSLEngine on
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/usr/local/apache2/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-5]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
CustomLog "/usr/local/apache2/logs/ssl_request_log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
[[email protected] ~]# mkdir -p /usr/local/apache2/htdocs/secret
[[email protected] ~]# echo "secret">/usr/local/apache2/htdocs/secret/index.html
[[email protected] ~]# /usr/local/apache2/bin/apachectl restart
[[email protected] ~]# iptables -I INPUT -p tcp --dport 443 -j ACCEPT
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[[email protected] ~]# gedit /etc/hosts