一、基本概念
Apache(或httpd)是Internet上使用最多的Web服務器技術之一,使用的傳輸協議是http超文本傳輸協議(一個基於超文本的協議),用於通過網絡連接來發送和接受對象。
有兩個版本:
- http:超文本傳輸協議,通過線路以明文形式發送,默認情況下使用80/TCP(也可以使用其他端口)
- https:經TLS/SSL安全加密的超文本傳輸協議,默認情況下使用端口443/TCP
二、了解Apache的配置文件
1、配置文件的分類
在Linux系統中配置服務,其實就是修改服務的配置文件,httpd服務程序的主要配置文件及存放位置如下:
配置文件的名稱 | 存放位置 |
---|---|
服務目錄 | /etc/httpd |
主配置文件 | /etc/httpd/conf/httpd.conf |
虛擬主機配置文件 | /etc/httpd/conf.d |
日志文件 | /etc/httpd/logs |
網站數據目錄 | /var/www/html |
2、主配置文件的重要參數
主配置文件/etc/httpd/conf/httpd.conf
參數 | 用途 |
---|---|
ServerRoot | 服務目錄 |
ServerAdmin | 管理員郵箱 |
User | 運行服務的用戶 |
Group | 運行服務的用戶組 |
ServerName | 網站服務器的域名 |
DocumentRoot | 文檔根目錄(網站數據目錄) |
Directory | 網站數據目錄的權限 |
Listen | 監聽的IP地址與端口號 |
DirectoryIndex | 默認的索引頁頁面 |
ErrorLog | 錯誤日志文件 |
CustomLog | 訪問日志文件 |
Timeout | 網頁超時時間,默認為300秒 |
3、Directory標簽
<Directory "/var/www/html"> AllowOverride None #設置.htaccess文件中的指令類型,None表示禁止使用.htaccess,該參數一般不改 Require all granted #設置權限,默認開啟所有客戶機訪問權限 </Directory>
三、如何配置Apache服務器
首先准備:主機名、網絡、yum源
1、更改主機名:
[root@localhost ~]# hostnamectl set-hostname $主機名 [root@localhost ~]# bash #環境變量重載
2、配置網絡
(1)虛擬交換機、網絡適配器選擇僅主機模式,並且配置為192.168.100.0網段;
(2)編輯網絡配置文件:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 修改: BOOTPROTO=static #改為靜態IP地址 ONBOOT=yes #改為開機自啟 IPADDR=192.168.100.10 PREFIX=24 或者 NETMASK=255.255.255.0
(3)重啟網絡服務:
[root@localhost ~]# systemctl restart network
3、配置yum源
(1)先在VMware里面把系統鏡像文件連接到虛擬機的光驅上;
(2)掛載光驅里的鏡像:
[root@localhost ~]# mount /dev/cdrom /media
(3)修改yum源配置文件:
[root@localhost ~]# vim /etc/yum.repos.d/local.repo [rhel] name=rhel baseurl=file:///media enabled=1 gpgcheck=0
(4)清空yum源緩存信息:
[root@localhost ~]# yum clean all
(5)檢索當前yum源信息:
[root@localhost ~]# yum repolist
任務一:配置簡單的httpd服務
1、安裝httpd服務
[root@server ~]# yum -y install httpd
2、啟動httpd服務
[root@server ~]# systemctl restart httpd [root@server ~]# systemctl enable httpd
3、配置防火牆
[root@server ~]# firewall-cmd --permanent --add-service=http [root@server ~]# firewall-cmd --reload
4、關閉SELinux
[root@server ~]# setenforce 0
5、客戶端測試
[root@client ~]# firefox http://IP地址 或者 curl http://IP地址
任務二:配置基於用戶的個人網站
注意:該用戶必須在Linux系統中存在
1、新建一個用戶(網站基於該用戶)
[root@server ~]# useradd user0 [root@server ~]# passwd user0
2、修改用戶的家目錄權限,使其他用戶具有讀取和執行的權限
[root@server ~]# chmod -R 705 /home/user0
3、創建存放用戶個人主頁空間的目錄,寫user0的網頁文件
[root@server ~]# mkdir /home/user0/public_html [root@server ~]# cd /home/user0/public_html [root@server ~]# echo "this is user0's web">>index.html
4、修改基於用戶的httpd配置文件
[root@server ~]# vim /etc/httpd/conf.d/userdir.conf 修改: UserDir enabled #開啟,表示讓httpd服務程序開啟個人用戶主頁功能 UserDir public_html #去注釋,UserDir參數表示網站數據在用戶家目錄中的保存目錄名稱
5、配置防火牆(同上)
[root@server ~]# firewall-cmd --permanent --add-service=http [root@server ~]# firewall-cmd --reload
6、修改selinux權限
[root@server ~]# getsebool -a|grep home [root@server ~]# setsebool httpd_enable_homedirs on
7、重啟服務
[root@server ~]# systemctl restart httpd
8、客戶端測試
[root@client ~]# firefox http://IP地址/~username 或者curl http://IP地址/~username
任務三:配置基於域名訪問的虛擬主機
1、新建虛擬主機的網頁文件
[root@server ~]# mkdir /www/one /www/two [root@server ~]# cd /www/one [root@server ~]# echo "this is a web for virtual host one">>index.html [root@server ~]# cd /www/two [root@server ~]# echo "this is a web for virtual host two">>index.html [root@server ~]# chmod o+x /www
2、配置虛擬主機的文件
[root@server ~]# cd /etc/httpd/conf.d [root@server ~]# vim vhost.conf <Directory /www/one> #設置網站目錄權限 Require all granted #開啟所有客戶機訪問權限 </Directory> <VirtualHost 192.168.100.10> #虛擬主機 ServerName one.example.com #定義服務器名稱 DocumentRoot /www/one/ #網站數據目錄 </VirtualHost> <Directory /www/two> Require all granted </Directory> <VirtualHost 192.168.100.11> ServerName two.example.com DocumentRoot /www/two/ </VirtualHost>
3、做域名解析文件
server/client
[root@server ~]# vim /etc/hosts 192.168.100.10 one.example.com 192.168.100.11 two.example.com
4、配置防火牆(同上)
[root@server ~]# firewall-cmd --permanent --add-service=http [root@server ~]# firewall-cmd --reload
5、修改虛擬主機網頁文件的selinux上下文類型
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?' [root@server ~]# restorecon -RFv /www
6、重啟服務
[root@server ~]# systemctl restart httpd
7、使用瀏覽器訪問
http://one.example.com
http://two.example.com
任務四:配置基於端口訪問的虛擬主機
1——新建虛擬主機的網頁文件
[root@server ~]# mkdir /www/8088 [root@server ~]# echo "this is a web for port 8088 ">>index.html [root@server ~]# mkdir /www/8089 [root@server ~]# echo "this is a web for port 8089 ">>index.html
2——配置虛擬主機的文件
[root@server ~]# cd /etc/httpd/conf.d [root@server ~]# vim vhost.conf <Directory /www/8088/> Require all granted </Directory> <virtualHost 192.168.100.10:8088> DocumentRoot /www/8088/ </virtualHost> <Directory /www/8089/> Require all granted </Directory> <virtualHost 192.168.100.10:8089> DocumentRoot /www/8089/ </virtualHost>
3、配置防火牆
[root@server ~]# firewall-cmd --permanent --zone=public --add-port=8089/tcp [root@server ~]# firewall-cmd --permanent --zone=public --add-port=8088/tcp [root@server ~]# firewall-cmd --reload
4、關閉SELinux
[root@server ~]# setenforce 0
5、重啟服務
[root@server ~]# systemctl restart httpd
6、使用瀏覽器訪問
http://192.168.100.10:8088
http://192.168.100.10:8089
任務五:配置基於TLS加密的虛擬主機
注意:經TLS/SSL安全加密的超文本傳輸協議,默認情況下使用端口443/TCP
1、安裝TLS加密軟件,網站內容不用明文傳輸
[root@server ~]# yum -y install mod_ssl
2、生成密鑰
[root@server ~]# openssl genrsa >tlsweb.key
3、生成證書請求文件
[root@server ~]# openssl req -new -key tlsweb.key > tlsweb.csr
4、生成證書文件
[root@server ~]# openssl req -x509 -days 365 -key tlsweb.key -in tlsweb.csr >tlsweb.crt
5、修改ssl.conf配置文件
[root@server ~]# vim /etc/httpd/conf.d/ssl.conf SSLCertificateFile /etc/pki/tls/certs/tlsweb.crt SSLCertificateKeyFile /etc/pki/tls/private/tlsweb.key
6、把證書文件拷貝到ssl.conf配置文件里的對應路徑下面
[root@server ~]# cp tlsweb.crt /etc/pki/tls/certs/
7、把秘鑰文件拷貝到ssl.conf配置文件里的對應路徑下面
[root@server ~]# cp tlsweb.key /etc/pki/tls/private/
8、使用瀏覽器訪問
https://192.168.100.10