大网数据广告活动
服务器运维问答

Ubuntu Apache Subdomain not Displaying

· 52CE 我爱测

Using Ubuntu 18.04 I have my main website www.example.com , all the files are hosted in /var/www/html/ and it works as expected. I would now like to make a new website in a subdomain www.blog.example.com . I created a new dns rec…

问题原文

原帖链接 · TheOrdinaryGeek · CC BY-SA 4.0

Using Ubuntu 18.04

I have my main website www.example.com, all the files are hosted in /var/www/html/ and it works as expected. I would now like to make a new website in a subdomain www.blog.example.com.

I created a new dns record for the subdomain on my web host and have confirmed that it's working, when I ping www.blog.example.com I see the correct server IP address.

My directory structure looks like this;

  • /var/www/html (contains all files for www.example.com)
  • /var/www/blog.example.com (contains single index.html file for testing)

I have virtual hosts for each domain, contents are below;

/etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html
    <Directory /var/www/html/rascal.ac.uk>
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    Include conf-available/serve-cgi-bin.conf
</VirtualHost>

/etc/apache2/sites-available/blog.example.com.conf

<VirtualHost *:80>
    ServerName blog.example.com
    DocumentRoot /var/www/blog.example.com
    <Directory /var/www/blog.example.com/>
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

There's also /etc/apache2/sites-available/000-default.conf looks like this;

<VirtualHost *:80>
    DocumentRoot /var/www/html
    <Directory "/var/www/html">
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

All sites are enabled and I have reloaded / restarted apache multiple times.

When I visit www.example.com I see my main website, as expected. When I visit www.blog.example.com I see my main website also, I should be seeing the contents on my index.html file.

If I disable 000-default.conf both urls display the contents of blog.example.com, could the issue be in this file?

已采纳解答

原帖链接 · Andrew Schulman · CC BY-SA 4.0

You have

ServerName blog.example.com

but that's a different name from www.blog.example.com. So Apache doesn't use that VirtualHost. It falls back to the default VirtualHost instead. Adding

ServerAlias www.blog.example.com

should fix it. See ServerAlias.

Of course these are example names. If that doesn't fix it, please edit your question to show the real hostnames.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-02-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。