apache多域名配置
CentOS7Apache服务的安装与配置

CentOS7Apache服务的安装与配置⼀、Apache简介Apache 是⼀个知名的开源Web服务器。
早期的Apache服务器由Apache Group来维护,直到1999年6⽉Apache Group在美国德拉⽡市成⽴了⾮盈利性组织的公司,即Apache软件基⾦会(Apache Software Foundation,ASF)。
⽹站需要web服务器来架构,⽹页设计美⼯⼈员(flash,dreamweaver,firework,photoshop等),⽹页开发⼈员(php,.net,jsp等),⽹站建⽴好后,需要我们维护,优化,排错,架构延伸扩容等。
简单点说就是我们如果要浏览⼀个⽹页的话,基本上所有的⽹站都使⽤的是http协议来进⾏数据传输的!⾄于怎么样传输,我们做为运维来说就没有必要去深究了,那是做html前端开发⼈员要去考虑的事情!Apache由内核、标准模块和第三⽅提供的模块三个层次组成。
通常Apache在默认安装时,只安装图中的1、2两部分。
根据⽤户需要,⽤户可以通过修改配置去掉⼀些默认安装的标准模块;也可以通过修改配置安装⼀些默认不安装的模块。
同时,如果⽤户需要,也可以安装⼀些第三⽅提供的模块。
[此⽹站会有每⽉份的世界上⽹站使⽤的WEB服务器的使⽤率统计](https:///archives/category/web-server-survey/)Apache是世界上应⽤最⼴泛的web服务器之⼀[ Apache官⽹](/)⼆、CentOS下的Apache1. ⽹站分为两种静态⽹站:Apache,Nginx,html动态⽹站:php/perl/python,jsp(java), .net2. Apache服务概览软件包: httpd, httpd-devel, httpd-manual服务类型:由systemd启动的守护进程配置单元: /usr/lib/systemd/system/httpd.service守护进程: /usr/sbin/httpd端⼝: 80(http), 443(https)配置: /etc/httpd/Web⽂档: /var/www/html/Apache⽇志记录⽬录:/var/log/httpd/该⽬录下有两种⽂件:access_log # 记录客户端访问Apache的信息,⽐如客户端的iperror_log # 记录访问页⾯错误信息Apache服务启动的记录⽇志:/var/log/messages # 这个⽇志是系统的⼤集合3. 配置Apache服务器的准备⼯作系统平台: CentOS 7.3DHCP Server: 192.168.1.20第1步:服务器设置静态IP第2步:更改主机名,写/etc/hosts记录[root@Apache ~]# echo "192.168.1.20 Apache" >> /etc/hosts --往/etc/hosts添加ip和主机名[root@Apache ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.1.20 Apache第3步:关闭防⽕墙[root@Apache ~]# systemctl stop firewalld --临时关闭防⽕墙[root@Apache ~]# systemctl disable firewalld --永久关闭防⽕墙第4步:关闭selinux临时关闭:[root@Apache ~]# setenforce 0setenforce: SELinux is disabled永久关闭:[root@Apache ~]# vim /etc/selinux/configSELINUX=disabled # 将enforcing改为disabled[root@Apache ~]# reboot --重启系统永久⽣效三、Apache服务的搭建与配置1. 使⽤yum包安装Apache软件[root@Apache ~]# yum -y install httpd*[root@Apache ~]# rpm -qa | grep httpd --查看安装的http包httpd-manual-2.4.6-67.el7.centos.6.noarchhttpd-tools-2.4.6-67.el7.centos.6.x86_64httpd-2.4.6-67.el7.centos.6.x86_64httpd-devel-2.4.6-67.el7.centos.6.x86_64安装成功后,会产⽣下⾯两个⽂件/etc/httpd/conf/httpd.conf # 主配置⽂件/var/www/html # 默认⽹站家⽬录2. 认识配置⽂件⾥的主要参数[root@Apache ~]# vim /etc/httpd/conf/httpd.conf31 serverRoot "/etc/httpd" # 存放配置⽂件的⽬录42 Listen 80 # Apache服务监听端⼝66 User apache # ⼦进程的⽤户67 Group apache # ⼦进程的组86 ServerAdmin root@localhost # 设置管理员邮件地址119 DocumentRoot "/var/www/html" --⽹站家⽬录# 设置DocumentRoot指定⽬录的属性131 <Directory "/var/www/html"> # ⽹站容器开始标识144 Options Indexes FollowSymLinks # 找不到主页时,以⽬录的⽅式呈现,并允许链接到⽹站根⽬录以外151 AllowOverride None # none不使⽤.htaccess控制,all允许156 Require all granted # granted表⽰运⾏所有访问,denied表⽰拒绝所有访问157 </Directory> # 容器结束164 DirectoryIndex index.html # 定义主页⽂件,当访问到⽹站⽬录时如果有定义的主页⽂件,⽹站会⾃动访问316 AddDefaultCharset UTF-8 # 字符编码,如果中⽂的话,有可能需要改为gb2312或者gbk,因你的⽹站⽂件的默认编码⽽异3. 启动Apache⽹站[root@Apache ~]# systemctl start httpd.service[root@Apache ~]# lsof -i:80 --查看httpd服务是否启动COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEhttpd 20585 root 4u IPv6 402909 0t0 TCP *:http (LISTEN)httpd 20586 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)httpd 20587 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)httpd 20588 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)httpd 20589 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)httpd 20590 apache 4u IPv6 402909 0t0 TCP *:http (LISTEN)启动成功后使⽤浏览器:输⼊⾃⼰的IP地址会看到⼀个红帽的欢迎页⾯:[root@Apache ~]# firefox 192.168.1.20每次打开浏览器不是很⽅便,因此我们可以使⽤⽂本浏览器,⽅便测试。
apache下配置详解

Apache下关于虚拟主机的配置关于虚拟主机的概述:配置虚拟主机主要应用场景:多站点访问,一个服务器放置了多个站点服务。
主要的三种配置方法:1、基于IP 2、基于端口3、基于主机名步骤详解:1.虚拟主机使用的话,必须将中心服务关闭,在/etc/httpd/conf/httpd.conf文件里,将DocumentRoot一行注释掉来关闭中心服务。
2.可以在httpd.conf文件最后面直接添加虚拟主机的配置内容,也可以在/etc/httpd/conf.d/下面创建一个自己的配置文件,如:my.conf文件。
3.my.conf文件配置详解1)基于IP配置<VirtualHost 192.168.0.112:80>ServerName DocumentRoot "/www/"</VirtualHost><VirtualHost 192.168.0.113:80>ServerName DocumentRoot "/www/"</VirtualHost>以上2个虚拟主机配置的IP分别为192.168.0.113和192.168.0.112,端口同为80端口。
这样在浏览器分别输入不同的ip或域名时就会访问到不同的站点。
Tips:如果只有一块网卡可以同过ipaddr add 192.168.0.112 dev eth0来添加辅助ip。
如果是域名方式访问,还需要在windows和linux下的hosts文件里面自行添加域名解析。
关于辅助ip的设置:ipaddr add 192.168.0.112/24 dev eth0这是为网卡设置一个辅助ip。
假如一个网卡的ip地址为192.168.0.22,执行上面的命令,在用ifconfig 查看,会发现有两个ip地址,而192.168.0.22称为主地址(Primary IP address),而192.168.0.112称为辅助地址(secondary IP address),一块网卡是允许有多个IP地址的,所以就算再多添加几个secondary IP 也是合法行的。
apache_配置多个ip网站站点

附录:进一步的提示
一个简单示例
<VirtualHost www.abc.dom>
ServerAdmin webgirl@abc.dom
DocumentRoot /www/abc
</VirtualHost> webgirl@abc.dom
DocumentRoot /www/abc
本文档的涵义一言以蔽之就是:不要让Apache在解析配置文件的时候用到DNS。 如果Apache在解析配置文件时用到了DNS,您的服务器就会发生可靠性的问题(也可能根本无法启动), 或者遭致拒绝(偷窃)服务攻击(包括用户可以从其他用户那里偷窃点击)。
一个简单示例
拒绝服务
"main server"地址
基于域名的虚拟主机相对比较简单,因为我们只需要配置DNS服务器将每个主机名映射(CNAMES)到正确的IP地址,然后配置Apache HTTP服务器,令其辨识不同的主机名就可以了。基于域名的服务器也可以缓解IP地址(IPV4)不足的问题。这种方式下,各个虚拟主机共享同一份Apache,因此有CGI程序运行时,安全性也不高。
</VirtualHost> webmaster@
DocumentRoot /www/docs/
ServerName
ErrorLog logs/-error_log
TransferLog logs/-access_log
用Apache设置虚拟主机服务通常可以采用两种方案:基于IP地址的虚拟主机和基于主机名字的虚拟主机,下面我们分别介绍一下它们的实现方法以及优缺点。以便大家在具体的应用中能够选择最合适的实现方法。
Apache服务器配置

IN CNAME
IN CNAME
在上面的配置中,在提交的DNS正向配置文件以及反向配置文件中, 已经具有如下的A记录和PTR记录,分别为
www
利用httpd.conf我们可以对Apache服务器进行全局 配置、管理或预设服务器的参数定义、虚拟主机的设 置等。httpd.conf是一个文本文件,我们可以用vi编辑 工具进行修改。 httpd.conf文件主要分为三个部分: Section 1: Global Environment (全局变量) Section 2: 'Main' server configuration(主服务器配置) Section 3: Virtual Hosts(虚拟主机配置)
2.配置基于IP的虚拟主机 (1)基于IP虚拟主机的DNS配置 如果用户想要创建两个基于IP的虚拟主机和
这两个虚拟主机站点的共同域名是 。用户可以按照如下所示在DNS正向配置文 件中进行配置:
lth IN
A 192.168.15.212
3.Port 该参数用来指定Apache服务器的监听端口。一般来说,标准的HTTP
服务默认端口号是80,一ቤተ መጻሕፍቲ ባይዱ不要更改这个数值。本例为80端口:
Port 80 4.ServerAdmin
设置Apache服务器管理员的邮件地址。
5.ServerName 该参数使得用户可以自行设置主机名,以取代安装Apache服务器主机
7.MaxKeepAliveRequests 当使用保持连接(persistent connection)功能时,可以使用本参数决定
每次连接所能发出的要求数目的上限。如果此数值为0,则表示没有限制。 建议尽可能使用较高的数值,以充分发挥Apache的高性能,本例设置每次 连接所能发出的要求数目上限为100:
Apachehttpd.conf配置详解

Apachehttpd.conf配置详解常⽤配置指令说明1. ServerRoot:服务器的基础⽬录,⼀般来说它将包含conf/和logs/⼦⽬录,其它配置⽂件的相对路径即基于此⽬录。
默认为安装⽬录,不需更改。
语法:ServerRoot directory-path如: ServerRoot "/usr/local/apache-2.2.6"注意,此指令中的路径最后不要加 / 。
2. Listen:指定服务器监听的IP和端⼝。
默认情况下Apache会在所有IP地址上监听。
Listen是Apache2.0以后版本必须设置的指令,如果在配置⽂件中找不到这个指令,服务器将⽆法启动。
语法:Listen [IP-address:]portnumber [protocol]Listen指令指定服务器在那个端⼝或地址和端⼝的组合上监听接⼊请求。
如果只指定⼀个端⼝,服务器将在所有地址上监听该端⼝。
如果指定了地址和端⼝的组合,服务器将在指定地址的指定端⼝上监听。
可选的protocol参数在⼤多数情况下并不需要,若未指定该参数,则将为443端⼝使⽤默认的https 协议,为其它端⼝使⽤http协议。
使⽤多个Listen指令可以指定多个不同的监听端⼝和/或地址端⼝组合。
默认为:Listen 80如果让服务器接受80和8080端⼝上请求,可以这样设置:Listen 80Listen 8080如果让服务器在两个确定的地址端⼝组合上接受请求,可以这样设置:Listen 192.168.2.1:80Listen 192.168.2.2:8080如果使⽤IPV6地址,必须⽤⽅括号把IPV6地址括起来:Listen [2001:db8::a00:20ff:fea7:ccea]:803. LoadModule:加载特定的DSO模块。
Apache默认将已编译的DSO模块存放于4.1⽬录结构⼩节中所⽰的动态加载模块⽬录中。
Apache配置详解

Apache配置详解Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改。
主站点的配置(基本配置)(1) 基本配置:ServerRoot "/mnt/software/apache2" #你的apache软件安装的位置。
其它指定的目录如果没有指定绝对路径,则目录是相对于该目录。
PidFile logs/httpd.pid #第一个httpd进程(所有其他进程的父进程)的进程号文件位置。
Listen 80 #服务器监听的端口号。
ServerName :80 #主站点名称(网站的主机名)。
ServerAdmin admin@ #管理员的邮件地址。
DocumentRoot "/mnt/web/clusting" #主站点的网页存储位置。
以下是对主站点的目录进行访问控制:<Directory "/mnt/web/clusting">Options FollowSymLinksAllowOverride NoneOrder allow,denyAllow from all</Directory>在上面这段目录属性配置中,主要有下面的选项:Options:配置在特定目录使用哪些特性,常用的值和基本含义如下:ExecCGI: 在该目录下允许执行CGI脚本。
FollowSymLinks: 在该目录下允许文件系统使用符号连接。
Indexes: 当用户访问该目录时,如果用户找不到DirectoryIndex指定的主页文件(例如index.html),则返回该目录下的文件列表给用户。
SymLinksIfOwnerMatch: 当使用符号连接时,只有当符号连接的文件拥有者与实际文件的拥有者相同时才可以访问。
其它可用值和含义请参阅:/Apache/ApacheManual/mod/core.html#options AllowOverride:允许存在于.htaccess文件中的指令类型(.htaccess文件名是可以改变的,其文件名由AccessFileName指令决定):None: 当AllowOverride被设置为None时。
第14讲 Apache服务器配置

# /usr/local/apache2/bin/htpasswd -c /share/soft/.htpasswd sam
更改已设定的密码 # /usr/local/apache2/bin/htpasswd -m /share/soft/.htpasswd sam 第四步:重启服务,测试 通过浏览器输入http://服务器IP 或域名/soft
站点登录控制
第二步:在/share/soft目录下建立.htaccess 文件: AuthName “The LAMPBrother’s Docs” AuthType Basic AuthUserFile /share/soft/.htpasswd require valid-user
站点登录控制
站点登录控制
第一步:在Apache 配置文件 /usr/local/apache2/etc/httpd.conf中添加: Alias /soft “/share/soft” <Directory “/share/soft”> Options Indexes FollowSymLinks AllowOverride AuthConfig Order allow,deny Allow from all </Directory>
虚拟主机
修改Apache配置文件 /usr/local/apache2/etc/httpd.conf,设置行: etc/httpd/extra/httpd-vhosts.conf 取消注释,使其生效 解析虚拟机所需域名,可通过/etc/hosts设置。 编辑 /usr/local/apache2/etc/httpd/extra/httpdvhosts.conf NameVirtualHost 192.168.90.250
apaphe笔记

apache单ip多域名多目录配置自己的vps上放了别人的网站,那怎么样让自己的网站和别人的网站能同时被访问呢?需要使用apache的虚拟主机配置。
配置httpd.conf文件比如原来是这种只是指向一个目录的配置DocumentRoot "/opt/lampp/htdocs/ppe112"<Directory "/opt/lampp/htdocs/ppe112">## Possible values for the Options directive are "None", "All",# or any combination of:# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews## Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you.## The Options directive is both complicated and important. Please see# /docs/trunk/mod/core.html#options# for more information.##Options Indexes FollowSymLinks# XAMPPOptions Indexes FollowSymLinks ExecCGI Includes## AllowOverride controls what directives may be placed in .htaccess files.# It can be "All", "None", or any combination of the keywords:# Options FileInfo AuthConfig Limit##AllowOverride None# since XAMPP 1.4:AllowOverride All## Controls who can get stuff from this server.#Require all granted</Directory>把这段话变成<VirtualHost 198.199.107.XXX> #这里填主机的ip地址ServerName DocumentRoot "/opt/lampp/htdocs/AAA"<Directory "/opt/lampp/htdocs/AAA">## Possible values for the Options directive are "None", "All", # or any combination of:# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews## Note that "MultiViews"must be named *explicitly* --- "Options All"# doesn't give it to you.## The Options directive is both complicated and important. Please see# /docs/trunk/mod/core.html#options# for more information.##Options Indexes FollowSymLinks# XAMPPOptions Indexes FollowSymLinks ExecCGI Includes## AllowOverride controls what directives may be placedin .htaccess files.# It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit##AllowOverride None# since XAMPP 1.4:AllowOverride All## Controls who can get stuff from this server.#Require all granted</Directory></VirtualHost><VirtualHost 198.199.107.XXX> #这里填主机的ip地址ServerName DocumentRoot "/opt/lampp/htdocs/BBB"<Directory "/opt/lampp/htdocs/BBB">## Possible values for the Options directive are "None", "All", # or any combination of:# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews## Note that "MultiViews"must be named *explicitly* --- "Options All"# doesn't give it to you.## The Options directive is both complicated and important. Please see# /docs/trunk/mod/core.html#options# for more information.##Options Indexes FollowSymLinks# XAMPPOptions Indexes FollowSymLinks ExecCGI Includes## AllowOverride controls what directives may be placedin .htaccess files.# It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit##AllowOverride None# since XAMPP 1.4:AllowOverride All## Controls who can get stuff from this server. #Require all granted</Directory></VirtualHost>。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Ubuntu下apache配置
时间:2010.11.12 15:00-16:00
地点:大会议室
主讲:李建波
1.配置文件介绍
apache2.conf:apache的主配置文件,apache启动时会加载
其他文件。
httpd.conf:用户配置文件。
Ports.conf:端口配置文件
sites-enabled:存放虚拟主机配置文件的软连接
sites-available:存放虚拟主机配置文件
mods-enabled:存放模块软连接
mods-available :模块存放处
2.添加文件支持类型和添加首页文件
文件:apache2.conf
添加文件支持:
AddType application/x-httpd-php .php .htm .html 添加首页文件:
<IfModule dir_module>
DirectoryIndex index.htm index.html index.php
</IfModule>
3.添加多域名支持
单台机器上设置多个域名或主机名时,我们就要用到基于名称的虚拟主机。
主配置文件apache2.conf,在该文件中我们可以看到下列字段:
3.1设置步骤
修改文件:/etc/apache2/sites-available/default
修改内容:
分析设置语句:
●NameVirtualHost 10.39.6.59:80:表示我们要做的是一个基于
名称的虚拟主机,其IP为:192.168.0.173
●<VirtualHost 10.39.6.59> 和 </VirtualHost>:表示在其中的
是一个虚拟主机的配置
●ServerName :设置虚拟主机的域名
●DocumentRoot /var/www/han:设置该虚拟主机的主目录路径
●ErrorLog /var/log/apache2/error.log:设置该虚拟主机的出
错信息
3.2进一步说明
我们再添加一个虚拟主机站点 ,首先到
/etc/apache2/sites-available/ 目录中建立一个文件 li,编
辑该文件:
<VirtualHost 192.168.0.173:80>
ServerName
ServerAdmin ubuntu.firehare@
DocumentRoot "/var/www/li/"
ErrorLog "/var/log/apache2/edunuke_errors.log"
CustomLog "/var/log/apache2/edunuke_accesses.log" common </VirtualHost>
设置的具体含义同上面的相似,然后再运行命令:
sudo a2ensite li
这样,虚拟主机站点 就已经安装好了。
这时可以在
/etc/apache2/sites-enabled/ 目录中发现多了一个到
/etc/apache2/sites-available/li 的软链接。
然后注释ports.conf里这行字段:
否则apache重启时会包这样的错误:
报错原因:
NameVirtualHost语法错误,对于同一主机支持多个虚拟主机的情况,只需要命名一次NameVirtualHost。
接下来将 Apache2 重启来使虚拟主机站点运行起来
3.3注意事项
●NameVirtualHost字段的端口号不能忽略,否则起
apache时报错:VirtualHost 220.231.32.*:80 --
mixing * ports and non-* ports with a
NameVirtualHost address is not supported,
proceeding with undefined results——apache不
能起动。
●如果VirtualHost 字段的端口号确实,起apache时
报错:VirtualHost 220.231.32.28:0 -- mixing *
ports and non-* ports with a NameVirtualHost
address is not supported, proceeding with
undefined results——但apache可以启动,访问也
正常。