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
- Installing Apache
On Ubuntu/Debian
On CentOS/RHEL
- Starting and Enabling Apache
On Ubuntu/Debian
On CentOS/RHEL
- Verifying the Installation
Open a web browser and navigate to http://your_server_ip
. You should see the default Apache welcome page.
- Understanding the Directory Structure
-
Ubuntu/Debian:
- Configuration files:
/etc/apache2/
- Document root:
/var/www/html/
- Log files:
/var/log/apache2/
- Configuration files:
-
CentOS/RHEL:
- Configuration files:
/etc/httpd/
- Document root:
/var/www/html/
- Log files:
/var/log/httpd/
- Configuration files:
- 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:
- Hosting a Simple HTML Website
Creating a Sample HTML File
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
- Managing Apache
Checking the Status
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
- 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.
-
Create a new directory for the website:
sudo mkdir /var/www/mywebsite sudo chown -R $USER:$USER /var/www/mywebsite
-
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>
-
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>
-
Enable the new virtual host:
sudo a2ensite mywebsite.conf # Ubuntu/Debian sudo systemctl restart apache2 # Ubuntu/Debian sudo systemctl restart httpd # CentOS/RHEL
-
Update your hosts file (for testing purposes):
sudo nano /etc/hosts
Add the following line:
127.0.0.1 mywebsite.com www.mywebsite.com
-
Restart Apache:
sudo systemctl restart apache2 # Ubuntu/Debian sudo systemctl restart httpd # CentOS/RHEL
-
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.
Linux Mastery: From Beginner to Advanced
Module 1: Introduction to Linux
Module 2: Basic Linux Commands
- Introduction to the Command Line
- Navigating the File System
- File and Directory Operations
- Viewing and Editing Files
- File Permissions and Ownership
Module 3: Advanced Command Line Skills
- Using Wildcards and Regular Expressions
- Piping and Redirection
- Process Management
- Scheduling Tasks with Cron
- Networking Commands
Module 4: Shell Scripting
- Introduction to Shell Scripting
- Variables and Data Types
- Control Structures
- Functions and Libraries
- Debugging and Error Handling
Module 5: System Administration
- User and Group Management
- Disk Management
- Package Management
- System Monitoring and Performance Tuning
- Backup and Restore
Module 6: Networking and Security
- Network Configuration
- Firewall and Security
- SSH and Remote Access
- Intrusion Detection Systems
- Securing Linux Systems
Module 7: Advanced Topics
- Virtualization with Linux
- Linux Containers and Docker
- Automating with Ansible
- Linux Kernel Tuning
- High Availability and Load Balancing