Apache常用虛擬主機(jī)配置方法解析
1、apache服務(wù)器安裝與配置
yum install httpd -y

systemctl start httpd &&systemctl enable httpd
systemctl stop firewalld
setenforce 0 //設(shè)置selinux安全級別為premise重啟會失效.
本機(jī)windows瀏覽器測試虛擬機(jī)ip地址(一定要關(guān)閉防火墻),看到以下界面代表啟動http成功。

2、apache2配置文件
安裝好httpd后會自動生成/etc/httpd目錄

主要配置文件: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地址

(2)在/etc/httpd/conf.d目錄中增加一個(gè)站點(diǎn)文件lyn1.conf
也可以在/etc/httpd/conf/httpd.conf 直接配置,httpd.conf文件會自動導(dǎo)入/etc/httpd/conf.d中文件,為了方便我們直接寫到/etc/httpd/conf.d文件夾下
mkdir /mnt/lyn1
cd /etc/httpd/conf.d
vi lyn1.conf
<VirtualHost192.168.100.135> //本機(jī)ip地址 DocumentRoot/mnt/lyn1/ //網(wǎng)絡(luò)數(shù)據(jù)目錄 ServerNamewww.lyn1.com //網(wǎng)站服務(wù)器的域名<Directory/mnt/lyn1/> //網(wǎng)站數(shù)據(jù)目錄的權(quán)限 AllowOverrideNone //不允許重寫 Requireallgranted //允許所有訪問請求 </Directory> </VirtualHost>
(3)在shiyan1.com對應(yīng)網(wǎng)站的發(fā)布目錄下增加網(wǎng)頁文件index.html
vi /mnt/lyn1/index.html
<html> <head> <title>lyn1</title> </head> <body> <h1>lyn1</h1> 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 //允許所有訪問請求 </Directory> </VirtualHost>
vi /mnt/lyn2/index.html
<html> <head> <title>lyn2</title> </head> <body> <h1>lyn2</h1> this is the www.lyn2.com website </body> </html>
(6)重啟Apache服務(wù)器,并使用瀏覽器進(jìn)行驗(yàn)證
systemctl restart httpd


4、配置基于端口號的虛擬主機(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

(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


5、配置基于主機(jī)名的虛擬機(jī)
www.lyn1.com----192.168.100.135:80
www.lyn2.com----192.168.100.135:80
(1)注冊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

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


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信