在CentOS上搭建Web服務器是一個相對簡單的過程,可以選擇使用Apache或Nginx作為Web服務器軟件。以下是使用Apache和Nginx搭建Web服務器的詳細步驟:
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
/etc/httpd/conf.d/example.com.conf
:VirtualHost *:80
ServerAdmin webmaster@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
創建網站目錄并設置權限:
sudo mkdir -p /var/www/html/example.com
sudo chown -R apache:apache /var/www/html/example.com
sudo chmod -R 755 /var/www/html/example.com
重啟Apache服務:
sudo systemctl restart httpd
sudo yum update -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
/etc/nginx/conf.d/example.com.conf
:server {
listen 80;
server_name example.com;
root /usr/share/nginx/html/example.com;
}
重啟Nginx服務:
sudo systemctl restart nginx
以上步驟展示了如何在CentOS上安裝和配置基本的Web服務器。根據具體需求,可能還需要進一步配置SSL證書、優化性能、設置監控等。