一、Apache 是什么?
Apache(全稱?Apache HTTP Server)是當前最流行的開源Web服務器軟件之一,由Apache軟件基金會維護。它以穩定性高、模塊化設計和靈活的配置著稱,支持Linux、Windows等多平臺,是搭建個人博客、企業官網乃至復雜Web應用的首選工具。
/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
# 1. 安裝Apache
sudo dnf install httpd -y# 2. 放行防火墻(允許HTTP/HTTPS流量)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload# 3. 啟動并設置開機自啟
sudo systemctl enable --now httpd# 4.生成默認測試頁文件
echo 172.25.254.100 > /var/www/html/index.html# 5.測試:
curl 172.25.254.100
172.25.254.100
三、Apache的基本配置信息
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.默認發布目錄
修改selinux ——開著的話會有所影響
grubby --update-kernel ALL --args selinux=0reboot ——重啟getenforce
Disabled#建立默認發布目錄
[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.conf86 SSLCertificateFile /etc/httpd/certs/timinglee.org.crt95 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
5.apache的虛擬主機
修改selinux ——有所影響
grubby --update-kernel ALL --args selinux=1reboot ——重啟getenforce
Enforcing#為每個發布站點建立默認發布目錄
[root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/news
[root@apache ~]# mkdir -p /var/www/virtual/timinglee.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.orgDocumentRoot /var/www/virtual/timiniglee.org/bbs/
</VirtualHost><VirtualHost *:80>ServerName news.timinglee.orgDocumentRoot /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