Setting up an Apache2 web server on Ubuntu is a straightforward process. In this post, we’ll walk through each step in detail, from installation to verifying that the server is running correctly. Follow these instructions to get your Apache2 server up and running in no time.

Prerequisites

  • A system running Ubuntu (or an Ubuntu-based distribution).
  • Access to the terminal with sudo privileges.

Step 1: Update the System

Before installing any new software, it’s always a good idea to update your system to ensure that all the packages are up-to-date. Run the following command:

sudo apt update && sudo apt upgrade -y

This will refresh your package list and upgrade any installed packages.

Step 2: Install Apache2

To install the Apache2 package, run the following command:

sudo apt install apache2 -y

The -y flag automatically confirms the installation. Once the installation is complete, Apache2 will be up and running.

Step 3: Check Apache2 Status

You can verify that Apache2 is running by checking its status using the systemctl command:

sudo systemctl status apache2

You should see an output indicating that the Apache service is active and running.

Step 4: Configure UFW Firewall

If you have UFW (Uncomplicated Firewall) enabled on your server, you need to allow HTTP (port 80) and HTTPS (port 443) traffic through the firewall:

sudo ufw allow 'Apache Full'

To check the status of UFW and see which ports are open, use:

sudo ufw status

Step 5: Test Apache

Once the server is running, you can test it by opening a web browser and entering your server’s IP address or localhost if you’re on the same machine:

http://localhost

or

http://<your-server-ip>

You should see the default Apache2 Ubuntu welcome page, confirming that the server is up and running.

Step 6: Manage Apache2 Service

You can manage the Apache2 service using systemctl commands:

  • Restart Apache2:

      sudo systemctl restart apache2
    
  • Stop Apache2:

      sudo systemctl stop apache2
    
  • Enable Apache2 to Start at Boot:

      sudo systemctl enable apache2
    

Step 7: Customize Apache Configuration (Optional)

The default configuration files for Apache2 are located in the /etc/apache2/ directory. To customize the settings, you can edit the apache2.conf file:

sudo nano /etc/apache2/apache2.conf

Make any necessary changes and restart Apache for the changes to take effect:

sudo systemctl restart apache2

Conclusion

That’s it! You’ve successfully set up an Apache2 web server on your Ubuntu machine. You can now host your website or web application. Stay tuned for more tutorials on how to configure Apache for specific use cases, such as setting up virtual hosts and securing your server with SSL.

If you encounter any issues, feel free to leave a comment below or check the Apache documentation for more information.

Happy hosting!