状态:只有一个服务器,需要多个站点,比如博客、文件、Android接口等。
最开始我的做法是用子文件夹来区分站点,所以本站的地址都是这样的http://hylstudio.cn/xxx
后来曾老师提醒可以使用下一级域名来区分不同的站点,于是研究了下。
首先,更改DNS,把需要的域名解析到这台服务器,然后更改apache配置如下
进入apache配置目录/etc/apache2其中目录结构如下
├── apache2.conf 主配置文件
├── conf-available 可用配置文件
├── conf-enabled 启用的配置文件
├── mods-available 可用的模块
├── mods-enabled 启用的模块
├── sites-available 可用的站点
└── sites-enabled 启用的站点
各个文件夹的功能如上,规则是在available中创建真实的配置、然后在enabled文件夹下创建软连接(ln -s命令)。
默认状态下sites-enabled下只有一个000-default,被我改成了999-default.conf,把内容改成下面这样
0 1 2 3 4 5 6 7 8 9 |
<VirtualHost *:80> ServerAdmin master@hylstudio.cn DocumentRoot /xxx/xxxx/xxxxx/wordpress ServerName hylstudio.cn ServerAlias *.hylstudio.cn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
然后复制一份这个文件为001-wordpress.conf。内容如下
0 1 2 3 4 5 6 7 8 9 10 |
<VirtualHost *:80> ServerAdmin master@hylstudio.cn #这里写wordpress的真实地址,主目录 DocumentRoot /xxx/xxxx/xxxxx/wordpress ServerName blog.hylstudio.cn ServerAlias blog.hylstudio.cn ErrorLog ${APACHE_LOG_DIR}/wordpress/error.log CustomLog ${APACHE_LOG_DIR}/wordpress/access.log combined </VirtualHost> |
其他的同理,只需要更改ServerName、Alias、DocumentRoot即可。
需要说一下的是tomcat,因为tomcat在其他端口,需要做反向代理。内容如下
0 1 2 3 4 5 6 7 8 9 10 11 |
<VirtualHost *:80> ServerAdmin master@hylstudio.cn DocumentRoot /xxx/xxxx/tomcatX/webapps #这里写tomcat真实路径 ServerName tomcat.hylstudio.cn ServerAlias tomcat.hylstudio.cn ErrorLog ${APACHE_LOG_DIR}/tomcat/error.log CustomLog ${APACHE_LOG_DIR}/tomcat/access.log combined ProxyPass / http://127.0.0.1:XXXX/ #这里写tomcat实际监听的端口 ProxyPassReverse / http://127.0.0.1:XXXX/ </VirtualHost> |
同时需要启用proxy模块
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/proxy.load
这样做的好处是对外不暴露tomcat真实端口。
这样,就实现了通过不同域名访问不同站点。
配置default虚拟主机,rewrite含义为我没定义过的虚拟主机都会302重定向到blog.hylstudio.cn
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<VirtualHost *:80> ServerAdmin master@hylstudio.cn DocumentRoot /var/www/html/wordpress ServerName hylstudio.cn ServerAlias *.hylstudio.cn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)hylstudio.cn [NC] RewriteRule ^(.*) http://blog.hylstudio.cn/ [L] </VirtualHost> |
0 Comments