How to Fix Apache Connection Refused on Ubuntu 22.04


  1. The Root Cause On Ubuntu 22.04, “Apache Connection Refused” most commonly indicates that the Apache web server process is not running, or that the Uncomplicated Firewall (UFW) is blocking incoming connections to the HTTP/HTTPS ports. This prevents any client from establishing a TCP connection with the server.

  2. Quick Fix (CLI)

    # 1. Start the Apache service
    sudo systemctl start apache2
    
    # 2. Enable Apache to start automatically on boot
    sudo systemctl enable apache2
    
    # 3. Allow Apache traffic through UFW (Ubuntu's default firewall)
    #    'Apache Full' profile opens both HTTP (port 80) and HTTPS (port 443)
    sudo ufw allow 'Apache Full'
    
    # 4. Ensure UFW is active (if not already)
    sudo ufw --force enable
  3. Configuration Check If the service is running and UFW is configured, verify that Apache is configured to listen on the correct network interfaces and ports.

    File to edit: /etc/apache2/ports.conf

    Ensure the following lines are present and uncommented:

    Listen 80
    <IfModule mod_ssl.c>
        Listen 443
    </IfModule>

    After any changes to configuration files, restart Apache:

    sudo systemctl restart apache2
  4. Verification

    # 1. Check the Apache service status
    sudo systemctl status apache2
    # Expected output: "active (running)"
    
    # 2. Verify Apache is listening on the expected ports (80 and/or 443)
    sudo ss -tulpn | grep LISTEN | grep 80 || sudo ss -tulpn | grep LISTEN | grep 443
    # Expected output: Lines showing 'LISTEN' on ports 80 and/or 443, typically by 'apache2' or 'httpd' process.
    
    # 3. Test a local HTTP connection
    curl http://localhost
    # Expected output: HTML content of your default Apache page or website.