Why does my domain name work whenever I ping it, but not in the browser?

Solution 1:

Ping answers to ICMP packets and web access use to listen on TCP/80 and/or TCP/443.

If you can ping your host, means that probably your firewall is allowing ICMP packets.

If you can't access your website or API from the browser, that can be caused by different things:

  1. First, double-check your firewall rules and confirm ports TCP 80 and/or 443 are allowed

You can also try this from the command-line:

curl http://yoursite.example
curl -I http://yoursite.example
  1. Look into your server's log and check if you can see your request reaching your server.

If so, it's probably because your application isn't answering properly, which means you'll have to debug it on the application layer

If it's not, it's probably by your firewall blocking HTTP/HTTPS packets or your webserver isn't up. So, try starting it

EDIT To have you apache server answering for specific domains, you'll need to add a VirtualHost config. To do this:

Create a file /etc/apache2/sites-available/yourdomain.conf with the following content:

<VirtualHost *:80>
    ServerName yourdomain.example
    ServerAlias www.yourdomain.example
    DocumentRoot /var/www/yourdomain/public/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

adjust its configuration (domain and directories) according to your site and add a symbolic link to sites0-enabled:

ln -s /etc/apache2/sites-available/yourdomain.conf /etc/apache2/sites-enabled/yourdomain.conf

Restart apache2.

I also suggest you reading this howto: How To Set Up Apache Virtual Hosts on Ubuntu 18.04

Solution 2:

You have a web site that answers correctly when called using its IP address, but not when called using its name.

This means your web server is not configured correctly: web servers choose the content to serve based on (amongst other things) the host name used in the HTTP request; that's how you can host different web sites the same system: same IP address, different names.

If the web server replies to unnamed request (those done using its IP address without any specific host name) but not to named ones, you should check its config and make sure it actually knows it should answer to that name.

Since you can actually see your website by pointing at its IP address, this rules out firewall or networking issues; this seems to be specifically a web server problem.