Cloud Service >> Knowledgebase >> Cloud Server >> How to Configure Apache Web Server?
submit query

Cut Hosting Costs! Submit Query Today!

How to Configure Apache Web Server?

The Apache HTTP Server is a widely used web server globally. It caters to various uses with its:

 

- Open-source nature

- Extensive customization options

- Strong features

Setting up Apache may appear overwhelming initially, but it becomes manageable with clear instructions.

 

This manual will guide you in configuring an Apache server hosting efficiently.

 

Prerequisites

Make sure you have the necessary prerequisites before starting the configuration process:

 

1. A server that operates on a Linux distribution like:

- Ubuntu

- CentOS

- Debian

2. Root or sudo access to the server.

3. Apache is installed on the server. If Apache is not installed, you can do so using the following command for your respective Linux distribution:

 

Ubuntu/Debian:

sudo apt update

sudo apt install apache2

 

CentOS/RHEL

sudo yum install httpd

1. Basic Apache Configuration

The usual location for Apache's main configuration file is typically found

 

/etc/apache2/apache2.conf (Ubuntu/Debian systems)

or

/etc/httpd/conf/httpd.conf  (CentOS/RHEL systems)

This document includes worldwide configurations for the Apache server.

Open the main configuration file:

sudo nano /etc/apache2/apache2.conf   # Ubuntu/Debian

sudo nano /etc/httpd/conf/httpd.conf  # CentOS/RHEL

ServerName Directive

Specify the ServerName directive with your domain name or IP address to prevent warning messages regarding the server's complete domain name.

ServerName yourdomain.com

Adjusting the Timeout Setting

The Timeout directive defines how long Apache will wait for certain events before failing a request. The default value is generally 300 seconds.

Timeout 60

Modules

Apache’s functionality can be extended with modules. Common modules include:

- mod_rewrite for URL rewriting

- mod_ssl for enabling SSL

Ensure necessary modules are enabled.

 

Enable modules on Ubuntu/Debian

 

sudo a2enmod rewrite

sudo a2enmod ssl

 

Enable modules on CentOS/RHEL

 

Add or remove the specified lines in the configuration file.

LoadModule rewrite_module modules/mod_rewrite.so

LoadModule ssl_module modules/mod_ssl.so

 

2. Configuring Virtual Hosts

Virtual Hosts enable the cloud hosting of numerous websites on one server. Every site will possess its configuration file.

Create a Virtual Host file

- Ubuntu/Debian

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

- CentOS/RHEL

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

Add the following configuration to the Virtual Host file

ServerAdmin [email protected]

ServerName yourdomain.com

ServerAlias www.yourdomain.com

DocumentRoot /var/www/yourdomain.com/public_html

ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log

CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined

Options Indexes FollowSymLinks

AllowOverride All

Require all granted



Enable the Virtual Host

- Ubuntu/Debian

sudo a2ensite yourdomain.com.conf

sudo systemctl reload apache2

- CentOS/RHEL

There is no need to enable sites separately. Just restart Apache.

sudo systemctl restart httpd

Create the Document Root directory and set permissions

sudo mkdir -p /var/www/yourdomain.com/public_html

sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html

sudo chmod -R 755 /var/www/yourdomain.com

3. SSL for Secure Connections

To activate SSL, you must have a certificate. You can acquire it from a:

- Certificate Authority

- Use a self-signed certificate

Install mod_ssl

- Ubuntu/Debian

sudo a2enmod ssl

sudo systemctl restart apache2

- CentOS/RHEL

sudo yum install mod_ssl

sudo systemctl restart httpd

Create an SSL Virtual Host

- Ubuntu/Debian

sudo nano /etc/apache2/sites-available/yourdomain.com-ssl.conf

- CentOS/RHEL

sudo nano /etc/httpd/conf.d/yourdomain.com-ssl.conf

Add the following configuration to the SSL Virtual Host file

ServerAdmin [email protected]

ServerName yourdomain.com

ServerAlias www.yourdomain.com

DocumentRoot /var/www/yourdomain.com/public_html

ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log

CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined

SSLEngine on

SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt

SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

Options Indexes FollowSymLinks

AllowOverride All

Require all granted


Enable the SSL Virtual Host

- Ubuntu/Debian

sudo a2ensite yourdomain.com-ssl.conf

sudo systemctl reload apache2

- CentOS/RHEL

No need to enable sites separately. Just restart Apache.

sudo systemctl restart httpd

Testing and Troubleshooting

Test Configuration

Ensure your Apache configuration files are free of syntax errors.

sudo apachectl configtest  # Ubuntu/Debian

sudo httpd -t             # CentOS/RHEL

Restart Apache

sudo systemctl restart apache2  # Ubuntu/Debian

sudo systemctl restart httpd   # CentOS/RHEL

Verify the Website

Access https://yourdomain.com on your web browser to confirm accessibility and proper SSL certificate setup.

To Sum it Up!

Configuring the Apache web server involves:

- Setting up the main configuration file.

- Creating and enabling Virtual Hosts.

- Securing the server with SSL certificates.

Following these steps ensures your Apache server is configured properly to efficiently and securely serve your websites. Updating your server, monitoring its performance, and staying current with top security practices can ensure a strong and secure web hosting environment.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!