Installing Apache Web Server

From Server rental store
Jump to navigation Jump to search

Installing Apache Web Server

This guide provides a comprehensive walkthrough for installing and configuring the Apache HTTP Server (httpd) on a Linux system. We will cover basic installation, setting up virtual hosts for multiple websites, enabling URL rewriting with `mod_rewrite`, and securing your server with SSL/TLS certificates. This tutorial is suitable for beginners to intermediate Linux system administrators.

Prerequisites

Before you begin, ensure you have the following:

  • A Linux server with root or sudo privileges. Dedicated servers from PowerVPS offer the full root access needed for these operations.
  • Internet connectivity to download packages.
  • Basic understanding of the Linux command line.
  • (Optional) A domain name pointing to your server's IP address.

Step 1: Installing Apache HTTP Server

The installation process varies slightly depending on your Linux distribution.

For Debian/Ubuntu-based systems

Use the Advanced Package Tool (APT) to install Apache:

sudo apt update
sudo apt install apache2 -y

For RHEL/CentOS/Fedora-based systems

Use the YUM or DNF package manager:

sudo yum update -y
sudo yum install httpd -y
# Or for newer Fedora/RHEL versions:
# sudo dnf update -y
# sudo dnf install httpd -y

After installation, start and enable the Apache service to run on boot:

# For Debian/Ubuntu
sudo systemctl start apache2
sudo systemctl enable apache2

# For RHEL/CentOS/Fedora
sudo systemctl start httpd
sudo systemctl enable httpd

Verify the status of the Apache service:

# For Debian/Ubuntu
sudo systemctl status apache2

# For RHEL/CentOS/Fedora
sudo systemctl status httpd

You should see output indicating the service is "active (running)".

Firewall Configuration

If you have a firewall enabled (e.g., `ufw` or `firewalld`), you need to allow HTTP and HTTPS traffic.

Using UFW (Debian/Ubuntu)

sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw status

Using Firewalld (RHEL/CentOS/Fedora)

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

Now, open your web browser and navigate to your server's IP address (e.g., `http://your_server_ip`). You should see the default Apache welcome page.

Step 2: Configuring Virtual Hosts

Virtual hosts allow you to host multiple websites on a single server. Each website will have its own configuration file.

Create Directory Structure

For each website, create a dedicated directory for its files. It's common practice to place these in `/var/www/`.

Let's assume you want to host `example.com` and `blog.example.com`.

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/blog.example.com/public_html

Set Permissions

Ensure Apache has the necessary permissions to access these directories.

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/blog.example.com/public_html
sudo chmod -R 755 /var/www/

Create a sample `index.html` file for each site:

echo "<h1>Welcome to example.com!</h1>" | sudo tee /var/www/example.com/public_html/index.html
echo "<h1>Welcome to blog.example.com!</h1>" | sudo tee /var/www/blog.example.com/public_html/index.html

Create Virtual Host Configuration Files

Apache's virtual host configurations are typically stored in `/etc/apache2/sites-available/` (Debian/Ubuntu) or `/etc/httpd/conf.d/` (RHEL/CentOS/Fedora).

For Debian/Ubuntu

Create a new configuration file for `example.com`:

sudo nano /etc/apache2/sites-available/example.com.conf

Paste the following content, replacing `example.com` with your domain and `your_server_ip` with your server's IP address:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Create a similar file for `blog.example.com`:

sudo nano /etc/apache2/sites-available/blog.example.com.conf

Paste the following content:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName blog.example.com
    DocumentRoot /var/www/blog.example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/blog.example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/blog.example.com_access.log combined
</VirtualHost>

Enable the new virtual host configurations and disable the default one:

sudo a2ensite example.com.conf
sudo a2ensite blog.example.com.conf
sudo a2dissite 000-default.conf

For RHEL/CentOS/Fedora

Create a new configuration file for `example.com` in the `conf.d` directory:

sudo nano /etc/httpd/conf.d/example.com.conf

Paste the following content:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>

Create a similar file for `blog.example.com`:

sudo nano /etc/httpd/conf.d/blog.example.com.conf

Paste the following content:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName blog.example.com
    DocumentRoot /var/www/blog.example.com/public_html
    ErrorLog /var/log/httpd/blog.example.com_error.log
    CustomLog /var/log/httpd/blog.example.com_access.log combined
</VirtualHost>

Test and Reload Apache

Before applying the changes, test your Apache configuration for syntax errors:

# For Debian/Ubuntu
sudo apache2ctl configtest

# For RHEL/CentOS/Fedora
sudo httpd -t

If the syntax is OK, reload Apache to apply the new virtual hosts:

# For Debian/Ubuntu
sudo systemctl reload apache2

# For RHEL/CentOS/Fedora
sudo systemctl reload httpd

Now, if you have your DNS records set up correctly, `example.com` and `blog.example.com` should point to their respective content.

Step 3: Enabling mod_rewrite

The `mod_rewrite` module allows for powerful URL manipulation, often used for SEO-friendly URLs or redirecting traffic.

Enable the Module

For Debian/Ubuntu

sudo a2enmod rewrite
sudo systemctl restart apache2

For RHEL/CentOS/Fedora

`mod_rewrite` is usually enabled by default. If not, you might need to uncomment a `LoadModule rewrite_module modules/mod_rewrite.so` line in your main Apache configuration file (e.g., `/etc/httpd/conf/httpd.conf`). After enabling, reload Apache:

sudo systemctl reload httpd

Configure .htaccess

To use `mod_rewrite` rules, you need to allow `.htaccess` files to override server configurations. Edit your virtual host configuration files and add `AllowOverride All` within the `<Directory>` block for your `public_html` directories.

For `example.com.conf` (Debian/Ubuntu):

<Directory /var/www/example.com/public_html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

For `example.com.conf` (RHEL/CentOS/Fedora):

<Directory "/var/www/example.com/public_html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Repeat this for `blog.example.com`. After making changes, test and reload Apache.

Example .htaccess Rule

Create a `.htaccess` file in your `public_html` directory:

echo "RewriteEngine On
RewriteRule ^new-page$ /old-page.html [R=301,L]" | sudo tee /var/www/example.com/public_html/.htaccess

This rule will redirect requests for `/new-page` to `/old-page.html` with a permanent (301) redirect.

Step 4: Securing Apache with SSL/TLS

Securing your websites with HTTPS is crucial for data privacy and trust. We'll use Let's Encrypt for free SSL certificates.

Install Certbot

Certbot is a tool that automates the process of obtaining and renewing Let's Encrypt certificates.

For Debian/Ubuntu

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

For RHEL/CentOS/Fedora

First, ensure you have the EPEL repository enabled.

sudo yum install epel-release -y
sudo yum install certbot python3-certbot-apache -y
# Or for newer Fedora/RHEL versions:
# sudo dnf install epel-release -y
# sudo dnf install certbot python3-certbot-apache -y

Obtain SSL Certificates

Run Certbot to get certificates for your domains. Make sure your domain's DNS records are pointing to your server's IP address.

sudo certbot --apache -d example.com -d www.example.com -d blog.example.com

Follow the on-screen prompts. Certbot will automatically detect your virtual hosts, obtain certificates, and configure Apache to use them. It will also set up automatic renewal.

Verify SSL Configuration

After running Certbot, your sites should be accessible via HTTPS. You can verify this by visiting `https://example.com` in your browser. Look for the padlock icon.

Certbot typically modifies your virtual host files to include SSL configurations and redirects HTTP traffic to HTTPS.

Troubleshooting

  • Apache not starting: Check the Apache error logs (`/var/log/apache2/error.log` or `/var/log/httpd/error_log`) for specific messages. Common issues include syntax errors in configuration files or port conflicts.
  • Virtual hosts not working:
   *   Ensure DNS records are correctly pointing to your server's IP.
   *   Verify that your virtual host configuration files are enabled (e.g., using `a2ensite` on Debian/Ubuntu).
   *   Check file permissions for your web root directories.
   *   Confirm that `ServerName` directives match the domains you are testing.
  • SSL certificate errors:
   *   Ensure Certbot ran successfully and that you followed all prompts.
   *   Check that your domain's DNS is pointing to the correct IP address.
   *   Verify that port 80 is open for the initial Let's Encrypt challenge.
   *   Run `sudo certbot renew --dry-run` to test the renewal process.
  • mod_rewrite not working:
   *   Ensure `mod_rewrite` is enabled.
   *   Verify that `AllowOverride All` is set in the relevant `<Directory>` block in your Apache configuration.
   *   Check your `.htaccess` file for syntax errors.

Related Articles