简体   繁体   English

在单个Apache实例上配置多个网站

[英]Configuring multiple websites on a single Apache instance

I am looking for a way to host multiple websites on a single server. 我正在寻找一种在单个服务器上托管多个网站的方法。 Currently I have used reverse proxy to host two of the websites using the following method: 目前,我已经使用反向代理通过以下方法托管两个网站:

I have a php site in /var/www/html folder and a nodejs app running on localhost:3015. 我在/ var / www / html文件夹中有一个php网站,并在localhost:3015上运行了一个nodejs应用程序。 My apache2 config is like following:- 我的apache2配置如下:

<VirtualHost *:80>
        ServerName site1.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

<VirtualHost *:80>
        ProxyPreserveHost On
        ProxyPass / http://localhost:3015/
        ProxyPassReverse / http://localhost:3015/
        ServerName site2.example.com
</VirtualHost>

Now I further want to host few old websites at: 现在,我进一步希望在以下位置托管一些旧网站:

site1.example.com/archives/2014, 13 and so on site1.example.com/archives/2014、13等

site2.example.com/archives/2014, 13 and so on. site2.example.com/archives/2014、13等。

In case of site1.example.com I can use alias as the site hosted is a php site. 如果是site1.example.com,我可以使用别名,因为托管的站点是一个php站点。 In the second case where I am using reverse proxy what will be the best way to host a php site. 在第二种情况下,我使用反向代理,这将是托管php站点的最佳方法。

Also, please suggest a way where new sites can be easily added and the old sites can be moved to archived folder. 另外,请提出一种可以轻松添加新站点并将旧站点移动到存档文件夹的方法。 These site might be on django, ROR and so on. 这些站点可能位于django,ROR等上。

Is this even possible? 这有可能吗?

This may not be quite what you are asking for, but give this a try. 这可能不是您所要求的,但是请尝试一下。 It's a variation on the Apache config I use in a dev VM, which is set up for wildcard domain hosting. 这是我在开发VM中使用的Apache配置的变体,该VM是为通配符域托管而设置的。 I haven't tested this config specifically, but you should be able to tweak it to suit your needs. 我没有专门测试过此配置,但是您应该可以对其进行调整以适合您的需求。

This essentially tells Apache how to find sites such as: 从本质上讲,这告诉Apache如何查找以下站点:

  • site1.example.com => /var/www/html/site1/public_html
  • site2.example.com => /var/www/html/site1/public_html
  • 2014.archive.site1.example.com => /var/www/html/site1/public_html/archives/2014

     <VirtualHost *:80> ServerName example.com ServerAlias *.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html <Directory /> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> <Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> DirectoryIndex index.html index.php RewriteEngine on RewriteMap lowercase int:tolower # *.example.com RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-zA-Z0-9-]+\\.example\\.com$ RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C] RewriteRule ^([a-zA-Z0-9-]+)\\.example\\.com/(.*) /var/www/html/$1/public_html/$2 # *.archive.*.example.com RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-zA-Z0-9-]+\\.archive\\.[a-zA-Z0-9-]+\\.example\\.com$ RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C] RewriteRule ^([a-zA-Z0-9-]+)\\.archive\\.([a-zA-Z0-9-]+)\\.example\\.com/(.*) /var/www/html/$2/public_html/archives/$1/$3 </VirtualHost> 

If this isn't what you were asking for, let me know. 如果这不是您要的,请告诉我。

I finally figured out a way to do the same. 我终于想出了一种方法来做同样的事情。 What you need to do is host all the sites at different port: 您需要做的是将所有站点托管在不同的端口:

site1 => 3015
site2 => 4015

site3 => 3014
site4 => 4014

and so on.

Now you can configure your default.conf in apache2 as following: 现在,您可以在apache2中配置default.conf,如下所示:

<VirtualHost *:80>    
        ProxyPreserveHost On

        ProxyPass /archive/2014/ http://localhost:3014/
        ProxyPassReverse /archive/2014/ http://localhost:3014/

        ProxyPass / http://localhost:3015/
        ProxyPassReverse / http://localhost:3015/

        ServerName site1.example.com    
</VirtualHost>
<VirtualHost *:80>
        ProxyPreserveHost On

        ProxyPass /archive/2014/ http://localhost:4014/
        ProxyPassReverse /archive/2014/ http://localhost:4014/

        ProxyPass / http://localhost:4015/
        ProxyPassReverse / http://localhost:4015/

        ServerName site2.example.com
</VirtualHost>

This will work for sites on any platform, currently my site3 and site4 are php and site1 and site2 are node and django respectively. 这将适用于任何平台上的站点,当前我的site3和site4是php,site1和site2分别是node和django。 You might need to play with urls and links a little to make everything work perfectly. 您可能需要稍微使用URL和链接,才能使所有功能正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM