溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Apache虛擬主機(jī)的配置與安裝

發(fā)布時(shí)間:2020-11-04 15:28:30 來源:億速云 閱讀:241 作者:15號(hào)計(jì)劃 欄目:開發(fā)技術(shù)

這篇文章運(yùn)用簡單易懂的例子給大家介紹Apache虛擬主機(jī)的配置與安裝,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

1、apache服務(wù)器安裝與配置

yum install httpd -y

Apache虛擬主機(jī)的配置與安裝

systemctl start httpd &&systemctl enable httpd

systemctl stop firewalld

setenforce 0 //設(shè)置selinux安全級(jí)別為premise重啟會(huì)失效.

本機(jī)windows瀏覽器測試虛擬機(jī)ip地址(一定要關(guān)閉防火墻),看到以下界面代表啟動(dòng)http成功。

Apache虛擬主機(jī)的配置與安裝

2、apache2配置文件

安裝好httpd后會(huì)自動(dòng)生成/etc/httpd目錄

Apache虛擬主機(jī)的配置與安裝

主要配置文件:conf/httpd.conf

3、基于IP地址(服務(wù)器需要多個(gè)公網(wǎng)IP地址)

www.lyn1.com----192.168.100.135
www.lyn2.com----192.168.100.136

(1)給服務(wù)器另外再綁定一個(gè)IP地址

Apache虛擬主機(jī)的配置與安裝

(2)在/etc/httpd/conf.d目錄中增加一個(gè)站點(diǎn)文件lyn1.conf

也可以在/etc/httpd/conf/httpd.conf 直接配置,httpd.conf文件會(huì)自動(dòng)導(dǎo)入/etc/httpd/conf.d中文件,為了方便我們直接寫到/etc/httpd/conf.d文件夾下

mkdir /mnt/lyn1

cd /etc/httpd/conf.d

vi lyn1.conf

<VirtualHost 192.168.100.135>  //本機(jī)ip地址
DocumentRoot /mnt/lyn1/     //網(wǎng)絡(luò)數(shù)據(jù)目錄
ServerName www.lyn1.com     //網(wǎng)站服務(wù)器的域名<Directory /mnt/lyn1/ >     //網(wǎng)站數(shù)據(jù)目錄的權(quán)限
AllowOverride None       //不允許重寫
Require all granted       //允許所有訪問請(qǐng)求
</Directory>
</VirtualHost>

(3)在shiyan1.com對(duì)應(yīng)網(wǎng)站的發(fā)布目錄下增加網(wǎng)頁文件index.html

vi /mnt/lyn1/index.html

<html>
<head>
<title>lyn1</title>
</head>
<body>
<h2>lyn1</h2>
this is the www.lyn1.com website
</body>
</html>

(4)在/etc/httpd/conf.d目錄中增加一個(gè)站點(diǎn)文件lyn2.conf

mkdir /mnt/lyn1

cd /etc/httpd/conf.d

vi lyn1.conf

<VirtualHost 192.168.100.136>  //本機(jī)另一個(gè)ip地址
DocumentRoot /mnt/lyn2/     //網(wǎng)絡(luò)數(shù)據(jù)目錄
ServerName www.lyn2.com     //網(wǎng)站服務(wù)器的域名
<Directory /mnt/lyn2/ >     //網(wǎng)站數(shù)據(jù)目錄的權(quán)限
AllowOverride None       //不允許重寫
Require all granted       //允許所有訪問請(qǐng)求
</Directory>
</VirtualHost>

vi /mnt/lyn2/index.html

<html>
<head>
<title>lyn2</title>
</head>
<body>
<h2>lyn2</h2>
this is the www.lyn2.com website
</body>
</html>

(6)重啟Apache服務(wù)器,并使用瀏覽器進(jìn)行驗(yàn)證

systemctl restart httpd

Apache虛擬主機(jī)的配置與安裝

Apache虛擬主機(jī)的配置與安裝

4、配置基于端口號(hào)的虛擬主機(jī)

www.lyn1.com----192.168.100.135:80
www.lyn2.com----192.168.100.135:81

(1)在主配置文件/etc/httpd/conf/httpd.conf文件中增加監(jiān)聽端口81

#vi /etc/httpd/conf/httpd.conf
Listen 80
Listen 81

Apache虛擬主機(jī)的配置與安裝

(2)修改/etc/httpd/conf.d/lyn1.conf文件:

<VirtualHost 192.168.100.135:80>
ServerName www.lyn1.com
DocumentRoot /var/www/html/lyn1/
<Directory /var/www/html/lyn1/ > 
AllowOverride None     
Require all granted   
</Directory>
</VirtualHost>

(3)修改/etc/httpd/conf.d/shiyan2.conf文件:

<VirtualHost 192.168.100.135:81>
ServerName www.lyn2.com
DocumentRoot /var/www/html/lyn2/
<Directory /var/www/html/lyn2/ > 
AllowOverride None    
Require all granted  
</Directory>
</VirtualHost>

(4)重啟Apache服務(wù)器,并使用瀏覽器進(jìn)行驗(yàn)證

systemctl restart httpd

Apache虛擬主機(jī)的配置與安裝

Apache虛擬主機(jī)的配置與安裝

5、配置基于主機(jī)名的虛擬機(jī)

www.lyn1.com----192.168.100.135:80
www.lyn2.com----192.168.100.135:80

(1)注冊(cè)DNS(配置DNS服務(wù)器并實(shí)現(xiàn)正常解析)、臨時(shí)測試時(shí)可以使用修改/etc/hosts方法,此處采用修改hosts方法

#vi /etc/hosts

192.168.100.135 www.lyn1.com

192.168.100.135 www.lyn2.com

(2)在主配置文件/etc/httpd/conf.d/lyn1.conf文件中

<VirtualHost *:80>
ServerName www.lyn1.com
DocumentRoot /var/www/html/lyn1/
<Directory /var/www/html/lyn1/ >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

(3)在主配置文件/etc/httpd/conf.d/lyn2.conf文件中

<VirtualHost *:81>
ServerName www.lyn2.com
DocumentRoot /var/www/html/lyn2/
<Directory /var/www/html/lyn2/ >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

(4)重啟apache2服務(wù)器并進(jìn)行驗(yàn)證

systemctl restart httpd

[root@lyn html]# curl www.lyn1.com

[root@lyn html]# curl www.lyn2.com

Apache虛擬主機(jī)的配置與安裝

windows下訪問網(wǎng)站要向C:\Windows\System32\drivers\etc\hosts文件中追加下面兩行

192.168.100.135 www.lyn1.com

192.168.100.135 www.lyn2.com

Apache虛擬主機(jī)的配置與安裝

Apache虛擬主機(jī)的配置與安裝

關(guān)于Apache虛擬主機(jī)的配置與安裝就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI