Published on October 21, 2024By DeveloperBreeze

Tutorial: How to Install and Configure Apache on Ubuntu

Apache is one of the most popular and widely-used web servers. This tutorial will guide you through the process of installing and configuring Apache on an Ubuntu server.

Prerequisites:

  • An Ubuntu server (18.04, 20.04, or newer).
  • Root or sudo user privileges.

Step 1: Update the Package Index

Before installing Apache, it's a good idea to ensure your package index is up to date. Run the following command to update your server:

sudo apt update

Step 2: Install Apache

To install Apache on your server, use the following command:

sudo apt install apache2

Once the installation is complete, Apache will automatically start. You can verify if it's running with:

sudo systemctl status apache2

You should see a message indicating that Apache is active and running.

Step 3: Adjust the Firewall to Allow Web Traffic

If you have UFW (Uncomplicated Firewall) running, you will need to allow Apache traffic through the firewall. UFW has several predefined profiles that you can use to allow or deny traffic.

Check the available application profiles:

sudo ufw app list

You should see an output similar to:

Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

To allow both HTTP and HTTPS traffic, run:

sudo ufw allow 'Apache Full'

Then, enable the firewall (if not already enabled):

sudo ufw enable

Step 4: Verify Apache Installation

To verify if Apache is installed and running correctly, open your browser and enter your server’s IP address:

http://your-server-ip

You should see the default Apache welcome page, which means Apache is working properly.

Step 5: Basic Apache Configuration

Apache’s configuration files are located in the /etc/apache2/ directory. You can configure global settings, virtual hosts, and more from these files.

Modify the Default Virtual Host

To customize the default website served by Apache, edit the configuration file:

sudo nano /etc/apache2/sites-available/000-default.conf

In this file, you can change the document root (where Apache serves files from) or add configurations such as additional directories.

Example: Change Document Root

If you want Apache to serve content from a different directory, you can change the DocumentRoot directive:

DocumentRoot /var/www/html

Save the file after making changes, then restart Apache to apply the changes:

sudo systemctl restart apache2

Step 6: Managing Apache

Start Apache:

sudo systemctl start apache2

Stop Apache:

sudo systemctl stop apache2

Restart Apache:

sudo systemctl restart apache2

Reload Apache (without stopping):

sudo systemctl reload apache2

Step 7: Enable Apache to Start on Boot

To ensure Apache starts on boot, use the following command:

sudo systemctl enable apache2

Step 8: Check Apache Server Logs

Apache logs can help troubleshoot issues with your web server. The logs are located in the /var/log/apache2/ directory.

  • Access logs: /var/log/apache2/access.log
  • Error logs: /var/log/apache2/error.log

You can view the logs using the cat or tail command:

sudo tail -f /var/log/apache2/error.log

Conclusion

Congratulations! You have successfully installed and configured Apache on your Ubuntu server. You can now host your websites and further customize your Apache setup according to your needs.


This tutorial provides all the steps you need to set up Apache on an Ubuntu system, similar to an SSL tutorial but focused on configuring a web server.

Comments

Please log in to leave a comment.

Continue Reading: