您的位置:首页 > 运维架构 > Apache

Linux 配置Apache服务器 下(虚拟主机,排错)

2014-08-20 17:28 633 查看
Linux 配置Apache服务器 下(虚拟主机,排错)三.创建虚拟主机
配置域名解析机文件站点文件

配置域名解析(DNS或Hosts)保证能够正确地解析oa.abc.local、crm.abc.local。
在本示例中,我以hosts为例。
通过记事本打开C:\Windows\System32\drivers\etc\hosts进行修改。

#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
#127.0.0.1 localhost
#::1 localhost
192.168.188.22 oa.abc.local
192.168.188.22 crm.abc.local

[root@li ~]# mkdir /opt/crm
[root@li ~]# echo 'CRM test page' >/opt/crm/index.html
[root@li ~]# mkdir /opt/oa
[root@li ~]# echo 'OA test page' >/opt/oa/index.html
[root@li ~]# cd /etc/httpd/
[root@li httpd]# ls
conf conf.d logs modules run

[root@li httpd]# mkdir vhost-conf.d
[root@li httpd]# echo "Include vhost-conf.d/*.conf" >>conf/httpd.conf
配置基于端口的虚拟主机

[root@li httpd]# vi /etc/httpd/vhost-conf.d/vhost-ip.conf
Listen 8002
Listen 8002
<VirtualHost *:8001>
DocumentRoot /opt/crm/
</VirtualHost>

<VirtualHost *:8002>
DocumentRoot /opt/oa/
</VirtualHost>
[root@li httpd]# apachectl configtest
Syntax OK
[root@li httpd]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
在浏览器中输入:http://192.168.188.22:8001/ 进行访问



在浏览器中输入:http://192.168.188.22:8002/ 进行访问



测试成功后,删除新添加的配置文件,恢复实验环境

[root@li ~]# rm -i /etc/httpd/vhost-conf.d/vhost-ip.conf
rm: remove regular file `/etc/httpd/vhost-conf.d/vhost-ip.conf'? y

配置基于域名的虚拟主机:

[root@li ~]# vi /etc/httpd/vhost-conf.d/vhost-name.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName crm.abc.local
DocumentRoot /opt/crm/
</VirtualHost>
<VirtualHost *:80>
ServerName oa.abc.local
DocumentRoot /opt/oa/
</VirtualHost>
[root@li ~]# apachectl configtest
[root@li ~]# service httpd restart
进行测试http://crm.abc.local/



http://oa.abc.local/



测试成功后,删除新添加的配置文件,恢复实验环境

[root@li ~]# rm -i /etc/httpd/vhost-conf.d/vhost-name.conf
rm: remove regular file `/etc/httpd/vhost-conf.d/vhost-name.conf'? y

四.排错
Apache启动或检查配置文件时有警告信息

[root@li ~]# apachectl configtest
编辑httpd.conf文件
[root@li ~]# vi /etc/httpd/conf/httpd.conf
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work. See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
#ServerName www.example.com:80
ServerName localhost:80
#

重新启动Apache,完成

本期《配置Apache服务器》完结!
感谢您的来临!轻轻的赞一下,何乐而不为呢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息