Cloudflare Origin SSL certificate

How to Configure Cloudflare Origin CA for Apache

Last updated on

Prerequisites

You should already have your Apache server configured and serving web pages for your domain. We have several guides to help you set up an Apache web server from scratch on Ubuntu 20.04 / 18.04 / 19.10.

1. Generate Cert and Private Key

Log in to Cloudflare and navigate to the Crypto page.

Scroll down to Origin Certificates and click Create Certificate.

Cloudflare Origin certificate

In Origin Certificate Installation, the defaults should be Private Key Type: RSA with 15 years validity. Click Next.

Cloudflare Origin certificate

In the next screen, the Key format should be PEM (default) and Web Server for Installation: Apache httpd.

Copy your Origin Certificate and Private Key to a text editor for later. We will need to paste these into Linux.

Finally, click OK to close.

Cloudflare Origin certificate

2. Copy Cert and Key to Server

Create a new directory where our Cert and Key will reside.

sudo mkdir -p /etc/cloudflare/

Using nano text editor, create a new file example.com.pem (where example.com is your own domain).

sudo nano /etc/cloudflare/example.com.pem

Now paste in your Origin Certificate from Cloudflare.  Save file and exit. (Press CTRL + X, press Y and then press ENTER)

Create a new file example.com.key (where example.com is your own domain).

sudo nano /etc/cloudflare/example.com.key

Paste in your Private Key from Cloudflare.  Save file and exit. (Press CTRL + X, press Y and then press ENTER)

3. Configure Apache

Firstly, make sure you have the SSL module enabled for Apache by running:

sudo a2enmod ssl

Open the Apache configuration file for your domain. This is usually located in /etc/apache2/sites-available/

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

There should already be a block for <VirtualHost *:80>. You need to add a new block underneath it for SSL port 443. You can also add a rewrite condition in your port 80 block to redirect all requests to https.

Below is an example.

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>

    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =example.com [OR]
    RewriteCond %{SERVER_NAME} =www.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

<VirtualHost *:443>

    ServerAdmin [email protected]example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on
    SSLCertificateFile /etc/cloudflare/example.com.pem
    SSLCertificateKeyFile /etc/cloudflare/example.com.key

</VirtualHost>

Save file and exit. (Press CTRL + X, press Y and then press ENTER)

Test the configuration syntax for errors.

apachectl configtest

You can ignore any errors that say Could not reliably determine the server’s fully qualified domain name

If you see Syntax OK, restart Apache.

sudo systemctl restart apache2

Note, it can take 5 or 10 minutes for Cloudflare to deploy certs.

Make sure that SSL in Cloudflare is set to Full (Strict). This will ensure that data between Cloudflare and your server is always encrypted.

Getting the Real Client IP Address from Cloudflare in Apache

While you’re still here.. I recently wrote an article which may be of interest to you.

As may or may not know, when you enable Cloudflare on your site, the IP address that appears in your Apache access and error logs is that of the Cloudflare proxy and not the real client’s IP. As you can imagine, this is a security concern should you ever need to audit your logs or use IP restrictions with .htaccess.

Thankfully, Cloudflare sends the real client IP in the HTTP headers as CF-Connecting-IP, and you can configure Apache to use this IP in your logs instead of Cloudflare’s proxy IP.

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (3 votes)

Let me know in the comments if this helped. Follow me on Twitter, Facebook and YouTube.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

9 replies

avatar
  Subscribe  
newest oldest
Notify of
Stepan
Guest
Stepan
Stepan
3 months ago

I have a problem. I’ve got handshake error, and i dont know where to check the error.

Dec
Guest
Dec
Dec
5 months ago

anyone know how to include the root origin cert? how do we use it when cloudflare already generated the normal certificate.

do we add it to the end of that file ? any help much appreciated

johnm
Guest
johnm
johnm
7 months ago

AH00526: Syntax error on line 26 of /etc/apache2/sites-enabled/example.conf:
Invalid command ‘SSLEngine’, perhaps misspelled or defined by a module not included in the server configuration
Action ‘configtest’ failed.

andy
Guest
andy
andy
7 months ago

I get as far as the virtual host bit and when I type the command it opens nano and it is blank..anybody help or know why..

Thanks

seonglae
Guest
seonglae
seonglae
7 months ago

i occured error 525 ssl handshake failed
i have open 433 port how can i resolve this!?

Mevo
Guest
Mevo
Mevo
6 months ago

Hi, I ran into the same problem. I had to run “a2ensite xxx.conf” (the conf file you edited). It enables the VirtualHost, and also creates a conf file in the “sites-enabled” directory. After just that, boom, worked.

John B
Guest
John B
John B
1 year ago

Great walkthrough. On the final step I was running into a “Invalid command ‘SSLEngine'” error, needed to run “sudo a2enmod ssl” to fix this.