快乐的老鼠 发表于 2015-8-3 10:53:04

Ubuntu Apache 虚拟主机 VirtualHost 初级配置

  本文基于 Ubuntu 10.04 LTS + Apache 2.2.14

  
注: 不同的 Linux 发型版本配置 Apache 有所不同,这篇文章还介绍了 Redhat Enterprise Linux 和 Windows 下的相关配置:http://www.neoease.com/apache-virtual-host/
  
1. ls /etc/apache2/sites-available/ 目录,下面有 default 和 default-ssl 两个文件,default 是 http 虚拟主机服务的配置文件, default-ssl 是配置 https 服务使用的。复制一份 default 文件,并修改配置文件名,文件名必须与域名一致。这里测试的为文件名 google.com

  
注 : 文件名命名为域名,为了方便后面的操作和管理
  
2. 打开新建的配置文件,修改 DocumentRoot, ServerName 和对应的配置目录


#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名,需要跟 DNS 指向的域名一致
#

ServerAdmin shamrocker@google.com
DocumentRoot /var/www/google
ServerName google.com

Options FollowSymLinks
AllowOverride None


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128


  3. 通过 apache2ctl 检查下语法


sudo apache2ctl configtest
  4. 通过 a2ensite 激活虚拟主机配置


sudo a2ensite google.com
  5. ls /etc/apache2/sites-enabled/,你会发现所有激活的虚拟主机,可以通过 a2dissite 进行注销


sudo a2dissite google.com
  6. 重启 Apache 服务,激活虚拟主机


sudo /etc/init.d/apache2 restart
  注 : 习惯使用 sudo service apache2 restart
  参考:
http://www.neoease.com/apache-virtual-host/
http://hi.baidu.com/%C0%EE%B1%F8/blog/item/e825321297f71350f919b824.html
http://www.thetian.com/?p=37
页: [1]
查看完整版本: Ubuntu Apache 虚拟主机 VirtualHost 初级配置