在centos stream 8系統上搭建web服務器,需要安裝并配置web服務器軟件(如apache或nginx),并進行必要的安全設置。以下步驟提供了一個基本的配置流程:
Web服務器軟件安裝
sudo dnf install httpd -y
Nginx的安裝則需要參考其官方文檔進行編譯安裝,例如安裝Nginx 1.22版本。
防火墻配置
centos 8默認防火墻會阻止外部訪問Web服務器。 需要開放HTTP和https端口:
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
虛擬主機創建
若需在一臺服務器上運行多個網站,需要創建虛擬主機。 例如,創建名為example.com的虛擬主機:
sudo nano /etc/httpd/conf.d/example.com.conf
在文件中添加以下內容:
<VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/html/example.com ServerName example.com ServerAlias www.example.com ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined </VirtualHost>
ssl/TLS配置
為了保障數據安全,建議配置SSL/TLS。 可以使用Let’s Encrypt等免費服務獲取SSL證書。 安裝Certbot并獲取證書:
sudo yum install certbot Python2-certbot-apache -y sudo certbot --apache
設置開機自啟動
確保Apache在系統重啟后自動啟動:
sudo systemctl enable httpd
服務器監控與維護
定期檢查Apache的訪問日志和錯誤日志,并使用yum-cron工具進行自動更新,是保持服務器穩定運行的關鍵。
注意: 以上步驟僅為基本指南,實際配置可能因具體需求而異。 請根據實際情況調整相關參數。