leo
leo

Reputation: 535

Apache2 ServerName and ServerAlias not working

On a nearly fresh Ubuntu 20.04 LTS computer, I would like to set up a virtual host on my local machine. So I created a index.html under /var/www/test/ with the following content: you have entered a test page

I have set up a test.conf file under /etc/apache2/sites-available/

with the following content:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/test/
    ServerName zhihu.com
    ServerAlias www.zhihu.com
    <Directory /var/www/test/>
        Options Indexes FollowSymLinks
        AllowOverride All 
        Require all granted     
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The localhost is working:localhost is working

/etc/hosts config is probably working as indicated by ping

ping zhihu.com
PING zhihu.com (127.0.1.1) 56(84) bytes of data.
64 bytes from xxx (127.0.1.1): icmp_seq=1 ttl=64 time=0.045 ms

but browser cannot bring me to the domain which should now be hosted in /var/www/test/.

enter image description here

I have also a2ensite test.conf and a2dissite 000-default.conf and service apache2 reload

So I think the only possible place for error to occur is ServerName and ServerAlias. Why are they not working?

Upvotes: 0

Views: 3751

Answers (3)

MediaFormat
MediaFormat

Reputation: 377

Your UseCanonicalName setting may be Off by default.

UseCanonicalName: Determines how Apache constructs self-referencing URLs and the SERVER_NAME and SERVER_PORT variables. When set “Off”, Apache will use the Hostname and Port supplied by the client. When set “On”, Apache will use the value of the ServerName directive.

If you are using a tunnel (ngrok), the hostname supplied by the client may differ.

I was able to override the setting in httpd-default.conf

UseCanonicalName On

After this ServerName set in the vhosts file works properly.

Upvotes: 0

yebowhatsay
yebowhatsay

Reputation: 341

Ensure that the ssl certificate for the site includes both example.com and www.example.com and ServerAlias is set to www.example.com

Upvotes: 0

Pandurang
Pandurang

Reputation: 1741

Could you please enable VirtualHost using a2ensite and access site in incognito mode.

Upvotes: 0

Related Questions