第三課unit9 Apache
1.安裝
yum install httpd -y
systemctl start httpd
systemctl enable httpd
systemctl stop firewalld
systemctl disable firewalld
2.Apache信息
index.html ##默認發布文件
/etc/httpd/conf/httpd.conf ##默認配置文件
/etc/httpd/conf/*.conf
/var/www/html ##默認發布目錄
80 ##Apache默認端口
3.基本配置
(1)vim /etc/httpd/conf/httpd.conf ##修改默認發布文件
4.修改默認發布目錄
**selinux狀態為disabled
vim /etc/httpd/conf/httpd.conf ##修改默認發布目錄
**selinux狀態為enforcing
vim /etc/httpd/conf/httpd.conf ##修改默認發布目錄
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?‘ ##配置安全上下文
restorecon -RvvF /westos/
systemctl restart httpd.service ##重啟服務
mkdir /westos/www/tset -p ##新建默認發布目錄
vim /westos/www/tset/westos.html ##新建默認發布文件
**測試
5.控制訪問
vim /etc/httpd/conf/httpd.conf
**允許除了74主機的任何主機訪問
測試
74主機
其他主機
**只允許74主機訪問
測試
74主機
其他主機
設定用戶訪問
htpasswd -cm /etc/httpd/accessuser admin ##設置用戶admin和密碼
vim /etc/httpd/conf/httpd.conf ##修改配置文件
<Directory "/var/www/html/admin">
AuthUserFile /etc/httpd/accessuser ##用戶認證文件
AuthName "input name and password" ##用戶認證提示信息
Authtype basic ##認證類型
Require valid-user ##用戶認證,認證文件裏所有用戶都可以訪問
Require user admin ##只允許admin用戶
</Directory>
測試
6.Apache語言支持
*HTML語言默認支持
*PHP語言
yum install php -y ##安裝PHP服務
vim /var/www/html/index.php ##寫PHP測試
測試
*CGI語言
mkdir /var/www/html/cgi ##創建默認發布目錄
vim index.cgi ##編寫默認發布文件
內容
chmod +x index.cgi ##給文件可執行權限
vim /etc/httpd/conf/httpd.conf
systemctl restart httpd ##重啟服務
selinux 設置為disabled
測試
7.Apache虛擬主機
*可以讓一臺Apache服務器在訪問不同域名的時候顯示不同主頁
*建立測試頁
*配置
(1)vim /etc/httpd/conf.d/default.conf ##未指定的域名訪問default
**內容
*<Virtualhost _default_:80> ##虛擬主機開啟80端口
DocumentRoot "/var/www/html" ##虛擬主機默認發布目錄
CustomLog "logs/default.log" combined ##虛擬主機日誌
</Virtualhost>
(2)vim /etc/httpd/conf.d/new.conf ##指定域名new.westos.com訪問指定默認目錄
vim /etc/httpd/conf.d/money.conf ##指定域名money.westos.com訪問指定默認目錄
**內容
* <Virtualhost *:80>
ServerName "new.westos.com"
DocumentRoot "/var/www/virtual/new.westos.com/html"
CustomLog "logs/new.log" combined
</Virtualhost>
<Directory "/var/www/virtual/new.westos.com/html"> ##默認發布目錄訪問
Require all granted
</Directory>
測試
vim /etc/hosts ##配置解析
8.HTTPS
(1)Hyper Text Transfer Protocol over Secure Socket Layer HTTP下加入SSL層
(2)配置
yum install mod_ssl crypto-utils -y ##安裝服務
genkey www.westos.com ##生成密鑰和密匙
*生成證書位置
**選擇密鑰大小
**生成隨機數
、
**拒絕向ca發送CSR
**拒絕加密私鑰
**為服務器提供合適身份
(3)vim /etc/httpd/conf.d/login.conf
<Virtualhost *:443>
ServerName "login.westos.com"
DocumentRoot "/var/www/virtual/login.westos.com/html" ##虛擬主機默認發布目錄
CustomLog "logs/login.log" combined
SSLEngine on ##開啟HTTPS功能
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt ##證書
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##密鑰
</Virtualhost>
<Directory "/var/www/virtual/login.westos.com/html"> ##默認發布目錄
Require all granted
</Directory>
<Virtualhost *:80> ##網頁重寫實現自動訪問HTTPS
ServerName "login.westos.com"
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
* ^(/.*)$ 客戶主機在地址欄中寫入所有字符 測試中的login.westos.com
* https:// 定向成為訪問協議
*%{HTTP_HOST} 客戶請求主機
*$1 表示^(/.*)$
*[redirect=301] 臨時重定向 302永久重定向
測試
mkdir /var/www/virtual/login.westos.com/html -p ##新建測試目錄
vim /var/www/virtual/login.westos.com/html/index.html
vim /etc/hosts ##配置解析
第三課unit9 Apache