In this section, we will guide you through the process of setting up a web server on a Linux system. This practical project will help you understand the essential components and configurations required to host a website. We will use Apache, one of the most popular web servers, for this tutorial.

Objectives

  • Install and configure Apache web server
  • Understand the directory structure and configuration files
  • Host a simple HTML website
  • Manage and troubleshoot the web server

Prerequisites

  • Basic knowledge of Linux commands
  • A Linux system (preferably Ubuntu or CentOS)
  • Internet connection

Step-by-Step Guide

  1. Installing Apache

On Ubuntu/Debian

sudo apt update
sudo apt install apache2

On CentOS/RHEL

sudo yum update
sudo yum install httpd

  1. Starting and Enabling Apache

On Ubuntu/Debian

sudo systemctl start apache2
sudo systemctl enable apache2

On CentOS/RHEL

sudo systemctl start httpd
sudo systemctl enable httpd

  1. Verifying the Installation

Open a web browser and navigate to http://your_server_ip. You should see the default Apache welcome page.

  1. Understanding the Directory Structure

  • Ubuntu/Debian:

    • Configuration files: /etc/apache2/
    • Document root: /var/www/html/
    • Log files: /var/log/apache2/
  • CentOS/RHEL:

    • Configuration files: /etc/httpd/
    • Document root: /var/www/html/
    • Log files: /var/log/httpd/

  1. Configuring Apache

Editing the Configuration File

The main configuration file is typically located at /etc/apache2/apache2.conf (Ubuntu/Debian) or /etc/httpd/conf/httpd.conf (CentOS/RHEL).

Example: Setting the ServerName

sudo nano /etc/apache2/apache2.conf  # Ubuntu/Debian
sudo nano /etc/httpd/conf/httpd.conf  # CentOS/RHEL

Add the following line:

ServerName your_server_ip

  1. Hosting a Simple HTML Website

Creating a Sample HTML File

sudo nano /var/www/html/index.html

Add the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Web Server</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a simple HTML page hosted on my Apache web server.</p>
</body>
</html>

Restarting Apache

sudo systemctl restart apache2  # Ubuntu/Debian
sudo systemctl restart httpd  # CentOS/RHEL

  1. Managing Apache

Checking the Status

sudo systemctl status apache2  # Ubuntu/Debian
sudo systemctl status httpd  # CentOS/RHEL

Stopping and Starting Apache

sudo systemctl stop apache2  # Ubuntu/Debian
sudo systemctl start apache2  # Ubuntu/Debian

sudo systemctl stop httpd  # CentOS/RHEL
sudo systemctl start httpd  # CentOS/RHEL

  1. Troubleshooting

Checking Log Files

  • Ubuntu/Debian: /var/log/apache2/error.log
  • CentOS/RHEL: /var/log/httpd/error.log

Common Issues

  • Port Conflicts: Ensure no other service is using port 80.
  • Permission Issues: Ensure the web server has the necessary permissions to read the document root.

Practical Exercise

Task: Create a new virtual host to serve a different website.

  1. Create a new directory for the website:

    sudo mkdir /var/www/mywebsite
    sudo chown -R $USER:$USER /var/www/mywebsite
    
  2. Create a sample HTML file:

    nano /var/www/mywebsite/index.html
    

    Add the following content:

    <!DOCTYPE html>
    <html>
    <head>
        <title>My Website</title>
    </head>
    <body>
        <h1>Welcome to My Website</h1>
        <p>This is a different website hosted on the same server.</p>
    </body>
    </html>
    
  3. Create a new virtual host configuration file:

    sudo nano /etc/apache2/sites-available/mywebsite.conf  # Ubuntu/Debian
    sudo nano /etc/httpd/conf.d/mywebsite.conf  # CentOS/RHEL
    

    Add the following content:

    <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName mywebsite.com
        ServerAlias www.mywebsite.com
        DocumentRoot /var/www/mywebsite
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  4. Enable the new virtual host:

    sudo a2ensite mywebsite.conf  # Ubuntu/Debian
    sudo systemctl restart apache2  # Ubuntu/Debian
    
    sudo systemctl restart httpd  # CentOS/RHEL
    
  5. Update your hosts file (for testing purposes):

    sudo nano /etc/hosts
    

    Add the following line:

    127.0.0.1 mywebsite.com www.mywebsite.com
    
  6. Restart Apache:

    sudo systemctl restart apache2  # Ubuntu/Debian
    sudo systemctl restart httpd  # CentOS/RHEL
    
  7. Verify the new website: Open a web browser and navigate to http://mywebsite.com. You should see the new website.

Conclusion

In this section, you learned how to set up an Apache web server on a Linux system, host a simple HTML website, and manage the server. You also practiced creating a new virtual host to serve multiple websites on the same server. This foundational knowledge will help you in more advanced web server configurations and deployments.

© Copyright 2024. All rights reserved