# How to Install and Configure SSL Certificates in Apache?

This guide will walk you through the complete process of setting up Apache with SSL (HTTPS) on an Ubuntu server. By the end, your Apache web server will be configured with a valid SSL certificate and ready to securely serve web trafficnumber in the ports section.&#x20;

**Prerequisites**

* Ubuntu VM with root access (root password set)
* Ports 80 and 443 must be open in the Security Group

**Step 1: Run the following command to install Apache:**&#x20;

```
apt install apache2
```

<figure><img src="https://2011575719-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVBa3yjTJ7LPbjby0TKRi%2Fuploads%2FoTUpcvXXCcapl4fw3nc5%2Funknown.png?alt=media&#x26;token=1224462f-d162-43db-923e-83cb95178d0b" alt=""><figcaption></figcaption></figure>

**Step 2: Enable Required Apache Modules**

Run the following commands to load necessary modules:

`sudo a2enmod ssl`

`sudo a2enmod proxy`

`sudo a2enmod proxy_http`

`sudo a2enmod headers`

`sudo a2enmod rewrite`

<figure><img src="https://2011575719-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVBa3yjTJ7LPbjby0TKRi%2Fuploads%2FQEEl7QY1HBdHzWBtsNeO%2Funknown.png?alt=media&#x26;token=efad0f42-74db-4087-af29-a860604d5bb6" alt=""><figcaption></figcaption></figure>

**Step 3: Restart the Apache Service**

```
sudo systemctl restart apache2
```

<figure><img src="https://2011575719-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVBa3yjTJ7LPbjby0TKRi%2Fuploads%2F8Bvk8EEiaQUei0nBdUPg%2Funknown.png?alt=media&#x26;token=e755b312-9734-46c8-a0f5-191efe4eb2b9" alt=""><figcaption></figcaption></figure>

**Step 4: Verify Apache Installation**

* Open a browser and enter the server's IP address.
* You should see the Apache default page.
* Ensure ports 80 and 443 are open in the Security Group.

<figure><img src="https://2011575719-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVBa3yjTJ7LPbjby0TKRi%2Fuploads%2FLA5EJWQVePicftuJx2W6%2Funknown.png?alt=media&#x26;token=0a443836-0ef2-4e07-b6fe-a8c9faf10b4b" alt=""><figcaption></figcaption></figure>

**Step 5**: **Check Listening Ports**

Run command:

```
netstat -tulnp
```

<figure><img src="https://2011575719-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVBa3yjTJ7LPbjby0TKRi%2Fuploads%2Fat6uwgsvBaGQiia8WoXt%2Funknown.png?alt=media&#x26;token=5f5bac29-631d-4c36-986a-eb91bd3d2015" alt=""><figcaption></figcaption></figure>

Note: If port 443 is allowed in the security group but not listening on the server, open and edit ports.conf:

* cd /etc/apache2/
* sudo nano ports.conf

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/7Nw9JhaeKZETHav62jfh/Unknown%20image)

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/Sj4E26t6pjULnf8uGps0/Unknown%20image)

To use a custom port instead of 443:

* Replace 443 with your desired port in ports.conf
* Open that custom port in the Security Group

Restart Apache:

* sudo apachectl configtest
* sudo systemctl restart apache2

{% hint style="info" %}
**Important Note:** Before restarting Apache after any config change, run this command:&#x20;

```
sudo apachectl configtest
```

If output is **Syntax OK**, proceed with a restart.
{% endhint %}

**Step 6: Disable Default Site**

* Cd /etc/sites-enabled&#x20;

```
ls 
```

It lists the enabled default site name

```
sudo a2dissite 000-default.conf
```

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/CkiJtCRao1hNKfdrzveE/Unknown%20image)

**Step 7: Create Reverse Proxy Configuration File**

```
sudo nano /etc/apache2/sites-available/reverse-proxy.conf
```

Add this content to the file :

```
<VirtualHost *:80> 
ServerName apache-poc-test.theacecloud.com
ServerAlias apache-poc-test.theacecloud.com
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
ProxyPreserveHost On
ProxyPass / 
http://localhost:8080/

ProxyPassReverse / 
http://localhost:8080/

ErrorLog ${APACHE_LOG_DIR}/reverse-proxy-error.log
CustomLog ${APACHE_LOG_DIR}/reverse-proxy-access.log combined </VirtualHost>
```

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/xZbAJN91JeiXTimM5a7o/Unknown%20image)

**(Update domain name as per your setup)**

**Step 8: Apply SSL Using Certbot**

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/ytpcBfFmhqkL2OGKrLwX/Unknown%20image)

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/UnOzl3fpuZxgR0rWaIvd/Unknown%20image)

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/VK7iUnwPimTgbggKNswN/Unknown%20image)

**Commands:**

* sudo snap install --classic certbot
* sudo ln -s /snap/bin/certbot /usr/bin/certbot
* sudo certbot –apache

**Follow prompts:**

* Enter email
* Agree to terms – press y
* Select domain (ensure the domain points to the correct server IP)

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/vCzvOLoeUxI0dyHdrdeH/Unknown%20image)

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/6OlYedaFCpTfG1gjxWFO/Unknown%20image)

Once SSL is deployed, it creates:

**reverse-proxy-le-ssl.conf**(Verify its content, this should be present in this file)

![](https://content.gitbook.com/content/VBa3yjTJ7LPbjby0TKRi/blobs/SCrgewWN8kMHQPp6x5mO/Unknown%20image)

Once the above steps are completed, your setup should now be successfully configured.\
Please review the configuration again to verify everything is working correctly.
