Skip to content

How to Setup WordPress with Nginx and Cloudflare on Ubuntu

Richard
Written by
Richard
Apr 22, 2019 Updated Jun 19, 2026 5 min read
How to Change Default Apps in Ubuntu

You set up WordPress with Nginx and Cloudflare on Ubuntu to create a fast and secure web server environment.

This combination leverages Nginx’s speed for web serving, Cloudflare’s CDN and security features, and WordPress for your website content, all running on an Ubuntu operating system.

Specifically, this tutorial guides you through configuring WordPress with Nginx and Cloudflare on Ubuntu 22.04 LTS, delivering optimal performance and robust protection for your site.

⚡ Quick Answer

Set up WordPress with Nginx and Cloudflare on Ubuntu by installing Nginx, MariaDB, and PHP. Configure Cloudflare by adding your domain, setting SSL to Full (strict), and enabling Always Use HTTPS. Then, create Nginx server blocks for your WordPress site and adjust PHP settings.

Why set this up?

You want this setup to protect your site from hackers and to ensure your pages load quickly for visitors. It adds a professional layer of security and performance optimization to your blog or business site.

What happens when done?

Once finished, your website will be fully encrypted with an SSL certificate, hosted on a fast Nginx server, and protected by Cloudflare’s global network.

How to set up Cloudflare

Setting up Cloudflare is a key step to protect and speed up your website, especially when using WordPress with Nginx and Cloudflare on Ubuntu.

Enter your email and create an account.

cloudflare wordpress setup

After logging in, click the button to add a site.

Cloudflare WordPress setup
cloudflare wordpress setup 1

Type in your domain name. registered

Cloudflare WordPress setup
cloudflare wordpress setup 2

Cloudflare will look for your current domain settings. If found, it will import them automatically.

Cloudflare WordPress setup
cloudflare wordpress setup 3

Choose the free plan for this setup.

Cloudflare WordPress setup
cloudflare wordpress setup 4

Cloudflare will give you two nameservers. You must log in to your domain provider’s website and replace your old nameservers with these new ones.

This image has an empty alt attribute; its file name is cloudflare-setup-name-servers.png
cloudflare setup name servers

For example, if you use a service like Google Domains, find the custom nameserver section and paste the Cloudflare addresses there.

Cloudflare WordPress Setup
cloudflare wordpress setup 16

After saving, return to Cloudflare and wait for the status to show as Active. This can take up to an hour.

This image has an empty alt attribute; its file name is cloudflare-overview-active.png
cloudflare overview active

Once active, you will see your DNS records.

Cloudflare WordPress Setup
cloudflare wordpress setup 6

Click the Crypto tab and set your SSL to Full (strict).

Cloudflare WordPress Setup
cloudflare wordpress setup 17

Scroll down to Origin Certificates and click to create one. These help encrypt the connection between Cloudflare and your server.

This image has an empty alt attribute; its file name is cloudflare_wordpress_setup_8.png
cloudflare wordpress setup 8

Let Cloudflare generate the key and click Next.

WordPress Cloudflare
cloudflare wordpress setup 9

Note: You will need admin privileges (sudo) to create these files on your server.

🐧Bash / Shell
sudo nano /etc/ssl/private/cloudflare_key_example.com.pem
🐧Bash / Shell
sudo nano /etc/ssl/certs/cloudflare_example.com.pem

Download the Cloudflare Origin Pull certificate here: Set up authenticated Origin pulls · Cloudflare SSL docs

🐧Bash / Shell
sudo nano /etc/ssl/certs/origin-pull-ca.pem

You should now have three files on your server. Keep these safe.

This image has an empty alt attribute; its file name is cloudflare_wordpress_setup_10.png
cloudflare wordpress setup 10

In the Cloudflare Crypto settings, turn on “Always use HTTPS.”

This image has an empty alt attribute; its file name is cloudflare_wordpress_setup_11.png
cloudflare wordpress setup 11

Enable “Authenticated Origin Pulls” and “Opportunistic Encryption.”

This image has an empty alt attribute; its file name is cloudflare_wordpress_setup_12.png
cloudflare wordpress setup 12

Enable “Automatic HTTPS Rewrites.”

This image has an empty alt attribute; its file name is cloudflare_wordpress_setup_13.png
cloudflare wordpress setup 13

Go to Page Rules and create a rule to always use HTTPS for your domain.

This image has an empty alt attribute; its file name is cloudflare_wordpress_setup_15.png
cloudflare wordpress setup 15

How to install and configure WordPress

1Install Nginx

Requires admin privileges.

🐧Bash / Shell
sudo apt update
sudo apt install nginx

Use these commands to manage the service:

🐧Bash / Shell
sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Test if it works by visiting your server’s address in a browser.

nginx default home page test
nginx default page

2Install MariaDB

MariaDB is a popular database that WordPress needs to store all your site’s information, and you’ll install it on your Ubuntu server next.

🐧Bash / Shell
sudo apt-get install mariadb-server mariadb-client

Manage the service:

🐧Bash / Shell
sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service
🐧Bash / Shell
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Secure your database:

🐧Bash / Shell
sudo mysql_secure_installation

Test the installation:

🐧Bash / Shell
sudo mysql -u root -p
mariadb welcome
mariadb ubuntu 1604

3Install PHP

WordPress runs on PHP, so installing the right version on your Ubuntu system is crucial for your site to function correctly.

🐧Bash / Shell
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
🐧Bash / Shell
sudo apt update
🐧Bash / Shell
sudo apt install php7.2-fpm php7.2-common php7.2-mysql php7.2-gmp php7.2-curl php7.2-intl php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-xml php7.2-cli php7.2-zip

Update the configuration file:

🐧Bash / Shell
sudo nano /etc/php/7.2/fpm/php.ini
💻Code
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

Restart Nginx:

🐧Bash / Shell
sudo systemctl restart nginx.service

Create a test file to verify PHP:

🐧Bash / Shell
sudo nano /var/www/html/phpinfo.php
💻Code

Visit the file in your browser to see the info page.

PHP Test Page
php test page

4Create the Database

Requires admin privileges.

🐧Bash / Shell
sudo mysql -u root -p
💻Code
CREATE DATABASE wpdatabase;
💻Code
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'new_password_here';
💻Code
GRANT ALL ON wpdatabase.* TO 'wpuser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;
💻Code
FLUSH PRIVILEGES;
EXIT;

5Download WordPress

Command Prompt
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
sudo mv wordpress /var/www/html/example.com
🐧Bash / Shell
sudo chown -R www-data:www-data /var/www/html/example.com/
sudo chmod -R 755 /var/www/html/example.com/

6Configure Nginx for WordPress

Configuring Nginx tells your web server how to handle requests for your WordPress site, making sure it runs smoothly.

🐧Bash / Shell
sudo nano /etc/nginx/sites-available/example.com
🐘PHP
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name example.com www.example.com;
root /var/www/html/example.com;
index index.php;

ssl_certificate /etc/ssl/certs/cloudflare_example.com.pem;
ssl_certificate_key /etc/ssl/private/cloudflare_key_example.com.pem;
ssl_client_certificate /etc/ssl/certs/origin-pull-ca.pem;
ssl_verify_client on;

client_max_body_size 100M;

autoindex off;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

7Enable the site

Enabling your WordPress site with Nginx connects everything, allowing you to finish the setup wizard and start using your new website.

🐧Bash / Shell
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

Visit your domain in a browser to finish the WordPress setup wizard.

WordPress Ubuntu Installation
wordpress 50 ubuntu install

Enter the database details you created earlier.

WordPress Ubuntu Installation
wordpress 50 ubuntu install 1
WordPress Ubuntu Installation
wordpress 50 ubuntu install 2
WordPress Ubuntu Installation
wordpress 50 ubuntu install 3

Create your admin account.

WordPress installation on Ubuntu
wordpress 50 ubuntu install 4
WordPress Ubuntu install
wordpress 50 ubuntu install 5

Finally, install the official Cloudflare plugin on your WordPress dashboard to finish the integration.

Summary

By following these steps, you have successfully secured your website with Cloudflare SSL, set up a high-performance Nginx web server, and installed WordPress with a dedicated MariaDB database on your Ubuntu Linux system.

[Y/n]

[Y/n]

[Y/n]

[Y/n]

[Y/n]









Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

No comments yet — be the first to share your thoughts!

Leave a Comment

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

Exit mobile version