文章目录:
- 1、如何在Debian 10上安装Apache并设置虚拟主机
- 2、什么是apache虚拟主机
- 3、如何配置 Apache 的虚拟主机
- 4、如何配置apache虚拟主机的实例小结
如何在Debian 10上安装Apache并设置虚拟主机
Apache HTTP Server 是目前应用最多的 web server 服务软件之一,Apache提供了许多强大的功能,而且可以通过其他模块进行扩展。Apache HTTP Server是一个开源的跨平台的服务器软件,目前大部分网站都采用Apache作为网站HTTP服务器。
在本教程中,我们将向你介绍如何在Debian 10中安装Apache并配置虚拟主机。
首先你得有一台安装了Debian的服务器,这样便于你在上面进行演练操作。如果你没有可以考虑自己在虚拟机中安装一个Debian 10。当然,我也推荐您购买一台 阿里云VPS 或者 腾讯云VPS 虚拟主机,如果你更喜欢国外服务器,推荐你尝试 Vultr上的VPS ,注册即送$50美元体验,性价比非常高。
当然有主机了,为安全考虑不建议使用root账号,建议创建一个具有 sudo权限的用户 。
同时,你的Debian已经安装了PHP,如果没有安装可以参考「 如何在Debian 10安装PHP 」教程。
Apache的安装包在Debian的默认源中提供了,所以只需要执行安装命令即可安装。执行安装Apache命令之前我们需要先更新一下系统,命令如下:
安装完成后,apache将默认自动运行,我们通过以下命令查看apache服务状态。
输出命令类似如下:
开启防火墙可以加强服务器的安全性,我们通过防火墙只开放需要访问的端口。可以通过防火墙管理软件 ufw 或者 nftables 进行管理。
如果系统没有安装 ufw 或者 nftables ,可以通过如下命令直接安装。
UFW 如果需要开启 (HTTP)80 或者 (HTTPS)443 端口,可以通过执行如下命令开启:
nftables 如果需要开启 (HTTP)80 或者 (HTTPS)443 端口,可以通过执行如下命令开启:
这个更建议使用 UFW 作为防火墙管理软件,相对来说操作更简单。
Apache安装完成后,需要校验一下Apache是否正常工作。校验方法是打开浏览器,在地址栏中输入 ,如果能正常显示类似如下界面,说明Apache安装成功运行正常。
Apache安装完成后,默认的安装配置文件路径如下:
我们在 /var/www 目录中创建一个网站目录,例如 example.com 。默认情况,目录结构如下:
可以通过以下命令创建:
在该目录下我们创建一个 index.php 文件,文件内容如下:
首先,进入 /etc/apache2/sites-available 目录,创建配置文件 example.com.conf 文件,并在其中添加如下内容。
您可以根据需要命名虚拟主机配置文件,但是建议使用域名作为配置文件的名称。
要启用新的虚拟主机文件,请创建一个从虚拟主机文件到启用站点的目录的符号链接,该链接在启动过程中由Apache读取。
在Debian系统中,您可以使用名为 a2ensite 的帮助程序脚本来启用虚拟主机,命令如下:
当然你也可以选择手动创建符号链接,如下所示:
启用配置后,通过键入以下命令测试语法是否正确:
如果一切正常,会输出如下内容:
重启服务,使配置生效,命令如下:
在浏览器地址栏中输入 ,正常情况下你将看到如下输出:
至此,本教程交接完毕。
通过本教程,你将学会如何在Debian 10上安装Apache并设置虚拟主机,如果你有自己的的Debian 10主机不妨按照教程进行安装尝试,如果没有自己的Debian自己建议可以安装一台Debian10虚拟主机或者去 阿里云 或者 腾讯云 或者 Vultr 购买一台自己的VPS服务器。
什么是apache虚拟主机
apache是开源的web服务软件之一,也是现在开源系统比如centos上安装web服务常见软件;
虚拟主机是提供网站服务的集成环境,比如:linux Apache+php+mysq? LAMP
就是常说的建站开通的虚拟空间;
所以Apache服务器只是虚拟空间的一种架构。
深入理解Apache虚拟主机服务
概论:
访问web服务,本质上看(从协议上)是访问某个IP的主机上的某个端口(默认是80)
通常需要通过访问不同的域名或者端口实现对不同网站的访问(具体到服务器里就是不同目录),这个时
候就需要设置虚拟主机(VirtualHost).通常分为这样3种:
基于域名,基于端口,基于IP,以及它们的混合。
如何配置 Apache 的虚拟主机
怎样配置Apache虚拟主机
分步阅读
怎样利用Apache在一台Linux服务器上面运行多个网站,最简单快速的方法。
工具/原料
一台Linux服务器
方法/步骤
安装Apache软件包
yum -y install httpd
(这里只实现Apache的虚拟主机功能,LAMP架构还需要安装php和mysqld-server等软件包)
打开Apache的主配置文件
#vim /etc/httpd/conf/httpd.conf
取消“#NameVirtualHost *:8”行首#注释
NameVirtualHost *:8
添加
VirtualHost *:80
ServerAdmin webmaster@example.com
DocumentRoot /var/www/html/web1
ServerName web1.example.com
ErrorLog logs/web1.example.com-error_log
CustomLog logs/web1.example.com-access_log common
/VirtualHost
VirtualHost *:80
ServerAdmin webmaster@example.com
DocumentRoot /var/www/html/web2
ServerName web2.example.com
ErrorLog logs/web2.example.com-error_log
CustomLog logs/web2.example.com-access_log common
/VirtualHost
ServerAdmin是网站管理员的邮箱
DocumentRoot是网站存放的路径
ServerName是你的网站域名
ErrorLog、CustomLog是日志文件
添加多个网站的时候DocumentRoot和ServerName必须不同
启动Apache服务
/etc/init.d/httpd start
chkconfig httpd on
在防火墙中添加80端口
#grep 80 /etc/sysconfig/iptables
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
5
在域名管理后台添加域名解析记录,然后访问域名就能跳转到对应的网站上面了。
注意事项
虚拟主机的DocumentRoot参数不能和Apache的默认站点目录相同
如何配置apache虚拟主机的实例小结
如果你是第一次配置apache虚拟主机,那么通过阅读这篇文章你将会了解到如何实现apache虚拟主机配置。其实要配置好一台虚拟主机没有想象中那么难
1、基于ip地址的虚拟主机
复制代码代码如下:
Listen 80
VirtualHost 172.20.30.40
DocumentRoot /home/httpd/html1
ServerName
ErrorLog /usr/local/apache/logs/error1_log
CustomLog /usr/local/apache/logs/access1_log combined
/VirtualHost
VirtualHost 172.20.30.50
DocumentRoot /home/httpd/html2
ServerName
ErrorLog /usr/local/apache/logs/error2_log
CustomLog /usr/local/apache/logs/access2_log combined
/VirtualHost
2、基于IP 和多端口的虚拟主机配置
复制代码代码如下:
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
VirtualHost 172.20.30.40:80
DocumentRoot /www/example1-80
ServerName
/VirtualHost
VirtualHost 172.20.30.40:8080
DocumentRoot /www/example1-8080
ServerName
/VirtualHost
VirtualHost 172.20.30.50:80
DocumentRoot /www/example2-80
ServerName
/VirtualHost
VirtualHost 172.20.30.50:8080
DocumentRoot /www/example2-8080
ServerName
/VirtualHost
3、单个IP 地址的服务器上基于域名的虚拟主机配置
复制代码代码如下:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
VirtualHost *:80
DocumentRoot /www/example1
ServerName
ServerAlias example1.com. *.example1.com
# Other directives here
/VirtualHost
VirtualHost *:80
DocumentRoot /www/example2
ServerName
# Other directives here
/VirtualHost
如果您感觉上面的文章还不够详细可以看下下面的文章:
实验目标:在apache实现基于域名的虚拟主机
实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21
实验前准备:
1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 –
复制代码代码如下:
127.0.0.1 test1.net
127.0.0.1 test2.net
如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。
2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 –
apache/htdocs1/test1.net – 放置test1.net网站内容
apache/htdocs1/test2.net – 放置test2.net网站内容
在这两个目录中各新建hello world一网页 index.html,内容 –
复制代码代码如下:
HTML
HEAD/HEAD
BODY
H1hello~, 这是[对应的网站名,用以区别].net/H1/BODY
/HTML
实验步骤:
1. 找到apache/conf/httpd.conf, 将其中的
ServerAdmin
ServerName
DocumentRoot
注释掉。
2. 在httpd.conf中,找到行
Include “conf/extra/httpd-vhosts.conf”
如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]
3. 这个httpd-vhosts.conf文件格式基本如下 –
复制代码代码如下:
#blah-blah
NameVirtualHost *:80
#blah-blah
#blah-blah
VirtualHost *:80
ServerAdmin XXXXXXXX
DocumentRoot “XXXXXXXX”
ServerName XXXXXXX
ServerAlias XXXXXX
ErrorLog “logs/XXXXXX-error.log”
CustomLog “logs/XXXXXXX-error.log” combined
/VirtualHost
需要修改的,就是VirtualHost中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个VirtualHost:
复制代码代码如下:
VirtualHost *:80
ServerAdmin adm@test1.net
DocumentRoot “C:/xampp/htdocs1/test1.net”
ServerName test1.net
ServerAlias
ErrorLog “logs/test1-error.log”
CustomLog “logs/test1-access.log” combined
Directory “C:/xampp/htdocs1/test1.net”
order allow,deny
allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin adm@test2.net
DocumentRoot “C:/xampp/htdocs1/test2.net”
ServerName test2.net
ServerAlias
ErrorLog “logs/test1-error.log”
CustomLog “logs/test1-access.log” combined
Directory “C:/xampp/htdocs1/test2.net”
order allow,deny
allow from all
/Directory
/VirtualHost
注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。
4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 –
复制代码代码如下:
VirtualHost *:80
ServerAdmin adm@localhost
DocumentRoot “C:/xampp/htdocs”
ServerName localhost
ErrorLog “logs/localhost-error.log”
CustomLog “logs/localhost-access.log” combined
Directory “C:/xampp/htdocs”
order allow,deny
allow from all
/Directory
/VirtualHost
为了避免出错,把它放置在第一个Virtualhost位置。
至此,apache基于域名的虚拟主机配置完成。可以通过访问XAMPP欢迎页,通过和访问各自的主页。
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn’t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# URL:;
# for further details before you try to setup virtual hosts.
#
# You may use the command line option ‘-S’ to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any VirtualHost block.
#
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot “E:/skydao/apache2/htdocs”
ServerName localhost
ServerAlias
ErrorLog “logs/localhost-error.log”
CustomLog “logs/localhost-access.log” combined
Directory “E:/skydao/apache2/htdocs”
order allow,deny
allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot “E:/skydao/apache2/htdocs/project1”
ServerName project1.com
ServerAlias
ErrorLog “logs/project1-error.log”
CustomLog “logs/project1-access.log” combined
Directory “E:/skydao/apache2/htdocs/project1”
order allow,deny
allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot “E:/skydao/apache2/htdocs/zendTest/public”
ServerName zendTest.com
ServerAlias
DirectoryIndex index.php
Directory /
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot “E:/skydao/apache2/htdocs/testRewrite”
ServerName testRewrite.com
ServerAlias
# DirectoryIndex index.php
Directory /
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot “E:/skydao/apache2/htdocs/test”
ServerName test.com
ServerAlias
ErrorLog “logs/zendTest-error.log”
CustomLog “logs/zendTest-access.log” combined
Directory “E:/skydao/apache2/htdocs/test”
order allow,deny
allow from all
/Directory
/VirtualHost
apache2.2虚拟主机的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于apache基于ip虚拟主机、apache2.2虚拟主机的信息别忘了在本站进行查找喔。
关于apache2.2虚拟主机(apache 虚拟主机)的内容来源于互联网引用、聚合而成,若侵权,请联系我们删除!九八云,优质云服务器提供商。