1 Apache 簡介
Apache HTTP Server(通常簡稱 “Apache”)是世界上最流行、歷史最悠久的開源 Web 服務器軟件之一,由 Apache 軟件基金會(Apache Software Foundation)維護。它的核心功能是接收客戶端(如瀏覽器)的 HTTP/HTTPS 請求,處理并返回對應的網頁、文件或數據,是構建 Web 應用、網站的核心基礎設施之一。
Apache 是一款成熟、穩定、可擴展的開源 Web 服務器,憑借模塊化架構和豐富的生態,成為中小企業、動態 Web 應用(尤其是 PHP 場景)的首選。盡管在高并發靜態資源場景下,Nginx 逐漸成為主流,但 Apache 憑借數十年的技術積累和穩定性,仍在大量生產環境中發揮核心作用。
2 Apache 安裝
2.1 Apache 基本信息
# apache的基本信息/etc/httpd/conf # apache的配置目錄
/etc/http/conf.d # 子配置目錄
/etc/httpd/conf/httpd.conf # 主配置文件
/lib/systemd/system/htpd.service # 啟動文件
:80 # 默認端口
/var/www/html # 默認發布目錄
index.html # 默認發布文件
2.2 安裝 Apache
1.下載
# yum install httpd -y
3.查看防火墻狀態
# systemctl status firewalld.service
3.在火墻中放行web服務
# firewall-cmd --permanent --add-service=https
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# firewall-cmd --list-services
4.開啟httpd服務
# systemctl enable --now httpd
5.生成默認測試頁文件,并測試效果
# echo 192.168.36.100 > /var/www/html/index.html
# cat /var/www/html/index.html
# curl 192.168.36.100
3 Apache的基本配置信息
3.1 修改默認端口
1.修改端口
# 修改配置文件
# vim /etc/httpd/conf/httpd.conf
48 Listen 8080# 刷新服務
# systemctl reload httpd.service# 防火墻放行端口
# firewall-cmd --permanent --add-port=8080/tcp
# firewall-cmd --reload
# firewall-cmd --list-ports
2.檢查端口
# ss -antlupe | grep httpd
3.訪問服務
# curl 192.168.36.100:8080
3.2 修改默認發布目錄
# 創建默認發布目錄
# mkdir /web/html -p# 修改主配置文件,并進行授權
# vim /etc/httpd/conf/httpd.conf
125 DocumentRoot "/web/html"
126 <Directory "/web/html">
127 Require all granted
128 </Directory># 重啟httpd服務
# systemctl restart httpd
# 寫入測試文件
# echo "默認目錄" > /web/html/index.html
# curl 192.168.36.100:8080
3.3 添加默認發布文件
# 建立新的默認發布文件
# echo "默認發布文件" > /web/html/lee.html# 當沒有對配置進行修改時新默認發布文件不會被默認訪問
# curl 192.168.36.100:8080
默認目錄
# curl 192.168.36.100:8080/lee.html
默認發布文件# 修改默認發布文件
# vim /etc/httpd/conf/httpd.conf
173 <IfModule dir_module>
174 DirectoryIndex lee.html index.html
175 </IfModule>
# systemctl reload httpd.service
# curl 192.168.36.100:8080
默認發布文件
4 https
4.1 安裝mod_ssl模塊
# yum install mod_ssl -y
4.2 建立證書
# 創建證書文件
# mkdir /etc/httpd/certs -p
# 建立證書
# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/certs/ceshi.key -x509 -days 365 -out /etc/httpd/certs/ceshi.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) []:shanxi
Locality Name (eg, city) [Default City]:XIAN
Organization Name (eg, company) [Default Company Ltd]:CG
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.cg.org
Email Address []:cg@cg.org
# 查看證書
# ls /etc/httpd/certs/
ceshi.crt ceshi.key
4.3 將生成的證書文件添加至子配置文件
# vim /etc/httpd/conf.d/ssl.conf
86 SSLCertificateFile /etc/httpd/certs/ceshi.crt
104 SSLCertificateChainFile /etc/httpd/certs/ceshi.key
# 重啟服務
# systemctl reload httpd.service
# 查看https狀態
# netstat -antlupe | grep httpd
在瀏覽器訪問https://IP:端口
5 Apache的虛擬主機
Apache 的虛擬主機(Virtual Host)是一項核心功能,允許在單臺服務器上通過同一個 Apache 服務托管多個網站(域名),每個網站可以有獨立的目錄、配置和域名,實現 “一臺服務器對應多個域名” 的效果。
虛擬主機的核心原理是:Apache 接收請求時,根據客戶端請求的?域名(Host 頭)?或?IP 地址,匹配對應的虛擬主機配置,返回該網站的內容。
# 為每個發布站點建立默認發布目錄
# mkdir -p /var/www/virtual/ceshi.org/news
# mkdir -p /var/www/virtual/ceshi.org/bbs# 為每個站點建立默認發布文件
# echo new.chshi.org > /var/www/virtual/ceshi.org/news/index.html
# echo bbs.chshi.org > /var/www/virtual/ceshi.org/bbs/index.html# 創建子配置文件
# vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost _default_:80>DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>ServerName bbs.ceshi.orgDocumentRoot /var/www/virtual/ceshi.org/bbs/
</VirtualHost>
<VirtualHost *:80>ServerName news.ceshi.orgDocumentRoot /var/www/virtual/ceshi.org/news/
</VirtualHost># 刷新服務
# systemctl reload httpd.service
# 測試效果
# 1.編寫本地解析
# vim /etc/hosts
192.168.36.100 Motherboard news.ceshi.org bbs.ceshi.org
# 2.測試效果
# 恢復實驗環境
# vim /etc/httpd/conf/httpd.conf
47 Listen 80
124 DocumentRoot "/var/www/html"
125 #DocumentRoot "/web/html"
126 #<Directory "/web/html">
127 # Require all granted
128 #</Directory>
173 <IfModule dir_module>
174 DirectoryIndex index.html
175 </IfModule>
# systemctl reload httpd.service[root@Motherboard ~]# curl Motherboard
192.168.36.100
[root@Motherboard ~]# curl bbs.ceshi.org
bbs.chshi.org
[root@Motherboard ~]# curl news.ceshi.org
new.chshi.org
到此Apache的簡單應用結束!