Install and Configure Apache On Ubuntu

Updated: June 6th, 2023, 02:06:53 IST
Published: June 5th, 2023
Install and Configure Apache On Ubuntu
Title: Install and Configure Apache On Ubuntu

Apache is a popular open-source web server for hosting websites and applications. Installing and configuring Apache is a simple task if you use Ubuntu as your operating system. In this article, we will walk you through the processes of installing and configuring Apache on your Ubuntu operating system, allowing you to create a solid foundation for hosting your website or web application.

1. Overview

Apache is a free open source web server that is available for Linux systems. In this article, we'll walk through the basics of installing an Apache server.

What you'll Learn

1. How to Install Apache

2. Some fundamental Apache configuration

What you'll require

1. Secure Shell (SSH) access to your Ubuntu Server 16.04 LTS server

2. Basic command-line expertise in Linux

Have you prepared everything? Now, let us go to the next phase!

2. Installing Apache

To install and configure Apache on Ubuntu, install the latest meta-package apache2 by running:

1. Update the package list:

sudo apt update

 

2. Install Apache:

sudo apt install apache2

 

3. Once the installation is complete, Apache will automatically start running. You can verify its status by running the following command:

sudo systemctl status apache2

If Apache is running, you should see a message indicating that it's active and running.

3.1. Once the installation is complete, use the following command to start the Apache service:

sudo systemctl start apache2 

3.2. You may also have Apache start automatically at startup time by typing the following command:

sudo systemctl enable apache2 

After letting the command run, all required packages are installed and we can test it out by typing in our IP address for the web server.

Localhost IP or web address: http://127.0.0.1 or http://localhost

 

4. Configure firewall settings:

If you have UFW (Uncomplicated Firewall) enabled, you need to allow incoming traffic on port 80 (HTTP) and port 443 (HTTPS). Run the following commands to allow HTTP and HTTPS traffic, respectively:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

 

To adjust the Apache configuration, edit the main configuration file located at /etc/apache2/apache2.conf using a text editor. Test the configuration for syntax errors with sudo apache2ctl configtest. Enable additional Apache modules as needed using sudo a2enmod module_name. Restart Apache with sudo systemctl restart apache2. Access the default Apache page at http://localhost or http://your-server-ip. Ensure security by configuring access controls, SSL certificates, and other necessary settings.

 

3. Creating Your Own Website

By default, Apache comes with a basic site enabled. To modify its content, navigate to /var/www/html or edit its Virtual Host file located at /etc/apache2/sites-enabled/000-default.conf.

To have multiple sites running on the same server and customize how Apache handles incoming requests, we need to modify the Virtual Hosts file.

In this tutorial, we will set up a new website called "gci.example.com" by creating a dedicated folder for it. Let's start by creating the folder using the following command:

sudo mkdir /var/www/gci/

Feel free to choose a different name for your website, but remember to update it accordingly in the virtual hosts configuration file later.

Now that we have the directory, let's create an HTML file inside it. Change to the newly created directory and open a text editor by running:

cd /var/www/gci/
nano index.html

Inside the index.html file, paste the following code:

index.html

<html>
<head>
  <title>Welcome to Ubuntu rocks! </title>
</head>
<body>
  <p>This website is running on an Ubuntu Server!</p>
</body>
</html>

Isn't that cool?

To make our website accessible at "gci.example.com" we need to create a VirtualHost configuration file. Let's create it now:

sudo nano /etc/apache2/sites-available/gci.conf

Inside the file, add the following configuration:

<VirtualHost *:80>
  ServerName gci.example.com
  DocumentRoot /var/www/gci/

  <Directory /var/www/gci/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/gci_error.log
  CustomLog ${APACHE_LOG_DIR}/gci_access.log combined
</VirtualHost>

Save the file and exit the text editor.

Next, enable the newly created VirtualHost configuration by creating a symbolic link:

sudo ln -s /etc/apache2/sites-available/gci.conf /etc/apache2/sites-enabled/

Now, disable the default site to avoid conflicts:

sudo a2dissite 000-default.conf

Finally, restart Apache to apply the changes:

sudo systemctl restart apache2

Congratulations! Your website at "gci.example.com" is now ready to be accessed. You can navigate to it using a web browser.

4. Setting up the VirtualHost Configuration File

To configure the VirtualHost file for your website:

Navigate to the Apache configuration files directory:

cd /etc/apache2/sites-available/

 

Create a copy of the default VirtualHost file as a base for your site configuration:

sudo cp 000-default.conf gci.conf

 

Edit the configuration file:

sudo nano gci.conf

 

Add your email address in the ServerAdmin directive to receive error notifications:

ServerAdmin yourname@example.com

 

Set the DocumentRoot directive to point to the directory where your site files are located:

DocumentRoot /var/www/gci/

 

Add the ServerName directive to define your subdomain:

ServerName gci.example.com

 

This ensures that visitors accessing "gci.example.com" reach your specific site.

Save the changes and exit the text editor.

In the next step, we will activate the configuration.

5. Activating VirtualHost file

To activate the virtual hosts configuration file for your website:

1. Change to the Apache configuration files directory:

cd /etc/apache2/sites-available/

 

2. Enable the site configuration file:

sudo a2ensite gci.conf

You should see a message confirming the site's activation.

 

3. To apply the changes, restart Apache:

sudo service apache2 reload

 

Now, when you enter your host name (e.g., "gci.example.com") in a web browser, your website should load correctly.

By activating the virtual hosts configuration file, you have made your website accessible through Apache. Remember to update the command with the actual name of your configuration file, if it differs from "gci.conf".

Following these steps will allow you to successfully install and setup Apache on Ubuntu, allowing you to easily host websites and apps. To create a safe hosting environment, remember to consider security measures such as implementing access restrictions, SSL certificates, and other relevant settings.