顯示廣告
隱藏 ✕
看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [RockyLinux9] 網頁伺服器 Apache 安裝與設定
時間 2023-09-22 Fri. 01:11:42


安裝環境: Linode 的 Rocky Linux 9

參考: [Linode] 新增 RockyLinux9 主機 - KnucklesNote板

安裝 Apache

$ sudo dnf install httpd

Rocky Linux 9 安裝的是 apache 2.4.53

啟動 httpd
$ sudo systemctl start httpd

設定 Apache 服務隨系統一起啟動
$ sudo systemctl enable httpd

查看執行狀態
$ sudo systemctl status httpd

防火牆開啟 80 與 443 port
$ sudo firewall-cmd --permanent --zone=public --add-service={http,https}
$ sudo firewall-cmd --reload

用瀏覽器連看看能不能看到 Apache 的預設畫面
[圖]



修改 Apache 設定檔

查看 Apache 的工作模式
$ sudo httpd -V
Server version: Apache/2.4.53 (Rocky Linux)
Server built:   Apr 28 2023 00:00:00
Server's Module Magic Number: 20120211:124
Server loaded:  APR 1.7.0, APR-UTIL 1.6.1, PCRE 8.44 2020-02-12
Compiled using: APR 1.7.0, APR-UTIL 1.6.1, PCRE 8.44 2020-02-12
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
可以看到 Server MPM: event
代表多工處理模式(MPM, Multi-Processing Module)預設是使用 event

Apache 的主要設定檔在 /etc/httpd/conf/httpd.conf
基本上不要改這個檔,而是將要修改的設定另外寫在 /etc/httpd/conf.d/ 裡
例如新增一個 /etc/httpd/conf.d/common.conf

$ sudo vim /etc/httpd/conf.d/common.conf
# 設定 ServerName,將 xxx.xxx.xxx.xxx 改成主機的網址或IP位址
ServerName xxx.xxx.xxx.xxx:80

# 網址沒有指定檔名時,預設開啟的檔名
# 預設 DirectoryIndex index.html
# 加上常用的 index.htm
# 不用加 index.php 安裝php後會自動加在 /etc/httpd/conf.d/php.conf
DirectoryIndex index.html index.htm

# 記憶體管理模式使用 event 時,依記憶體大小調整以下設定
<IfModule event.c>
        StartServers             2
        MinSpareThreads          25
        MaxSpareThreads          75
        ThreadLimit              64
        ThreadsPerChild          25
        MaxRequestWorkers        150
        MaxConnectionsPerChild   0
</IfModule>

若沒有設定 ServerName 的話會出現警告
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.x.x. Set the 'ServerName' directive globally to suppress this message

先測試看看有沒有問題
$ sudo apachectl configtest
或是
$ sudo httpd -t
Syntax OK


重新載入設定檔
$ sudo systemctl reload httpd

也可以在不中斷使用者連線的情況下更新設定檔
$ sudo apachectl graceful


設定虛擬主機 Virtual Host

例如我有申請一個網址 mydomain.com 會轉為主機的IP位址
我想要讓這個網址連進來是連到 /var/www/mydomain/ 這個資料夾
連線的記錄檔放在 /var/log/httpd/ 資料夾

先在 /var/www/ 新增 mydomain 資料夾
$ sudo mkdir /var/www/mydomain

設定權限讓 apache 可以讀寫
$ sudo chown apache.apache /var/www/mydomain -R

新增 vhost.conf
$ sudo vim /etc/httpd/conf.d/vhost.conf
# 預設的網頁資料夾
<VirtualHost *:80>
    DocumentRoot  /var/www/html
</VirtualHost>
       
<VirtualHost *:80>
    ServerName    mydomain.com
    # 如果還有其他網址要寫在 ServerAlias
    ServerAlias   www.mydomain.com mydomain2.com
    DocumentRoot  /var/www/mydomain

    <Directory "/var/www/mydomain">
        Options FollowSymLinks
        AllowOverride All
        # 下面這行是用來取代之前的 Allow from all
        Require all granted 
    </Directory>

    ErrorLog   "/var/log/httpd/mydomain.error.log"
    CustomLog  "/var/log/httpd/mydomain.access.log" combined
</VirtualHost>

測試並重啟httpd
$ sudo httpd -t
$ sudo systemctl restart httpd


SELinux 設定

因為預設有開啟 SELinux 的關係,網頁資料夾若放在 /var/www 以外的地方,
重啟 httpd 會出現錯誤:
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xeu httpd.service" for details.

查看目前的 SELinux 模式
$ sudo getenforce
Enforcing

將 SELinux 改為寬容模式(permissive)
$ sudo setenforce 0

重啟 httpd 看看,若沒問題那就是 SELinux 擋住的關係

將 SELinux 改為強制模式(Enforcing)
$ sudo setenforce 1


允許 httpd 可以連網路
$ setsebool -P httpd_can_network_connect on

允許可以存取 NFS
$ setsebool -P httpd_use_nfs on

需要上傳檔案的資料夾
$ chcon -R -t httpd_sys_rw_content_t {資料夾路徑}

查看資料夾的 SELinux 設定
$ ll -Z


相關文章:
[CentOS7] Apache 安裝與設定 - KnucklesNote板 - Disp BBS
[CentOS7] Apache 使用 Certbot 申請 Let's Encrypt 的SSL憑證 - KnucklesNote板 - Disp BBS
[Apache] log分析工具 AWStats - KnucklesNote板 - Disp BBS
[Apache] 讀取log檔的流量分析工具 GoAccess - KnucklesNote板 - Disp BBS
[Apache] 安裝 PageSpeed 模組 改善網頁速度 - KnucklesNote板 - Disp BBS
[Apache] mod_evasive 阻擋DDoS攻擊 - KnucklesNote板 - Disp BBS

參考:
https://www.linode.com/docs/guides/how-to-install-a-lamp-stack-on-centos-8/

--
※ 作者: Knuckles 時間: 2023-09-22 01:11:43 (台灣)
※ 編輯: Knuckles 時間: 2024-04-19 08:48:41 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 110 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇