使用 Apache2 在单个 Ubuntu Server 15.10 上运行多个网站

使用 Apache2 在单个 Ubuntu Server 15.10 上运行多个网站

我正在使用 Ubuntu Server 15.10

我指的是使用 apache2 在单个 ubuntu 服务器上运行多个网站博客。

问题

  1. 看来我的 apache2 目录现在/var/www/html/无法正确遵循上述文档

我的虚拟主机

  1. 默认服务器
    Address Any
    Port Any Server Name Automatic
    Document Root Automatic

  2. 虚拟服务器
    Address website1.com
    Port 80 Server Name Automatic
    Document Root /var/www/html/website1.com

  3. 虚拟服务器
    Address website2.com
    Port 80 Server Name Automatic
    Document Root /var/www/html/website2.com

但是当我尝试访问我的时,website1.com它会转到正确的虚拟主机,但我website2.com只是重定向到website1.com

vi /var/www/html/index.php
require( dirname( __FILE__ ) . '/website1.com/wp-blog-header.php' );

看起来它只是指向website1.com。如果我删除上面的行,那么所有网站都无法工作。所以也许我的两个虚拟主机都无法工作。

难道1. Default Server Virtual Hosts这只是我超车了。

请帮忙

***Content of conf files from /etc/apache2/sites-enabled***  <br/>
vi 000-default.conf   <br/>

现在是空的,我去了webmin并且看到了重复的条目website1.com所以我删除了多余的一个现在我只有如下所示的。

// added this just now after the first comment and enabled it  <br/>
vi website1.com.conf
<VirtualHost website1.com:80>  <br/>
ServerAdmin webmaster@localhost  <br/>
DocumentRoot /var/www/html/website1.com  <br/>
ErrorLog ${APACHE_LOG_DIR}/error.log  <br/>
CustomLog ${APACHE_LOG_DIR}/access.log combined  <br/>
</VirtualHost>  <br/>

vi webiste2.com.conf
<VirtualHost website2.com:80>  <br/>
ServerAdmin webmaster@localhost  <br/>
DocumentRoot /var/www/html/website2.com  <br/>
ErrorLog ${APACHE_LOG_DIR}/error.log  <br/>
CustomLog ${APACHE_LOG_DIR}/access.log combined  <br/>
</VirtualHost>

答案1

通常,Apache 上的站点是通过目录*.conf中的文件来处理的/etc/apache2/sites-available。以下是我的服务器的一个示例,它托管www.brownvpn.tkwww.browntech.tkwww.techyteen.tk以及其他一些。

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.

        ServerName browntech.tk
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/tech
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
</VirtualHost>

这些注释非常有用。每个文件都需要与此大致相同。但是,Rewrite如果您未启用 SSL,或者想要允许 HTTP 和 HTTPS 流量,则可能需要删除这些行。我们在顶部看到最重要的两行。服务器名称文档根目录。 这服务器名称字段应该是指向该域文档根目录路径。因此,您将需要两个配置文件(如果您想要 www.domain1.com 以及 domain1.com,则需要四个)。

cd /etc/apache2/sites-available/
touch site1.conf
touch site2.conf

site1.conf然后,在您喜欢的编辑器中,复制并粘贴我的示例配置文件,对和分别进行两处编辑(如果算上将其设置为电子邮件则为三处,如果删除那些重写行则为四处)site2.conf

sudo a2ensite site1 site2
sudo service apache2 restart

这将启用这两个站点并相应地重定向 URL。

答案2

答案3

我假设您已经将A域名记录更改为服务器的 IP 地址。现在,假设您有两个网站例子一例子二。默认网站是例子一。因此,默认主机路径为例子一将会/var/www/html。然后为了主持例子二创建名为的文件夹例子二里面/var/www/或相反html文件夹。

因此,现在您有两个文件夹名为html例子二里面/var/www

Apache 通过以下方式处理网站配置文件文件内部/etc/apache2/站点可用/。因此,启动您的终端并输入以下命令。sudo如果需要,请使用

cd /etc/apache2/sites-available/
touch exampletwo.conf

现在,打开你最喜欢的编辑器或使用 nano 来更改示例two.conf到 -

    <VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.exampletwo.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/exampletwo.in

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

创建新的配置文件文件后,我们将启用新网站。

sudo a2ensite exampletwo.conf
sudo service apache2 restart

Viola,您已经在同一个 ubuntu 服务器中托管了其他网站。

相关内容