Apache HTTP Server 從安裝到配置過程詳解
更新時間:2025年07月14日 09:40:09 作者:Fantasy丶夜雨笙歌
Apache(全稱)是當前最流行的開源Web服務器軟件之一,由Apache軟件基金會維護,它以和著稱,支持Linux、Windows等多平臺,是搭建個人博客、企業(yè)官網(wǎng)乃至復雜Web應用的首選工具,本文給大家介紹Apache HTTP Server 從安裝到配置方法,感興趣的朋友一起看看吧
一、Apache 是什么?
Apache(全稱 Apache HTTP Server)是當前最流行的開源Web服務器軟件之一,由Apache軟件基金會維護。它以穩(wěn)定性高、模塊化設計和靈活的配置著稱,支持Linux、Windows等多平臺,是搭建個人博客、企業(yè)官網(wǎng)乃至復雜Web應用的首選工具。
#apache的基本信息
| /etc/httpd/conf | #apache的配置目錄 |
|---|---|
| /etc/http/conf.d | #子配置目錄 |
| /etc/httpd/conf/httpd.conf | #主配置文件 |
| /lib/systemd/system/htpd.service | #啟動文件 |
| :80 | #默認端口 |
| /var/www/html | #默認發(fā)布目錄 |
| index.html | #默認發(fā)布文件 |
二、安裝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.1002.默認發(fā)布目錄
修改selinux ——開著的話會有所影響 grubby --update-kernel ALL --args selinux=0 reboot ——重啟 getenforce Disabled #建立默認發(fā)布目錄 [root@apache ~]# mkdir /web/html -p #修改配置文件 [root@apache ~]# vim /etc/httpd/conf/httpd.conf 125 DocumentRoot "/web/html" #指定默認發(fā)布目錄位置 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.默認發(fā)布文件
#建立新的默認發(fā)布文件 [root@apache ~]# echo "/web/html/lee's page" > /web/html/lee.html #當沒有對配置進行修改時新默認發(fā)布文件不會被默認訪問 [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.crt
Country 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
#命令執(zhí)行完成,證書出現(xiàn)
[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
5.apache的虛擬主機
修改selinux ——有所影響 grubby --update-kernel ALL --args selinux=1 reboot ——重啟 getenforce Enforcing #為每個發(fā)布站點建立默認發(fā)布目錄 [root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/news [root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/bbs #為每個站點建立默認發(fā)布文件 [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.org 2.測試效果 [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
到此這篇關于Apache HTTP Server 從安裝到配置過程詳解的文章就介紹到這了,更多相關Apache HTTP Server安裝配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Linux SSHD啟動失敗:OpenSSL版本不匹配問題分析與解決方法
在 Linux 服務器上,sshd 可能因 OpenSSL 版本不匹配而啟動失敗,本篇文章將詳細分析該錯誤的原因,并提供多種解決方案,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下2025-07-07
解決CentOS7虛擬機無法上網(wǎng)并設置CentOS7虛擬機使用靜態(tài)IP上網(wǎng)
這篇文章主要介紹了解決CentOS7虛擬機無法上網(wǎng)并設置CentOS7虛擬機使用靜態(tài)IP上網(wǎng),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02

