安裝

由於 CentOS 本身內建的 Apache httpd 的版本是 2.4.6 比較舊, 可能有安全性疑慮, 故須將其更新, 先安裝 CodeIT 套件庫並更新.

cd /etc/yum.repos.d && wget https://repo.codeit.guru/codeit.el`rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release)`.repo

執行安裝

yum -y install httpd

安裝完成後啟動 httpd

systemctl start httpd.service

建立 httpd 的系統啟動鏈接,在系統啟動時自動啟動 httpd

systemctl enable httpd.service

CentOS 7.X 安裝完成後,預設使用防火牆關閉所有連接port,因此您必須對其進行自定義,以允許外部訪問 port 80 (http) 和 port 443 (https),執行下面指令將 80 與 443 port 打開.

systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

做到這邊正常來說,Apache 已經可以正常工作,在瀏覽器中輸入 http://你的網址/ 確認網站是否正常運作。

設定檔

在CentOS 之下 Apache 用 yum 安裝完成後,開啟 /etc/httpd/conf/httpd.conf 檔案 (已刪除註解部分):

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on

IncludeOptional conf.d/*.conf
最後修改日期: 2019 年 12 月 19 日