在前面文章已经介绍过Apache安装, Apache安装教程:https://www.mxsina.com/archives/53
安装目录/usr/local/apache2/
例子:
- 域名a.mxsina.com访问到a.mxsina.comweb1
- 域名b.mxsina.com访问到b.mxsina.comweb2
- 发布目录:/usr/local/apache2/htdocs/web1/ /usr/local/apache2/htdocs/web2/
- Apache配置文件httpd.conf目录/usr/local/apache2/conf/
apache多域名虚拟主机配置文件httpd-vhosts.conf目录/usr/local/apache2/conf/extra/
一、启动Apache服务使用ps查看下是否启动
/usr/local/apache2/bin/apachectl start
ps -ef |grep httpd
- 在这里我们看到有一条信息:(这里是因为没有找到配置文件中的ServerName造成我们将它改为localhost即可,不改也不会影响)
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
- 开启ServerName(用/ServerName搜索下保存退出即可)将这里#号去掉修改www.example.com为localhost
vim /usr/local/apache2/conf/httpd.conf
域名解析;
接下来编辑多域名虚拟主机配置文件:
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
配置例子参数:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2/htdocs/web1"
ServerName a.mxsina.com
ServerAlias aa.mxsina.com aaa.mxsian.com
ErrorLog "logs/web1error_log"
CustomLog "logs/web1access_log" common
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/local/apache2/htdocs/web2"
ServerName b.mxsina.com
ErrorLog "logs/web2error_log"
CustomLog "logs/b2access_log" common
详解:(#号即不生效)
- ServerAdmin(可填写你的邮箱)
- DocumentRoot(这里即我们的发布目录)
- ServerName(这里填写一个域名)
- ServerAlias(填写多域名,泛域名)
- ErrorLog(错误日志配置路径)
- CustomLog(访问日志配置路径)
进入Apache发布目录创建您的发布目录:
cd /usr/local/apache2/htdocs
mkdir {web1,web2}
此时可以将源码上传到相应的目录即可;(我这里将默认发布目录的页面复制过来测试修改下)
cp index.html web1/
cp index.html web2/
修改Apache配置文件(搜索/vhosts把#去掉)
vim /usr/local/apache2/conf/httpd.conf
修改完后检查下Apache配置文件:(返回Syntax OK表示配置无误)
/usr/local/apache2/bin/apachectl -t
重启Apache服务:
/usr/local/apache2/bin/apachectl restart
最后通过a.mxsian.com,aa.mxsina.com和aaa.mxsina.com我们发布的web1;通过b.mxsina.com访问到web2;
(在b.mxsina.com中也可添加ServerAlias多个域名)
本文作者为SKY RING,转载请注明。