How to Install and Configure Apache on Ubuntu

admin | March 21, 2025


How to Install and Configure Apache on Ubuntu

How to Install and Configure Apache on Ubuntu

 

 

Apache is one of the most widely used web servers in the world. If you are running Ubuntu and want to set up a web server, Apache is a great choice. In this guide, we will walk you through installing and configuring Apache on Ubuntu step by step.


Step 1: Update Your System

Before installing Apache, it is always a good practice to update your system packages. Open the terminal and run:

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache

To install Apache, simply run:

sudo apt install apache2 -y

Once the installation is complete, Apache should start automatically.


Step 3: Verify Apache Installation

You can check if Apache is running with:

sudo service apache2 status

If it is not running, start it using:

sudo service apache2 start

other command

sudo service apache2 stop
sudo service apache2 restart

 

You can also verify Apache by opening a web browser and visiting:

http://localhost

If Apache is working correctly, you will see the default Ubuntu Apache page.


Step 4: Configure Firewall (Optional)

If you have UFW (Uncomplicated Firewall) enabled, allow Apache traffic:

sudo ufw allow 'Apache full'

To check the firewall status, run:

sudo ufw status

If not active firewall, run:

sudo ufw enable

for ufw app list, run:

sudo ufw app list

Step 5: Set Up Virtual Hosts

Virtual hosts allow you to host multiple websites on a single server. To create a new virtual host:

  1. Create a directory for your website:
sudo mkdir -p /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
sudo chmod -R 755 /var/www/yourdomain.com/html

 

When to Use Each?

OwnershipBest forNotes
$USER:$USERLocal developmentEasier for user to edit files
www-data:www-dataProduction serversMore secure, avoids permission issues with Apache
  1. Create a configuration file:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
  1. Add the following configuration:
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  1. Enable the virtual host and restart Apache:
sudo a2ensite yourdomain.com.conf
sudo service apache2 restart

Step 6: Enable HTTPS with Let’s Encrypt

To secure your site with SSL:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

Follow the prompts to generate and install an SSL certificate.


Step 7: Manage Apache Modules

To enable a module:

sudo a2enmod ssl

To disable a module:

sudo a2dismod ssl

Restart Apache after making changes:

sudo service apache2 restart

Step 8: Global Setting of apache2

first, for edit Configuration file, run:

sudo nano /etc/apache2/apache2.conf

Hide Apache Version and Server Information

To hide this information, add or modify the following lines in your apache2.conf file:

ServerSignature Off
ServerTokens Prod
  • ServerSignature Off: Disables the Apache-generated footer on error pages.
  • ServerTokens Prod: Ensures that the server only returns "Apache" without version details in response headers.

 

Disable Directory Indexing and Listing

By default, if a directory does not have an index.html or index.php file, Apache will display a list of all files in that directory. This can expose sensitive files and folders.

please remove Indexes  from  Options -Indexes FollowSymLinks:

<Directory /var/www/>
    Options -Indexes FollowSymLinks
</Directory>

 

Step 9: Uninstall Apache (If Needed)

If you ever need to remove Apache, use:

sudo apt remove --purge apache2 -y
sudo apt autoremove -y

Conclusion

Apache is a powerful and flexible web server that can be easily installed and configured on Ubuntu. By following these steps, you can have a fully functional web server running in no time. Whether you are setting up a personal website or managing a large-scale application, Apache provides the reliability and security you need.

Have questions or need further assistance? Leave a comment below! 🚀




0 COMMENTS:

How to Install and Configure Apache on Ubuntu

How to Install and Configure Apache on Ubuntu&nbsp;&nbsp;Apache is one of the most widely used web s

Read More
ubuntu chroot environment | What is chroot?

&nbsp;&nbsp;What is chroot?chroot (short for "change root") is a Unix/Linux command that changes the

Read More
How to Manage WiFi and Web Browsing in Linux Using the Terminal

How to Manage WiFi and Web Browsing in Linux Using the Terminal&nbsp;&nbsp;Linux users often find th

Read More
Essential Linux Commands for System Administration

&nbsp;&nbsp;&nbsp;Essential Linux Commands for System AdministrationLinux is a powerful operating sy

Read More
Beginner's Guide to Learning Ubuntu Server

Beginner's Guide to Learning Ubuntu ServerUbuntu Server is a powerful and widely used Linux-based op

Read More