實驗環境:RHEL7.0 server1.example.com 172.25.254.1
實驗内容: 1.Apache安裝
2.Apache主配置檔案
3.更改預設通路目錄
4.更改預設端口
5.通路目錄權限設定
6.基于使用者的身份認證配置(加密網頁)
7.更改預設通路頁面
8.添加通路php,cgi等網頁
9.虛拟主機
10.HTTPS自定義簽名證書
11.網頁重寫
1.Apache安裝
1.1 安裝apache軟體包
[root@server1 ~]# yum install httpd httpd-manual
1.2啟動apache服務
[root@server1 ~]# systemctl start httpd;systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
1.3檢視監聽端口
[root@server1 ~]# netstat -antple|grep httpd
tcp6 0 0 :::80 :::* LISTEN 0 119746 1428/httpd
[root@server1 ~]# ss -antple |grep httpd
LISTEN 0 128 :::80 :::* users:(("httpd",1433,4),("httpd",1432,4),("httpd",1431,4),("httpd",1430,4),("httpd",1429,4),("httpd",1428,4)) ino:119746 sk:ffff88003bfd6800 <->
2.Apache主配置檔案:/etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" #用于指定Apache的運作目錄
Listen 80 # 監聽端口
User apache #運作apache程式的使用者群組
Group apache
ServerAdmin root@localhost #管理者郵箱
DocumentRoot "/var/www/html" #網頁檔案的存放目錄
<Directory "/var/www/html"> #<Directory>語句塊自定義目錄權限
Require all granted
</Directory>
ErrorLog "logs/error_log" #錯誤日志存放位置
AddDefaultCharset UTF-8 #預設支援的語言
IncludeOptional conf.d/*.conf #加載其它配置檔案
DirectoryIndex index.html #預設首頁名稱
3.更改預設通路目錄
1)改安全上下文
[root@server1 ~]# getenforce
Enforcing
[root@server1 ~]# mkdir -p /www/html
[root@server1 ~]# ls -ldZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@server1 ~]# ls -ldZ /www/html/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/
[root@server1 ~]# semanage fcontext -a -t httpd_sys_content_t '/www/html(/.*)?'
[root@server1 ~]# restorecon -FvvR /www/html/
restorecon reset /www/html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
[root@server1 ~]# systemctl restart httpd
2) 改配置
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
DocumentRoot "/www/html"
<Directory />
Require all granted
</Directory>
3)測試
[root@server1 ~]# vim /www/html/index.html
hello,willis.
[root@server1 ~]# systemctl restart httpd.service
4.更改預設端口
1)檢視可更改端口
[root@server1 ~]# semanage port -l |grep http # selinux标簽
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
2)配置
#Listen 12.34.56.78:80
Listen 8080
3)通路
<a href="http://s1.51cto.com/wyfs02/M01/87/19/wKiom1fT-GHzddIMAAAhFREJkgI423.png" target="_blank"></a>
5.通路目錄權限
DocumentRoot "/www/html"
<Directory />
Require all granted
order allow,deny #讀取順序,後讀取的覆寫前讀取的
Allow from all #允許所有使用者通路
Deny from 172.25.254.2 #拒絕172.25.254.2通路
</Directory>
測試:172.25.254.2主機通路172.25.254.1
<a href="http://s1.51cto.com/wyfs02/M01/87/17/wKioL1fT-fzR6INeAABn5-Stp1g579.png" target="_blank"></a>
6.配置基于使用者的身份驗證(帳号密碼通路網頁)
Apache無格式檔案使用者身份驗證
在此配置中,使用者賬戶和密碼存儲在本地.htpasswd檔案中。處于安全原因,該檔案不能
儲存在網站的DocumentRoot中,而應儲存在Web伺服器不提供服務的一些目錄中。特殊
的htpasswd指令用于在.htpasswd檔案中管理使用者。
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf #還原端口與目錄配置
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# mkdir admin
[root@server1 html]# cd admin/
[root@server1 admin]# vim index.html
hello,boy.
[root@server1 admin]# cd /etc/httpd/conf
[root@server1 conf]# ls
httpd.conf magic
[root@server1 conf]# htpasswd -cm htpasswd admin #用兩個賬戶建立Apache密碼檔案
New password:
Re-type new password:
Adding password for user admin
[root@server1 conf]# htpasswd -m htpasswd willis
htpasswd: password verification error
[root@server1 conf]# htpasswd -cm htpasswd willis
Adding password for user willis
htpasswd httpd.conf magic
[root@server1 conf]# cat htpasswd
admin:$apr1$rEBIJilB$qGzEG6c4NYvOxg2qZLSfk/
willis:$apr1$qFoQCa7F$5IZqbqG5d5hVclspCl6R/0
[root@server1 conf]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html"
<Directory "/var/www/html/admin">
AuthUserfile /etc/httpd/conf/htpasswd
AuthName "Please input your user name and password "
AuthType basic
Require valid-user
# Require user admin
[root@server1 conf]# systemctl restart httpd.service
測試:
<a href="http://s2.51cto.com/wyfs02/M01/87/18/wKioL1fUDsbjj6xgAACayFuSjpc369.png" target="_blank"></a>
<a href="http://s2.51cto.com/wyfs02/M02/87/1A/wKiom1fUDsah3kXcAAAkDZrLPwU470.png" target="_blank"></a>
7.更改預設通路頁面
[root@server1 html]# ls
admin index.html
[root@server1 html]# vim test
This is a test page.
[root@server1 html]# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
# DirectoryIndex index.html
DirectoryIndex test index.html #先通路test,
</IfModule>
[root@server1 html]# systemctl restart httpd.service
<a href="http://s4.51cto.com/wyfs02/M00/87/18/wKioL1fUECOh_h1RAAAZ0pLWSLw291.png" target="_blank"></a>
8.可通路php,cgi等網頁
1) 通路 .php
[root@server1 html]# yum install php -y ##安裝php軟體包,其中包含mod_php子產品:
[root@server1 html]# pwd
/var/www/html
[root@server1 html]# vim index.php #寫php測試頁
<?php
phpinfo();
?>
[root@server1 html]# systemctl restart httpd.service
<a href="http://s5.51cto.com/wyfs02/M02/87/18/wKioL1fUEpORClbSAADt35ypsCQ584.png" target="_blank"></a>
2)可通路 .cgi
通用網關接口(CGI)是網站上放置動态内容的最簡單的方法。CGI腳本可用于許多目的,但是謹慎控制使用哪個CGI腳本以及允許誰添加和運作這些腳本十分重要。編寫品質差的CGI腳本可能為外部攻擊者提供了破壞網站及其内容安全性的途徑。是以,在Web伺服器級别和SELinux政策級别,都存在用于限制CGI腳本使用的設定。
[root@server1 html]# mkdir scripts
[root@server1 html]# cd scripts/
[root@server1 scripts]# vim index.cgi #測試内容,可從文檔中拷貝
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@server1 scripts]# chmod +x index.cgi #權限
[root@server1 scripts]# setenforce 0 #安全上下文
或者
semanage fcontext -l | grep httpd
/var/www/perl(/.*)? all files system_u:object_r:httpd_sys_script_exec_t:s0
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/scripts(/.*)?'
restorecon -FvvR /var/www/html/scripts
[root@server1 scripts]# vim /etc/httpd/conf/httpd.conf #配置内容,可從文檔中拷貝
DocumentRoot "/var/www/html"
<Directory "/var/www/html/scripts">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
[root@server1 scripts]# systemctl restart httpd.service
測試:http://172.25.254.1/scripts/index.cgi
<a href="http://s5.51cto.com/wyfs02/M02/87/18/wKioL1fUFlejom9pAAAsW4rXzpI629.png" target="_blank"></a>
<a href="http://s5.51cto.com/wyfs02/M02/87/1A/wKiom1fUFlfQ7JKQAAAsaZtr8sU566.png" target="_blank"></a>
9.虛拟主機
虛拟主機允許您從一個httpd伺服器同時為多個網站提供服務。在本節中,我們将了解基于名稱的虛拟主機其中多個主機名都指向同一個IP位址,但是Web伺服器根據用于到達站點的主機名提供具有不同内容的不同網站。
[root@server1 scripts]# vim /etc/hosts #浏覽器通路位址的主機修改DNS配置檔案
172.25.254.1 www.qq.com
172.25.254.1 news.qq.com
172.25.254.1 sport.qq.com
[root@server1 scripts]# cd /var/www/
[root@server1 www]# mkdir virtual/news/html -p
[root@server1 www]# mkdir virtual/sport/html -p
[root@server1 www]# echo new\'s page > virtual/news/html/index.html
[root@server1 www]# echo sport\'s page > virtual/sport/html/index.html
[root@server1 www]# vim /etc/httpd/conf.d/default.conf
<Virtualhost _default_:80> #定義預設虛拟主機的塊
Documentroot "/var/www/html" #在<VirtualHost>塊内部,指定從中提供内容的目錄。
Customlog "logs/default.log" combined
</Virtualhost>
[root@server1 www]# vim /etc/httpd/conf.d/news.conf
<Virtualhost *:80> #定義虛拟主機的塊
Servername news.qq.com #指定伺服器名稱。在使用基于名稱的虛拟主機的情況下,此處的名稱必須與用戶端請求完全的比對。。
Documentroot "/var/www/virtual/news/html" #指定從中提供内容的目錄。
Customlog "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news/html">
Require all granted #授權
</Directory>
[root@server1 www]# vim /etc/httpd/conf.d/sport.conf
<Virtualhost *:80>
Servername sport.qq.com
Documentroot "/var/www/virtual/sport/html"
Customlog "logs/sport.log" combined
<Directory "/var/www/virtual/sport/html">
Require all granted #授權
測試通路:
<a href="http://s4.51cto.com/wyfs02/M00/87/1B/wKiom1fUGoPzx3NEAAAdsEJgoYk287.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M02/87/1B/wKiom1fUGoTx7xbUAAAbRRbLl4Y626.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M02/87/18/wKioL1fUGoSiA2pUAAAZfNu9SF0178.png" target="_blank"></a>
10.HTTPS自定義自簽名證書
如果加密的通信非常重要,而經過驗證的身份不重要,管理者可以通過生成self-signed certificate來避免與認證機構進行互動所帶來的複雜性。
使用genkey實用程式(通過crypto-utils軟體包分發),生成自簽名證書及其關聯的私鑰。為了簡化起見,genkey将在“正确”的位置(/etc/pki/tls目錄)建立證書及其關聯的密鑰。相應地,必須以授權使用者(root)身份運作該實用程式。
[root@server1 www]# yum install crypto-utils mod_ssl -y #生成自簽名證書crypto-utils軟體包
[root@server1 www]# genkey Apache.example.com #調用genkey,同時為生成的檔案指定唯一名稱
1)記錄生成的證書(Apach.example.com .crt)和關聯的私鑰(Apach.example.com .key)的位置
2) 繼續使用對話框,并選擇合适的密鑰大小。(預設的2048位密鑰為推薦值)
3) 在生成随機數時比較慢,敲鍵盤和移動滑鼠可以加速
4) 拒絕向認證機構(CA)發送證書請求(CSR)
5) 拒絕加密私鑰
6) 為伺服器提供合适的身份。Common Name必須與伺服器的主機全名完全比對。
<a href="http://s3.51cto.com/wyfs02/M00/87/1B/wKiom1fUHNLBgPzsAAAU-wsX_yM830.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M02/87/19/wKioL1fUHNLg1c_QAAA7i7RRpQ0050.png" target="_blank"></a>
<a href="http://s2.51cto.com/wyfs02/M02/87/19/wKioL1fUHNCAnlh4AAAQWh_qutQ743.png" target="_blank"></a>
[root@server1 www]# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/Apache.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/Apache.example.com.key
[root@server1 www]# systemctl restart httpd.service
如要進行确認,請使用https協定(https://serverX.example.com)通過Web用戶端(如Firefox)通路Web伺服器。
Web用戶端可能會發出它不認可證書發行者的警告。這種情況适用自簽名證書。要求Web用戶端繞過證書認證。(對于Firefox,請選擇“I Understand the Risks” [我了解風險]、“Add Exception” [添加例外]和“Confirm Security Exception”[确認安全例外]。)
測試:
<a href="http://s2.51cto.com/wyfs02/M00/87/19/wKioL1fUIEfDWhxJAACEUy7XAes613.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M01/87/1B/wKiom1fUIEaRteTUAADGOyXAgOs793.png" target="_blank"></a>
<a href="http://s5.51cto.com/wyfs02/M02/87/1B/wKiom1fUIEXBv0bJAAARTvqz37Q605.png" target="_blank"></a>
11.網頁重寫
[root@server1 www]# vim /etc/hosts #浏覽器通路位址的主機修改DNS配置檔案
172.25.254.1 login.qq.com
[root@server1 www]# pwd
/var/www
[root@server1 www]# mkdir virtual/login/html -p
[root@server1 www]# echo login\'s page > virtual/login/html/index.html
[root@server1 www]# vim /etc/httpd/conf.d/login.conf
<Virtualhost *:443>
Servername login.qq.com
Documentroot "/var/www/virtual/login/html"
Customlog "logs/login.log" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/Apach.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/Apach.example.com.key
<Directory "/var/www/virtual/login/html">
<Virtualhost *:80>
ServerName login.qq.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
測試:
<a href="http://s1.51cto.com/wyfs02/M01/87/19/wKioL1fUJhCRw6pCAAAU8p4w4o0114.png" target="_blank"></a>
本文轉自willis_sun 51CTO部落格,原文連結:http://blog.51cto.com/willis/1851477,如需轉載請自行聯系原作者