在 CentOS 系統下,使用 Apache2(實際上在 CentOS 上稱為 httpd)設置虛擬主機的步驟如下:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
mywebsite
的目錄:sudo mkdir /var/www/mywebsite
sudo chown apache:apache /var/www/mywebsite
echo "<h1>Welcome to My Website</h1>" | sudo tee /var/www/mywebsite/index.html
mywebsite.conf
的文件:sudo vim /etc/httpd/conf.d/mywebsite.conf
<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/mywebsite
ErrorLog /var/log/httpd/mywebsite-error.log
CustomLog /var/log/httpd/mywebsite-access.log combined
</VirtualHost>
保存并關閉配置文件。
檢查 Apache 配置文件的語法是否正確:
sudo apachectl configtest
如果輸出顯示 “Syntax OK”,則表示配置正確。
sudo systemctl reload httpd
mywebsite.com
解析到服務器的 IP 地址。在 /etc/hosts
文件中添加以下行:127.0.0.1 mywebsite.com www.mywebsite.com
http://mywebsite.com
,您應該看到 “Welcome to My Website” 頁面。現在,您已成功在 CentOS 系統下為 Apache2 設置了虛擬主機。如果您有多個虛擬主機,只需為每個虛擬主機創建一個類似的配置文件,并確保為每個虛擬主機指定唯一的 ServerName
和 DocumentRoot
。