1、apache服務(wù)器安裝與配置
yum install httpd -y

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

2、apache2配置文件
安裝好httpd后會(huì)自動(dòng)生成/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文件會(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>
<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 //允許所有訪問請(qǐng)求
</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、配置基于端口號(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

(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)注冊(cè)DNS(配置DNS服務(wù)器并實(shí)現(xiàn)正常解析)、臨時(shí)測(cè)試時(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


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。