在 CentOS 系統下設置 Apache2 虛擬主機,你需要遵循以下步驟:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
mywebsite
的目錄:sudo mkdir /var/www/mywebsite
sudo chown -R apache:apache /var/www/mywebsite
/etc/httpd/conf/httpd.conf
文件中添加以下內容,以啟用虛擬主機配置:IncludeOptional conf.d/*.conf
mywebsite.conf
的文件:sudo vi /etc/httpd/conf.d/mywebsite.conf
mywebsite.conf
文件中添加以下內容,根據需要進行修改:<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/mywebsite
<Directory /var/www/mywebsite>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/mywebsite-error.log
CustomLog /var/log/httpd/mywebsite-access.log combined
</VirtualHost>
保存并關閉文件。
重啟 Apache 服務以應用更改:
sudo systemctl restart httpd
更新你的 DNS 設置,將域名(例如:mywebsite.com)指向你的服務器 IP 地址。
在你的服務器上創建一個簡單的 HTML 文件,以測試虛擬主機是否正常工作。例如,在 /var/www/mywebsite
目錄中創建一個名為 index.html
的文件,并添加以下內容:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
</body>
</html>
現在你已經在 CentOS 系統下成功設置了 Apache2 虛擬主機。你可以根據需要創建更多的虛擬主機配置文件,并為每個站點指定不同的域名和目錄。