How to Install Apache and Secure it with a Let’s Encrypt SSL Certificate?

Apache is a popular web server for hosting websites. Here’s how to set it up securely with an SSL certificate.

Creating DNS Record

Log in to Cloudflare or your domain registrar and create an A record to link your domain to the cloud VMs IP address.

DNS management

 

Installing Apache (Ubuntu)

Update the available packages:
sudo apt-get update

Install Apache:
sudo apt-get install apache2

Allow ports 80 and 443 in your firewall:
sudo ufw allow 'Apache Full'

Check if Apache is running:
sudo systemctl status apache2

Configuring Apache to Serve Important Files

Create a folder for your web apps:
sudo mkdir -p /var/www/test.sanakil.xyz/webapp

Create an HTML file for testing:
sudo vi /var/www/test.sanakil.xyz/webapp/index.html

Paste the HTML snippet for testing.

Create folders for logs:

sudo mkdir -p /var/www/test.sanakil.xyz/log
sudo touch /var/www/test.sanakil.xyz/log/request.log
sudo touch /var/www/test.sanakil.xyz/log/error.log

Open the configuration file:

Ubuntu: sudo vi /etc/apache2/sites-available/test.sanakil.xyz.conf

CentOS: sudo vi /etc/httpd/sites-available/test.sankil.xyz.conf

Paste the configuration snippet and modify the domain name.

Set permissions for the folder:
sudo chown -R $USER:$USER /var/www/test.abc.xyz
sudo chmod -R 755 /var/www/test.abc.xyz

Enable the virtual host configuration file (Ubuntu):
sudo a2ensite test.sanakil.xyz.conf

Disable the default configuration file:
sudo a2dissite 000-default.conf

Restart Apache:
sudo systemctl restart apache2

Additional Setup for CentOS

Create folders for Apache:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

Tell Apache to read configuration files from the sites-enabled folder.

Steps to Enable HTTPS

Install Certbot for Apache (Ubuntu):
sudo add-apt-repository ppa:certbot/certbot
sudo apt install python-certbot-apache

For CentOS:
sudo yum install epel-release
sudo yum install certbot python2-certbot-apache mod_ssl

Get an SSL certificate:
sudo certbot --apache -d your-domain-name

Follow the prompts to complete the setup.

This guide should help you install and set up an SSL certificate on Apache. For more help contact team host.co.in