溫馨提示×

CentOS Apache配置有哪些技巧

小樊
38
2025-05-12 23:27:45
欄目: 智能運維

在CentOS上配置Apache服務器時,可以采用以下一些技巧來提高其性能和安全性:

  1. 安裝與基本配置

    • 更新系統并安裝Apache:
      sudo yum update -ysudo yum install httpd -y
      
    • 啟動并啟用Apache服務:
      sudo systemctl start httpdsudo systemctl enable httpd
      
    • 檢查服務狀態:
      sudo systemctl status httpd
      
  2. 配置防火墻

    • 開放HTTP(端口80)和HTTPS(端口443):
      sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --reload
      
  3. 虛擬主機配置

    • 基于名字的虛擬主機

      sudo mkdir -p /var/www/nn /var/www/hh
      echo "nn.yunrange.com, NAME based virtual host test file." | sudo tee /var/www/nn/index.html
      echo "hh.yunrange.com, NAME based virtual host test file." | sudo tee /var/www/hh/index.html
      

      編輯 /etc/httpd/conf/httpd.conf 文件,添加以下內容:

      NameVirtualHost *:80
      VirtualHost *:80 DocumentRoot /var/www/nn
      ServerName nn.yunrange.com
      VirtualHost *:80 DocumentRoot /var/www/hh
      ServerName hh.yunrange.com
      

      重啟Apache服務:

      sudo systemctl restart httpd
      
    • 基于IP的虛擬主機

      sudo mkdir -p /var/www/ip1 /var/www/ip2
      echo "IP based virtual host file for IP1." | sudo tee /var/www/ip1/index.html
      echo "IP based virtual host file for IP2." | sudo tee /var/www/ip2/index.html
      

      編輯 /etc/httpd/conf/httpd.conf 文件,添加以下內容:

      VirtualHost 192.168.5.150:80 DocumentRoot /var/www/ip1
      ServerName 192.168.5.150
      VirtualHost 192.168.5.151:80 DocumentRoot /var/www/ip2
      ServerName 192.168.5.151
      

      重啟Apache服務:

      sudo systemctl restart httpd
      
  4. SSL配置

    • 為你的域名生成并安裝SSL證書(使用Let’s Encrypt):
      sudo yum install epel-release -y
      sudo yum install certbot python2-certbot-apache -y
      sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
      
  5. 性能優化

    • 啟用KeepAlive:
      KeepAlive On
      MaxKeepAliveRequests 100
      KeepAliveTimeout 5
      
    • 調整并發連接數:
      StartServers 5
      MinSpareServers 5
      MaxSpareServers 10
      MaxRequestWorkers 150
      MaxConnectionsPerChild 0
      
    • 啟用壓縮模塊:
      AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
      
    • 啟用瀏覽器緩存:
      IfModule mod_expires.c
      ExpiresActive On
      ExpiresByType text/css "access plus 30 days"
      ExpiresByType image/jpeg "access plus 1 year"
      ExpiresByType image/png "access plus 1 year"
      ExpiresByType image/gif "access plus 1 year"
      ExpiresByType application/javascript "access plus 30 days"
      
  6. 安全性

    • 隱藏服務器版本信息:
      ServerTokens Prod
      ServerSignature Off
      
    • 配置防火墻規則:
      sudo firewall-cmd --permanent --add-service=http
      sudo firewall-cmd --permanent --add-service=https
      sudo firewall-cmd --reload
      
    • 定期備份配置文件和網站數據,并監控日志文件。

通過這些配置技巧,可以顯著提高Apache服務器的性能、安全性和穩定性。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女