Install and Configure Apache2 in Ubuntu 24

Here’s a detailed step-by-step guide to install and configure Apache2 on Ubuntu 24:

Step 1: Update the Package List

Before installing any new software, update the local package index to ensure you have the latest information about available packages:
  1. Open a terminal (or connect to the server via SSH).
  2. Run the following command:
    
    sudo apt update
    This updates the package list for the Ubuntu package manager (apt).

Step 2: Install Apache2

The apache2 package is available in Ubuntu’s default repositories. Use the apt command to install it:
  1. Run the following command:
    sudo apt install apache2 -y
    The -y flag automatically confirms prompts during installation.
  2. After installation, Apache2 should start automatically. Verify this in the next step.

Step 3: Verify Apache2 Service Status

To ensure Apache2 is running correctly:
  1. Check the status of the Apache2 service:
    sudo systemctl status apache2
  2. Look for output like this:
    
    apache2.service - The Apache HTTP Server 
    Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled) 
    Active: active (running) since [timestamp]
    
    If it says active (running), the service is working.
  3. If it’s not running, start it manually:
    sudo systemctl start apache2

Step 4: Test Apache2 Installation

To confirm the installation:
  1. Open a web browser on your machine or any device connected to the same network.
  2. Go to the URL:
    http://localhost/
    Or use the server's IP address:
    http://[your-server-ip]/
  3. You should see the default Apache2 "It works!" page.
    • To find your server’s IP address, use: ip addr show Look for an IP address under the inet section of the active network interface.

Step 5: Configure the Firewall (Optional but Recommended)

If you have a firewall enabled (like UFW), allow Apache traffic:
  1. Allow basic HTTP traffic:
    sudo ufw allow 'Apache'
  2. If you’re hosting HTTPS traffic, allow that too:
    sudo ufw allow 'Apache Full'
  3. Enable the firewall (if not already enabled):
    sudo ufw enable
  4. Check the firewall status:
    sudo ufw status

Step 6: Enable Apache2 to Start on Boot

To ensure Apache2 starts automatically after a reboot:
  1. Run the following command:
    sudo systemctl enable apache2

Step 7: Customize Apache2 (Optional)

If you want to configure Apache further:

7.1. Set Up Virtual Hosts

  • Virtual hosts allow you to host multiple websites on a single server.
  • Create a configuration file for your site:
    sudo nano /etc/apache2/sites-available/your-site.conf
  • Add the following:
    <VirtualHost *:80>
    ServerName your-domain.com
    ServerAlias www.your-domain.com
    DocumentRoot /var/www/your-site
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  • Enable the site:
    sudo a2ensite your-site
  • Reload Apache:
    sudo systemctl reload apache2

7.2. Enable/Disable Apache Modules

Apache comes with several modules for added functionality (e.g., mod_rewrite).
  • Enable a module:
    sudo a2enmod rewrite
    sudo systemctl restart apache2
  • Disable a module:
    sudo a2dismod rewrite
    sudo systemctl restart apache2

Step 8: Uninstall Apache2 (If Needed)

If you need to uninstall Apache2:
  1. Stop the service:
    sudo systemctl stop apache2
  2. Uninstall Apache2:
    sudo apt remove apache2 -y
  3. Remove configuration files (optional):
    sudo apt purge apache2 -y
    sudo apt autoremove -y

This guide covers everything from basic installation to initial configuration. If you encounter issues, feel free to share the details!

Related post

Leave a Reply

Your email address will not be published. Required fields are marked *