apache(web服務器)
Apache是世界使用排名第一的Web服務器軟件。它可以運行在幾乎所有廣泛使用的計算機平臺上,由於其跨平臺和安全性被廣泛使用,是最流行的Web服務器端軟件之一。
一、實驗環境:
RHEL7.0 172.25.254.10 server1.example.com firewalld disabled
二、實驗內容:
1.apache的安裝:
yum install -t httpd httpd-manual
systemctl start httpd 啓動apache服務
systemctl enable httpd 自啓動
查看端口:
2.apache的主配置文件:
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.更改apache默認訪問目錄:
更改安全上下文,或者把selinux設爲disabled都可以。
[root@server1 yum.repos.d]# getenforce
Enforcing
[root@server1 yum.repos.d]# mkdir -p /www/html
[root@server1 yum.repos.d]# ls -ldZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@server1 yum.repos.d]# ls -ldZ /www/html/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/
[root@server1 yum.repos.d]# semanage fcontext -a -t httpd_sys_content_t '/www/html(/.*)?'
[root@server1 yum.repos.d]# 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
[root@server1 yum.repos.d]# ls -ldZ /www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
[root@server1 yum.repos.d]# systemctl restart httpd
修改主配置文件:將默認訪問目錄改成自己想要的訪問目錄
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/www/html"
<Directory "/">
AllowOverride None
Require all granted
</Directory>
echo hello > /www/html/index.html 給默認發佈目錄寫個首頁
systemctl restart httpd 重啓服務
測試:
4.更改默認端口:
查看可更改的端口: ##selinux標籤
修改主配置文件更改端口:
vim /etc/httpd/conf/httpd.conf
Listen 80 改成 8080
systemctl restart httpd.service
測試:
5.訪問目錄權限:
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/www/html"
<Directory "/">
AllowOverride None
Require all granted
order allow,deny ##讀取按順序,先讀取前面的
Allow from all ##允許所有用戶訪問
Deny from 172.25.254.20 ##禁止172.25.254.20訪問
</Directory>
systemctl restart httpd.service
測試:
用172.25.254.20主機進行測試:訪問被拒絕
用其他主機測試,可以測試成功。
6.配置基於用戶的身份驗證:
Apache無格式文件用戶身份驗證
在此配置中,用戶賬戶和密碼存儲在本地.htpasswd文件中。處於安全原因,該文件不能保存在網站的DocumentRoot中,而應保存在Web服務器不提供服務的一些目錄中。特殊的htpasswd命令用在.htpasswd文件中管理用戶。
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
my name is xiaoze.
[root@server1 admin]# cd /etc/httpd/conf
[root@server1 conf]# ls
httpd.conf magic
[root@server1 conf]# htpasswd -cm htpasswd admin 用兩個賬戶創建Apache密碼文件,(第一次創建密碼文件是需要有“c“參數,m表示使用md5加密)
New password:
Re-type new password:
Updating password for user admin
[root@server1 conf]# htpasswd -m htpasswd xiaoze
New password:
Re-type new password:
Adding password for user xiaoze
[root@server1 conf]# cat htpasswd
admin:$apr1$NmEXjM8K$H9QaQ4IBXsbjjrgqsvoI6/
xiaoze:$apr1$oiBEyWhW$CEe9Js1Gh5pu1TQ3/J.l5.
[root@server1 conf]# ls
htpasswd httpd.conf magic
[root@server1 conf]# vim /etc/httpd/conf/httpd.conf
[root@server1 conf]# systemctl restart httpd.service
測試:
7.更改默認訪問頁面:
vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
# DirectoryIndex index.html
DirectoryIndex test index.html
</IfModule>
測試:
8.php語言支持,可訪問cgi等網頁:
php語言支持:
yum install -y php (安裝php軟件包,其中包含mod_php模塊)
[root@server1 html]# cat index.php 寫php的測試網頁
<?php
phpinfo();
?>
systemctl restart httpd.service
可訪問CGI:
通用網關接口(CGI)是網站上放置動態內容的最簡單的方法。CGI腳本可用於許多目的,但是謹慎控制使用哪個CGI腳本以及允許誰添加和運行這些腳本十分重要。編寫質量差的CGI腳本可能爲外部***者提供了破壞網站及其內容安全性的途徑。因此,在Web服務器級別和SELinux策略級別,都存在用於限制CGI腳本使用的設置。
cd /var/www/html
mkdir scripts
vim index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
chmod +x index.cgi
setenforce 0 設置selinux權限爲Permissive
或者以下方法也可以:
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
vim /etc/httpd/conf/httpd.conf 編寫cgi配置模塊
DocumentRoot "/var/www/html"
<Directory "/var/www/html/scripts">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
systemctl restart httpd.service
測試:
9.虛擬主機:
虛擬主機允許您從一個httpd服務器同時爲多個網站提供服務。在本節中,我們將瞭解基於名稱的虛
擬主機其中多個主機名都指向同一個IP地址,但是Web服務器根據用於到達站點的主機名提供具有不
同內容的不同網站。
vim /etc/hosts 將虛擬主機地址做解析
172.25.254.10 news.qq.com
172.25.254.10 sport.qq.com
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 conf.d]# vim /etc/httpd/conf.d/news.conf
<Virtualhost *:80> #定義虛擬主機的塊
Servername news.qq.com #指定服務器名稱。在使用基於名稱的虛擬主機的情況下,此處的名稱必須與客戶端請求完全的匹配。
DocumentRoot "/var/www/virtual/news/html" #指定從中提供內容的目錄。
Customlog "logs/neww.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news/html">
Require all granted #授權
</Directory>
[root@server1 conf.d]# vim /etc/httpd/conf.d/sport.conf
<Virtualhost *:80>
Servername sport.qq.com
Documentroot "/var/www/virtual/sport/html"
Customlog "logs/sport.log" combined
</Virtualhost>
<Directory "/var/www/virtual/sport/html">
Require all granted
</Directory>
systemctl restart httpd.service
測試:
10.HTTPS自定義簽名證書:
如果加密的通信非常重要,而經過驗證的身份不重要,管理員可以通過生成self-signed certificate來避免與認證機構進行交互所帶來的複雜性。使用genkey實用程序(通過crypto-utils軟件包分發),生成自簽名證書及其關聯的私鑰。爲了簡化起見,genkey將在“正確”的位置(/etc/pki/tls目錄)創建證書及其關聯的密鑰。相應地,必須以授權用戶(root)身份運行該實用程序。
生成自簽名證書:
確保已安裝crypto-utils軟件包。
[root@server1 ~]# yum install crypto-utils mod_ssl
調用genkey,同時爲生成的文件指定唯一名稱(例如,服務器的主機全名)。
--days可以指定證書有效期
[root@server1 ~]# genkey server1.example.com
1)記錄生成的證書(Apach.example.com .crt)和關聯的私鑰(Apach.example.com .key)的位置
2) 繼續使用對話框,並選擇合適的密鑰大小。(默認的2048位密鑰爲推薦值)
3) 在生成隨機數時比較慢,敲鍵盤和移動鼠標可以加速
4) 拒絕向認證機構(CA)發送證書請求(CSR)
5) 拒絕加密私鑰
6) 爲服務器提供合適的身份。Common Name必須與服務器的主機全名完全匹配。
vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/server1.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/server1.example.com.key
systemctl restart httpd.service
測試:
如要進行確認,請使用https協議(https://serverX.example.com)通過Web客戶端(如Firefox)訪問Web服務器。
Web客戶端可能會發出它不認可證書發行者的警告。這種情況適用自簽名證書。要求Web客戶端繞過證書認證。(對於Firefox,請選擇“I Understand the Risks” [我瞭解風險]、“Add Exception” [添加例外]和“Confirm Security Exception”[確認安全例外]。)
11.網頁重寫:
vim /etc/hosts 添加解析
172.25.254.10 login.qq.com
cd /var/www
mkdir virtual/login/html -p
echo login\'s page > virtual/login/html/index.html
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/server1.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/server1.example.com.key
</Virtualhost>
<Directory "/var/www/virtual/login/html">
Require all granted
</Directory>
<Virtualhost *:80>
ServerName login.qq.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
systemctl restart httpd.service
測試: