设置 Apache2 VirtualHost 以在本地运行多个站点 Ubuntu 16.04

设置 Apache2 VirtualHost 以在本地运行多个站点 Ubuntu 16.04

我有一个网站在 /var/www/html 中运行了一段时间。我正在启动一个新项目,想在本地运行另一个网站。

经过一些谷歌搜索后,我查看了之前的问题:https://ubuntuforums.org/showthread.php?t=1423044

我创建了一个/var/www名为的文件夹my-site,并克隆了暂时将使用的 git repo。

my-site.com.conf然后我添加了一个名为的文件/etc/apache2/sites-enabled。它看起来像这样:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
    ServerName www.my-site.com
DocumentRoot /var/www/my-site/
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/my-site/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

我添加了以下内容/etc/hosts

127.0.0.1       www.my-site.com

我还在中运行了以下命令/etc/apache2/sites-enabled

ln -sf /etc/apache2/sites-available/my-site.com /etc/apache2/sites-enabled/my-site.com

问题是,当我在浏览器中访问 时my-site.com,我被重定向到之前在 localhost 上运行的旧网站。我的旧网站不允许用户在登录之前访问任何地方,而且我似乎一直被重定向到登录页面。

我如何访问我的新网站并建立一个可以同时处理这两个项目的环境?

如果我需要提供更多信息,请告诉我。

谢谢!

答案1

您的配置文件内容看起来不错。问题在于文件名 - my-site.com。配置文件必须以 结尾.conf,否则它不会被IncludeOptional sites-enabled/*.conf主配置文件中的指令包含在 Apache 的配置中/etc/apache2/apache2.conf。因此:

# remove the previously created symbolic link
sudo rm /etc/apache2/sites-enabled/my-site.com

# rename the configuration file
sudo mv /etc/apache2/sites-available/my-site.com{,.conf}

# enable the configuration file (create the symlink); use 'a2dissite' to disable
sudo a2ensite my-site.com.conf

# restart Apache2:
sudo systemctl restart apache2.service

不要忘记清除浏览器的缓存并尝试访问您的网站。

相关内容