其他分享
首页 > 其他分享> > Apache虚拟主机

Apache虚拟主机

作者:互联网

Apache虚拟主机

安装配置Apache

(1)	下载阿帕奇rpm软件包
(2)	Tar解包
tar zxvf apr-l.6.2.tar.gz
tar zxvf apr-util-l.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2 
(3)	将rpm包移动到阿帕奇文件内
mv apr-1.6.2 httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
(4)	Yum安装依赖包
yum -y install pcre pcre-devel zlib-devel perl expat-devel
(5),/configure配置
1) 进入阿帕奇主目录:
cd httpd-2.4.29/
2)配置,添加功能模块:
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite  --enable-cgi
make && make install
3)实现开机自启:
cp /usr/local/httpd/bin/apachectl  /etc/init.d/httpd
vi /etc/init.d/httpd
#chkconfig: 35 85 21   //35级别自动运行  第85个启动  第21个关闭
#description: Apache is a World Wide Web server
chkconfig  --add httpd  //将httpd加入到SERVICE管理器
绑定域名:
ln -s /usr/local/httpd/conf/httpd.conf /etc/
vi /etc/httpd.conf
ServerName www.mcc.com:80
做映射:
vim /etc/hosts
192.168.17.10 www.mcc.com
ln -s /usr/local/httpd/bin/*  /usr/bin/
systemctl start httpd
检查语法:
httpd -t
apachectl –t
检查监听端口是否正常:
netstat –anpt | grep 80

做虚拟主机

基于域名的虚拟主机

1.	创建测试页:创建两个目录,并分别在目录下创建主页
mkdir /opt/mcc
mkdir /opt/mcc2
cd /opt/mcc  (此处仅以第一个目录为例)
vim index.html
<html><body><h1>this is server mcc</h1></body></html>
2.	创建站点:添加虚拟主机配置
vim /etc/httpd.conf
#Include conf/extra/httpd-vhosts.conf  将此处的#去掉,启用专门建虚拟主机的vhosts配置文件;

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf创建独立的配置文件
默认虚拟主机(此处仅以第一个为例):
<VirtualHost *:80>
ServerAdmin webmaster@mcc. com   邮箱地址
DocumentRoot "/opt/ mcc"       默认的站点路径
ServerName www.mcc. com       域名
ErrorLog "logs/mcc.com-error_log"  出错日志路径
customLog "logs/mcc.com-access_log" common  访问日志路径
</VirtualHost>
授权:
<Directory "/opt/mcc">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

3.	做映射
vim /etc/hosts
192.168.17.10 www.mcc.com  www.mcc2.com
systemctl restart httpd

基于IP地址的虚拟主机

1.	给虚拟机添加一张网卡;
或者给现有网卡添加一个虚拟地址,并检查是否添加成功:
ifconfig ens33:1  192.168.17.11/24
ip addr
2.	做捆绑
修改虚拟主机的独立的配置文件
将里面的默认虚拟主机处的*改为IP地址
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf创建独立的配置文件
默认虚拟主机(此处仅以第一个为例):
<VirtualHost 192.18.17.10:80>
ServerAdmin webmaster@mcc. com   邮箱地址
DocumentRoot "/opt/ mcc"       默认的站点路径
ServerName www.mcc. com       域名
ErrorLog "logs/mcc.com-error_log"  出错日志路径
customLog "logs/mcc.com-access_log" common  访问日志路径
</VirtualHost>
授权:
<Directory "/opt/mcc">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

基于端口的虚拟主机

1.	修改虚拟主机的独立的配置文件
将里面的默认虚拟主机处的80改为其他端口号
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf创建独立的配置文件
默认虚拟主机:
<VirtualHost 192.18.17.10:8080>
ServerAdmin webmaster@mcc. com   邮箱地址
DocumentRoot "/opt/ mcc"       默认的站点路径
ServerName www.mcc. com       域名
ErrorLog "logs/mcc.com-error_log"  出错日志路径
customLog "logs/mcc.com-access_log" common  访问日志路径
</VirtualHost>
授权:
<Directory "/opt/mcc">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

2.	修改主配置文件,添加一个默认的监听端口
vim  /etc/httpd.conf
Listen 8080
systemctl restart httpd

标签:httpd,虚拟主机,mcc,vim,conf,Apache,com
来源: https://blog.csdn.net/weixin_50344792/article/details/110144189