Recent Posts

Setting up Wordpress on Raspberry Pi 4 (raspbian buster) using Cloudflare,NGINX - Part 2



To install Nginx server, run the commands below:

sudo apt update sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always start up with the server boots,

sudo systemctl stop nginx.service sudo systemctl start nginx.service sudo systemctl enable nginx.service

Now that Nginx is installed. to test whether the web server is working, open your browser and browse to the URL below.

https://localhost



If you see the page above, then Nginx is successfully installed.

WordPress also requires a database server to store its content.If you’re looking for a truly open source database server, then MariaDB is a great place to start. To install MariaDB run the commands below:


sudo apt-get install mariadb-server mariadb-client


After installing MariaDB, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

Run the following commands


sudo systemctl stop mysql.service sudo systemctl start mysql.service sudo systemctl enable mysql.service


Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.


sudo mysql_secure_installation


When prompted, answer the questions below by following the guide.

Enter current password for root (enter for none): Just press the Enter

Set root password? [Y/n]: Y

New password: Enter password

Re-enter new password: Repeat password

Remove anonymous users? [Y/n]: Y

Disallow root login remotely? [Y/n]: Y

Remove test database and access to it? [Y/n]:  Y

Reload privilege tables now? [Y/n]:  Y


Now that MariaDB is installed, to test whether the database server was successfully installed, run the commands below.


sudo mysql -u root -p

type the root password when prompted.


WordPress CMS is a PHP based CMS and PHP is required.
run the commands below to install PHP 7.2-FPM and related modules.

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

After installing PHP 7.2, run the commands below to open PHP default configuration file for Nginx…

sudo nano /etc/php/7.2/fpm/php.ini


The lines below is a good settings for most PHP based CMS. Update the configuration file with these and save,

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

Everytime you make changes to PHP configuration file, you should also restart Nginx web server. To do so, run the commands below:

sudo systemctl restart nginx.service

Now that you’ve installed all the packages that are required for WordPress to function, continue below to start configuring the servers. First run the commands below to create a blank WordPress database.

To logon to MariaDB database server, run the commands below.

sudo mysql -u root -p

Then create a database called wordpress

CREATE DATABASE wordpress;

Create a database user called user with a new password

CREATE USER 'user'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON wordpress.* TO 'user'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

To get WordPress latest release you will need to go to its official download page and get it from there… The link below is where to find WordPress latest archive versions…

https://wordpress.org/download/

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
sudo mv wordpress /var/www/html/example.com

Then run the commands below to set the correct permissions for WordPress root directory and give Nginx control,

sudo chown -R www-data:www-data /var/www/html/example.com/
sudo chmod -R 755 /var/www/html/example.com/


Run the commands below to create a new configuration file called example.com

sudo nano /etc/nginx/sites-available/example.com

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

Also make sure to reference the certificate files created above during Cloudflare setup.

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_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; 
    } 



Save the file and exit.

After configuring the VirtualHost above, enable it by running the commands below

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

Then open your browser and browse to the server domain name. You should see WordPress setup wizard to complete. Please follow the wizard carefully.

https://example.com/

Then follow the on-screen instructions, Select the installation language then click Continue



You will need to know the following items before proceeding…. Use the database connection info you created above….

Database name

Database username

Database password

Database host

Table prefix (if you want to run more than one WordPress in a single database)



Finish the setup,

and coming to the final part as we have to update ip resolver into cloudflare dns record.

we have to run a script and update your details,

#!/bin/bash
# Cloudflare as Dynamic DNS
# From: https://letswp.io/cloudflare-as-dynamic-dns-raspberry-pi/
# Based on: https://gist.github.com/benkulbertis/fff10759c2391b6618dd/
# Original non-RPi article: https://phillymesh.net/2016/02/23/setting-up-dynamic-dns-for-your-registered-domain-through-cloudflare/

# Update these with real values

auth_email="email@example.com"
auth_key="global_api_key_goes_here" 
zone_name="example.com"
record_name="home.example.com"

# Don't touch these

ip=$(curl -s http://ipv4.icanhazip.com)
ip_file="ip.txt"
id_file="cloudflare.ids"
log_file="cloudflare.log"

# Keep files in the same folder when run from cron

current="$(pwd)"
cd "$(dirname "$(readlink -f "$0")")"

log() {

    if [ "$1" ]; then
        echo -e "[$(date)] - $1" >> $log_file
    fi
}

log "Check Initiated"


if [ -f $ip_file ]; then

    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
        log "IP has not changed."
        exit 0
    fi
fi

if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then

    zone_identifier=$(head -1 $id_file)
    record_identifier=$(tail -1 $id_file)
else
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json"  | grep -Po '(?<="id":")[^"]*')
    echo "$zone_identifier" > $id_file
    echo "$record_identifier" >> $id_file
fi

update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")


if [[ $update == *"\"success\":false"* ]]; then

    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
    exit 1 
else
    message="IP changed to: $ip"
    echo "$ip" > $ip_file
    log "$message"
    echo "$message"
fi

save the file as cloudflare.sh inside pi directory,

run the command,

bash cloudflare.sh

I have done the same as above, https://blog.techiebouncer.com

Post a Comment

0 Comments