1.Apache是什么?
Apache 是世界上最流行的 ??開源Web服務器軟件??,由 Apache 軟件基金會維護。
??主要功能??:接收客戶端(如瀏覽器)的HTTP請求,返回網頁、圖片等靜態/動態資源。
??特點??:
跨平臺(Linux、Windows、macOS)
模塊化設計(按需加載功能)
支持多語言擴展(PHP、Python等)
高穩定性和安全性
2.核心架構與工作原理??
多進程模型(MPM)??
??Prefork??:多進程模式,每個請求由一個獨立子進程處理(適合穩定性要求高的場景)。
??Worker??:多進程+多線程混合模式(平衡性能與資源消耗)。
??Event??:基于事件驅動,高效處理高并發連接(現代推薦模式)。
3.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 #默認發布文件
#安裝apache
[root@apache ~]# dnf install httpd -y
#在火墻中放行web服務
[root@apache ~]# firewall-cmd --permanent --add-service=http
success
[root@apache ~]# firewall-cmd --permanent --add-service=https
success
#開啟服務
[root@apache ~]# systemctl enable --now httpd
#生成默認測試頁文件
[root@apache ~]# echo 172.25.254.100 > /var/www/html/index.html #nginx的發布地為/usr/share/nginx/html/index.html
#測試:
[root@apache ~]# curl 172.25.254.100
172.25.254.100
4.Apache的基本配置信息
?4.1.端口修改
#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.conf
47 Listen 8080
#刷新服務
[root@apache ~]# systemctl reload httpd
#設定火墻通過
[root@apache ~]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@apache ~]# firewall-cmd --reload
#檢測
[root@apache ~]# netstat -antlupe | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 0
78081 32315/httpd
#訪問:
[root@apache ~]# curl 172.25.254.100:8080
172.25.254.100
2.默認發布目錄
#建立默認發布目錄
[root@apache ~]# mkdir /web/html -p
#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.conf
125 DocumentRoot "/web/html" #指定默認發布目錄位置
126 <Directory "/web/html">
127 Require all granted #對于目錄訪問進行授權
128 </Directory>
[root@apache ~]# systemctl restart httpd
[root@apache ~]# echo "/web/html's page" > /web/html/index.html
[root@apache ~]# curl 172.25.254.100:8080
/web/html's page
3.默認發布文件
#建立新的默認發布文件
[root@apache ~]# echo "/web/html/lee's page" > /web/html/lee.html
#當沒有對配置進行修改時新默認發布文件不會被默認訪問
[root@apache ~]# curl 172.25.254.100:8080
/web/html's page
[root@apache ~]# curl 172.25.254.100:8080/lee.html
/web/html/lee's page
#修改配置文件
[root@apache ~]# vim /etc/httpd/conf/httpd.conf
172 <IfModule dir_module>
173 DirectoryIndex lee.html index.html
174 </IfModule>
#重啟服務
[root@apache ~]# systemctl reload httpd
#測試:
[root@apache ~]# curl 172.25.254.100:8080
/web/html/lee's page
4.https
#安裝mod_ssl
[root@apache ~]# dnf install mod_ssl -y#建立證書和key文件目錄
[root@apache ~]# mkdir /etc/httpd/certs#制作證書
[root@apache ~]# openssl req \
-newkey rsa:2048 \
-nodes \ # 這里 \ 表示空格的意思
-sha256 \
-keyout /etc/httpd/certs/timinglee.org.key \
-x509 \
-days 365 \
-out /etc/httpd/certs/timinglee.org.crtCountry Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shannxi
Locality Name (eg, city) [Default City]:XI'AN
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.timinglee.org
Email Address []:timinglee@timinglee.org#命令執行完成,證書出現
[root@apache ~]# ls /etc/httpd/certs/
timinglee.org.crt timinglee.org.key#編輯主配置文件
[root@apache ~]# vim /etc/httpd/conf.d/ssl.conf
86 SSLCertificateFile /etc/httpd/certs/timinglee.org.crt
95 SSLCertificateKeyFile /etc/httpd/certs/timinglee.org.key#重啟服務
root@apache ~]# systemctl reload httpd
[root@apache ~]# netstat -antlupe | grep httpd
tcp6 0 0 :::443 :::* LISTEN 0
85111 33518/httpd
tcp6 0 0 :::80 :::* LISTEN 0
80172 33518/httpd#在瀏覽器中訪問
https://服務器ip
6.apache的虛擬主機
#為每個發布站點建立默認發布目錄
[root@apache ~]# mkdir -p /var/www/virtual/timiniglee.org/news
[root@apache ~]# mkdir -p /var/www/virtual/timiniglee.org/bbs#為每個站點建立默認發布文件
[root@apache ~]# echo new.timinglee.org >
/var/www/virtual/timiniglee.org/news/index.html
[root@apache ~]# echo bbs.timinglee.org >
/var/www/virtual/timiniglee.org/bbs/index.html#修改配置文件
[root@apache ~]# vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost _default_:80>
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName bbs.timinglee.org
DocumentRoot /var/www/virtual/timiniglee.org/bbs/
</VirtualHost>
<VirtualHost *:80>
ServerName news.timinglee.org
DocumentRoot /var/www/virtual/timiniglee.org/news/
</VirtualHost>#刷新服務
[root@apache ~]# systemctl reload httpd
#測試:
1.在瀏覽器所在主機中手動編寫本地解析文件
[root@apache ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6#加入虛擬主機解析域名
172.25.254.100 mariadb.timinglee.org www.timinglee.org news.timinglee.org
bbs.timinglee.org2.測試效果
[root@apache ~]# curl www.timinglee.org
172.25.254.100
[root@apache ~]# curl bbs.timinglee.org
bbs.timinglee.org
[root@apache ~]# curl news.timinglee.org
new.timinglee.org