如何在CentOS 7上安装Apache

Apache 是一個免費、開源且流行的 HTTP 伺服器,運行在包括 Linux 和 Windows 作業系統在內的類 Unix 作業系統上。自從 20 年前釋出以來,它一直是最受歡迎的網頁伺服器,為互聯網上的多個網站提供動力。它易於安裝和配置,可用於在同一台 Linux 或 Windows 伺服器上託管單個或多個網站。

在本文中,我們將解釋如何在 CentOS 7 或 RHEL 7 伺服器上使用命令行安裝、配置和管理 Apache HTTP 網頁伺服器。

先決條件:

  1. A CentOS 7 Server Minimal Install
  2. A RHEL 7 Server Minimal Install
  3. A CentOS/RHEL 7 system with static IP address

安裝 Apache Web 伺服器

1. 首先將系統軟體套件更新到最新版本。

# yum -y update

2. 接下來,使用 YUM 套件管理器 從默認軟體庫安裝 Apache HTTP 伺服器,如下所示。

# yum install httpd
Install Apache on CentOS 7

在 CentOS 7 上管理 Apache HTTP 伺服器

3. 安裝完 Apache 網頁伺服器後,您可以首次啟動它並啟用自動在系統啟動時啟動。

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
Start and Enable Apache

配置 firewalld 以允許 Apache 流量

4. 默認情況下,CentOS 7 內建防火牆設置為阻止 Apache 流量。為了允許 Apache 上的網路流量,請使用以下命令更新系統防火牆規則以允許 HTTPHTTPS 上的入站封包。

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload
Configure firewalld to Allow Apache

在 CentOS 7 上測試 Apache HTTP 伺服器

5.現在您可以通過訪問以下網址來驗證Apache服務器,將顯示一個默認的Apache頁面。

http://SERVER_DOMAIN_NAME_OR_IP 
Default Apache Welcome Page

在CentOS 7上配置基於名稱的虛擬主機

如果您想在同一個Apache Web服務器上托管多個域名(虛擬主機),則本節非常有用。有許多設置虛擬主機的方法,但我們將在這裡解釋其中一種最簡單的方法。

6.首先在/etc/httpd/conf.d/目錄下創建一個vhost.conf文件,用於存儲多個虛擬主機配置。

# vi /etc/httpd/conf.d/vhost.conf

添加以下示例虛擬主機指令模板,用於網站mytecmint.com,請務必更改您自己域名的必要值

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mytecmint.com
    ServerAlias www.mytecmint.com
    DocumentRoot /var/www/html/mytecmint.com/
    ErrorLog /var/log/httpd/mytecmint.com/error.log
    CustomLog /var/log/httpd/mytecmint.com/access.log combined
</VirtualHost>
Apache Virtual Host Configurations

重要: 您可以將許多域名添加到vhost.conf文件中,只需復制上面的VirtualHost塊並為添加的每個域名更改值。

7.現在按照上面VirtualHost塊中引用的mytecmint.com網站創建目錄。

# mkdir -p /var/www/html/mytecmint.com    [Document Root - Add Files]
# mkdir -p /var/log/httpd/mytecmint.com   [Log Directory]

8./var/www/html/mytecmint.com下創建一個虛擬index.html頁面。

# echo "Welcome to My TecMint Website" > /var/www/html/mytecmint.com/index.html

9.最後,重新啟動Apache服務以使上述更改生效。

# systemctl restart httpd.service

10.現在您可以訪問mytecmint.com來測試上面創建的索引頁面。

Check Virtualhost Website

Apache重要文件和目錄

  • 默認服務器根目錄(包含配置文件的頂級目錄):/etc/httpd
  • 主要的Apache配置文件: /etc/httpd/conf/httpd.conf
  • 其他配置可添加在: /etc/httpd/conf.d/
  • Apache虛擬主機配置文件: /etc/httpd/conf.d/vhost.conf
  • 模組配置: /etc/httpd/conf.modules.d/
  • Apache預設伺服器文件根目錄(存放網頁文件): /var/www/html

您可能也喜歡閱讀以下與Apache網頁伺服器相關的文章。

  1. 13個Apache網頁伺服器安全性和加固提示
  2. 5個提升Apache網頁伺服器性能的提示
  3. 如何安裝Let’s Encrypt SSL憑證來保護Apache
  4. 使用Mod_Security和Mod_evasive模組保護Apache免受暴力或DDoS攻擊
  5. 如何使用.htaccess文件在Apache中對網頁目錄進行密碼保護
  6. 如何檢查 Linux 中啟用/載入的 Apache 模組
  7. 如何將 Apache 伺服器名稱更改為伺服器標頭中的任何內容

就是這樣!如有任何問題或想分享其他想法,請使用下方的反饋表單。並且請記得保持與 Tecmint.com 的聯繫。

Source:
https://www.tecmint.com/install-apache-on-centos-7/