顯示廣告
隱藏 ✕
看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [CentOS] Apache 安裝與設定 (CentOS 7)
時間 2017-02-12 Sun. 22:11:45


=========================
如要在新版的 Rocky Linux 9 安裝的話,請改至這篇
[RockyLinux9] 網頁伺服器 Apache 安裝與設定 - KnucklesNote板 - Disp BBS
=========================

安裝環境: Linode 的 CentOS 7 64bit

安裝 Apache

$ sudo yum install httpd

CentOS 7 安裝的是 apache 2.4

啟動 httpd
$ sudo systemctl start httpd

查看執行狀態
$ sudo systemctl status httpd

防火牆開啟 80 與 443 port

使用 firewalld 的話
$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https
$ sudo firewall-cmd --reload

使用 iptables 的話,修改設定檔
iptables -A INPUT -p TCP -i eth0 --dport  80  -j ACCEPT   # HTTP
iptables -A INPUT -p TCP -i eth0 --dport  443  -j ACCEPT   # HTTPS


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



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



修改 Apache 設定檔

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


# 加上 prefork 設定,依記憶體大小調整
# 例如 Linode 2G 可以設定像這樣:
<IfModule prefork.c>
    StartServers        5
    MinSpareServers     20
    MaxSpareServers     40
    MaxRequestWorkers   256
    MaxConnectionsPerChild 5500
</IfModule>


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

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

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


設定虛擬主機 Virtual Host

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

新增 vhost.conf
$ sudo vim /etc/httpd/conf.d/vhost.conf
# 預設的網頁資料夾
<VirtualHost *:80>
    DocumentRoot  /var/www/html
</VirtualHost>

# 要允許 Apache 存取的資料夾
<Directory "/var/www/mydomain">
    Options FollowSymLinks
    AllowOverride All
    # Apache 2.4 後要用下面這行取代之前的 Order allow,deny 與 Allow from all
    Require all granted 
</Directory>
       
<VirtualHost *:80>
    ServerName    mydomain.com
    # 如果還有其他網址要寫在 ServerAlias
    ServerAlias   www.mydomain.com mydomain2.com
    DocumentRoot  /home/mydomain/www
    ErrorLog   "/home/mydomain/log/mydomain.error.log"
    CustomLog  "/home/mydomain/log/mydomain.access.log" combined
</VirtualHost>

先測試看看有沒有問題
$ sudo httpd -t

重新啟動 apache
$ sudo systemctl reload httpd

如果某個目錄要設定僅限某些 IP 可以讀取的話
<Directory "/var/www/mydomain/private">
    Options FollowSymLinks
    AllowOverride All
    Require all denied
    Require ip 192.168.1.100 192.168.1.101
</Directory>

如果要禁止某些 IP 讀取的話
<Directory "/var/www/mydomain">
    Options FollowSymLinks
    AllowOverride All
    # 有 not ip 的話要用 <RequireAll> 包起來
    <RequireAll>
        Require all granted 
        Require not ip 192.168.1.200 192.168.1.201
    </RequireAll>
</Directory>



參考
https://www.linode.com/docs/websites/apache/install-and-configure-apache-on-centos-7

設定網頁使用 gzip 壓縮
[CentOS] Apache 設定 deflate 網頁壓縮 - KnucklesNote板 - Disp BBS

設定檔案的快取時間
[CentOS] Apache 設定 expires 檔案cache時間 - KnucklesNote板 - Disp BBS

設定 mod_evasive 防止DDOS攻擊
[Apache] mod_evasive 阻擋DDoS攻擊 - KnucklesNote板 - Disp BBS

加上 Google 的 PageSpeed 模組
[Apache] 安裝 PageSpeed 模組 改善網頁速度 - KnucklesNote板 - Disp BBS

有使用 Linode Node Balancer 分流器時,使用 mod_remoteip 修正使用者的IP位址
[CentOS7] 設定 Apache mod_remoteip 修正 Balancer 的IP - KnucklesNote板 - Disp BBS

使用 https 加密連線
[CentOS7] Apache 使用 Certbot 申請 Let's Encrypt 的SSL憑證 - KnucklesNote板 - Disp BBS

--
※ 作者: Knuckles 時間: 2017-02-12 22:11:45
※ 編輯: Knuckles 時間: 2023-11-29 18:20:18 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 2648 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇