Install and Configure Apache Web Server on Linux

Apache is the most widely used web server software, and it is available on almost every Linux distribution. In this tutorial, we will show you how to install and configure the Apache web server on Linux.

Before we start, it is essential to update the system. Run the following command in the terminal:

sudo apt-get update && sudo apt-get upgrade

Install Apache Web Server

Now, let’s install Apache. Run the following command in the terminal:

sudo apt-get install apache2

This will install Apache and its dependencies. Once the installation is complete, you can check whether Apache is running or not by typing the following command in the terminal:

systemctl status apache2

You should see a message that says “active (running)”.

Configure Apache Web Server

By default, Apache will serve the content from the /var/www/html directory. You can change this directory by editing the Apache configuration file. To do this, open the file using the following command:

sudo nano /etc/apache2/sites-available/000-default.conf

Here, you can change the DocumentRoot to your preferred directory. For example, if you want to serve content from the /home/user/public_html directory, you can change the line to:

DocumentRoot /home/user/public_html

Save and close the file.

Test Apache Web Server

Now, it’s time to test Apache. Open a web browser and enter your server’s IP address or domain name. You should see the default Apache page.

Create Virtual Hosts

If you want to host multiple websites on the same server, you can create virtual hosts. To do this, create a new configuration file for each virtual host in the /etc/apache2/sites-available directory. For example, to create a virtual host for example.com, create a file named example.com.conf and add the following lines:

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

Save and close the file. Then, enable the virtual host by running the following command:

sudo a2ensite example.com.conf

Restart Apache for the changes to take effect:

sudo systemctl restart apache2

Now, you can access the website by entering example.com in your web browser.

Secure Apache Web Server

After installing Apache, it is important to secure it to prevent unauthorized access. One way to do this is by creating a firewall to limit access to the server. You can use the ufw (uncomplicated firewall) command to create a firewall. First, allow incoming traffic on port 80 (HTTP) and 443 (HTTPS) by running the following commands:

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

Then, enable the firewall by running:

sudo ufw enable

This will block all incoming traffic except for the allowed ports.

Install and Configure SSL Certificate

If you want to secure your website with HTTPS, you can install and configure an SSL (Secure Socket Layer) certificate. Also, there are many SSL providers available, and some of them offer free SSL certificates. One popular provider is Let’s Encrypt. You can install Let’s Encrypt by running the following command:

sudo apt-get install certbot python3-certbot-apache

Once the installation is complete, run the following command to obtain an SSL certificate:

sudo certbot --apache

Follow the on-screen instructions to generate the certificate. After the certificate is installed, you can configure Apache to use HTTPS by editing the virtual host configuration file and adding the following lines:

SSLEngine On
SSLCertificateFile /path/to/certificate.crt
SSLCertificateKeyFile /path/to/privatekey.key

Replace /path/to/certificate.crt and /path/to/privatekey.key with the actual paths to your SSL certificate and private key.

Install Additional Apache Modules

Apache has many additional modules that you can install to extend its functionality. To install a module, use the following command:

sudo apt-get install apache2-mod-<module_name>

Replace <module_name> with the name of the module you want to install. Some popular modules include mod_rewrite (for URL rewriting), mod_ssl (for SSL support), and mod_security (for web application firewall).

In conclusion, by following the above steps, you can install and configure the Apache web server on Linux, secure it with a firewall and SSL certificate, and install additional modules to extend its functionality. With Apache, you can host static and dynamic websites and provide a reliable and secure platform for your web content.

4 thoughts on “Install and Configure Apache Web Server on Linux”

  1. Pingback: The Linux Defender's Guide to DDoS Prevention and Recovery - Learn with Arctic Guru

  2. Pingback: DDoS Prevention and Recovery - Learn with Arctic Guru

  3. Pingback: SSL certificates - Learn with Arctic Guru

  4. Pingback: Web Development - Learn with Arctic Guru

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top