需求:我的www.cronhub.com域名需要指向到我租的服务器上,该服务器的80端口已经运行一个论坛的服务了。
先敲入lsof -i :80,查看pid,确定apache装在哪COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 2761 root 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15599 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15600 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15605 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15607 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15608 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15611 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15617 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15618 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15627 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15632 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 2761 root 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15599 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15600 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15605 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15607 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15608 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15611 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15617 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15618 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15627 daemon 3u IPv6 10248 TCP *:http (LISTEN)
httpd 15632 daemon 3u IPv6 10248 TCP *:http (LISTEN)
这样就知道pid了
[root@localhost conf]# ps aux|grep 2761
root 2761 0.0 0.2 25848 9680 ? Ss Dec17 0:00/usr/local/httpd/bin/httpd-k start
root 15736 0.0 0.0 4988 688 pts/1 S+ 19:19 0:00 grep 2761
root 2761 0.0 0.2 25848 9680 ? Ss Dec17 0:00/usr/local/httpd/bin/httpd-k start
root 15736 0.0 0.0 4988 688 pts/1 S+ 19:19 0:00 grep 2761
这样去/usr/local/httpd/conf目录备份httpd.conf,以便修改失败回滚
编辑httpd.conf
加入虚拟主机配置
# default virtual host,这是保留默认的工大论坛的web server,其中DocumentRoot 和 ServerName 要设置为全局的。如果不设置这个VirtualHost ,就会直接浏览器上敲入ip也会定向到cronhub网站
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /usr/local/httpd/htdocs
ServerName xxx.xxx.xxx.xxx #这xxx就是填入ip
</VirtualHost>
# cronhub virtual host
<VirtualHost *:80>
DocumentRoot /home/mach/cron
ServerNamewww.cronhub.com
<VirtualHost *:80>
DocumentRoot /usr/local/httpd/htdocs
ServerName xxx.xxx.xxx.xxx #这xxx就是填入ip
</VirtualHost>
# cronhub virtual host
<VirtualHost *:80>
DocumentRoot /home/mach/cron
ServerNamewww.cronhub.com
# 设置别名
ServerAlias cronhub.com *.cronhub.com
ServerAlias cronhub.com *.cronhub.com
# 设置回滚日志
# 其中/usr/local/httpd/bin/rotatelogs是apache回滚日志的官方程序,/usr/local/httpd/logs/%Y_%m_%d_cronhub_access_log为日志文件名格式
CustomLog "| /usr/local/httpd/bin/rotatelogs /usr/local/httpd/logs/%Y_%m_%d_cronhub_access_log 86400 480" common
ErrorLog "| /usr/local/httpd/bin/rotatelogs /usr/local/httpd/logs/%Y_%m_%d_cronhub_error_log 86400 480"
CustomLog "| /usr/local/httpd/bin/rotatelogs /usr/local/httpd/logs/%Y_%m_%d_cronhub_access_log 86400 480" common
ErrorLog "| /usr/local/httpd/bin/rotatelogs /usr/local/httpd/logs/%Y_%m_%d_cronhub_error_log 86400 480"
#如果不设置这个会出现在浏览器敲入网址后:Apache: You don‘t have permission to access / on this server
<Directory "/home/mach/cron">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<Directory "/home/mach/cron">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
注意cronhub静态文件目录下/home/mach/cron的网站主页的文件改成index.html
否则配置还要加一句。
最后/usr/local/httpd/bin/httpd -t 检查配置
/usr/local/httpd/bin/httpd -k restart 重新启动apache载入配置
附录:官方网址教程
http://httpd.apache.org/docs/2.2/vhosts/name-based.html