WordPress is a CMS (content manager system) that is to say it is an application that allows you to create a blog of information. However, over time has evolved and not only provides services to create blogs but corporate websites or what you can imagine.
Prerequisites
- Apache
- MySQL/MariaDB
- PHP
Install LAMP Stack and Required PHP Extensions
sudo apt-get install -y apache2 mariadb-server php libapache2-mod-php php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,cli,fpm,opcache,xml,curl,intl,xsl,soap,json,apcu,imap}
Secure MySQL/MariaDB database server
Then, set a root password and secure the installation.
sudo mysql_secure_installation
Say “y” to all the questions.
Create a database for WordPress
sudo mysql -uroot -p (provide your MySQL root password)
create database wordpress;
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'wordpress123';
GRANT ALL PRIVILEGES ON *.* TO 'wordpress'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
quit;
Install WordPress
Now it is your turn to install WordPress. However, first, you have to download it. For that, I will use wget.
So, run the following command:
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar xvfz latest.tar.gz
sudo chown -R www-data:www-data wordpress
sudo chmod 755 -R wordpress
sudo chmod -R 777 /var/www/html/wordpress/wp-content
Create Virtualhost for WordPress
sudo nano /etc/apache2/sites-available/wordpress.conf
And add the following:
<VirtualHost *:80>
ServerAdmin admin@your_domain.com
DocumentRoot /var/www/html/wordpress
ServerName your-domain.com
<Directory /var/www/html/wordpress>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined
</VirtualHost>
Replace “your-domain” with yours. Save the changes and close the file.
Then you have to enable the new Virtualhost and also the Apache rewrite module. And for all this to work, then we’ll have to restart Apache.
sudo ln -s /etc/apache2/sites-available/wordpress.conf /etc/apache2/sites-enabled/wordpress.conf
sudo a2enmod rewrite
sudo systemctl reload apache2
sudo systemctl restart apache2
Now complete the installation
Now, you have to complete the installation using the web browser. Access to your server and you will see this a prompt where you need to provide database connection details and create an admin account
Automate WordPress Backups
Here's what we will be backing up:
- WordPress database: The WordPress database contains all your text-based content including your posts, pages, comments, tags, categories, links, etc.
- WordPress Uploads Folder: The uploads folder contains all your images/media files.
- WordPress Theme Folder: The theme folder contains your theme files. This includes all your theme customizations.
- WordPress Plugin Folder: The folder contains the plugin which you have installed
Your backups will be compressed using gzip and you will be able to open them using a program like WinRar or Winzip.
Before beginning, here are the steps involved in a nutshell:
- Create a backup folder in the server to save the backups in.
- Create a .sh file with the bash shell script and upload it to the server.
- Create a cron job to call and run this script on a daily basis.
- Sync the backed-up files and save them in Google Drive.
So let's look at these steps in detail:
Creating the Backup Folder
The first step is to create a backup folder on your server. This is the folder where all your backup files will be saved. You can create this folder under your server's etc folder or simply create a new folder in your home directory as follows:
Step 1: SSH to your server as the root user. I usually access the server using a terminal in Linux. You can use Putty in windows for accessing the server.
Step 2: Go to your home directory.
cd /home/username
Step 3: Create a folder named 'wp-backup' in the home directory.
sudo mkdir wp-backup
Adding the Shell Script
We will now create a sh file with the bash shell script inside our backup folder using any text editor, I am here using vim, if you are not familiar with vim, use nano.
Create a file named backup.sh and add the following code to it modify with your username and WordPress database details
sudo vim /usr/local/bin/wpbackup.sh
#!/bin/bash
## 1: Your backups will use these filenames.
db_backup_name="wp-db-backup-$(date +%d-%m-%Y-%H.%M).sql.gz"
wpfiles_backup_name="wp-files-backup-$(date +%d-%m-%Y-%H.%M).tar.gz"
## 2: Database connection info. You can get these details from your wp-config file.
db_name="wordpress"
db_username="wordpress"
db_password="wordpress123"
## 3: Path to your WordPress Upload and Theme directories. Replace /home/mahesh/ with path to your home directory.
wp_upload_folder="/var/www/html/wordpress/wp-content/themes"
wp_theme_folder="/var/www/html/wordpress/wp-content/uploads"
wp_plugin_folder="/var/www/html/wordpress/wp-content/plugins"
## 4: Path to your backup folder. Replace /home/username/ with the path to your home directory.
backup_folder_path="/home/username/wp-backup"
## 5: Backup MYSQL database, gzip it and send to backup folder.
mysqldump --opt -u$db_username -p$db_password $db_name | gzip > $backup_folder_path/$db_backup_name
## 6: Create a tarball of the wordpress files, gzip it and send to backup folder.
tar -czf $backup_folder_path/$wpfiles_backup_name $wp_upload_folder $wp_theme_folder $wp_plugin_folder
## 7: Sync to Google Drive using rclone
rclone sync /home/username/wp-backup Gdrive:UPCLOUD-DBBACKUPS
## 8: Delete all but 5 recent wordpress database back-ups (files having .sql.gz extension) in the backup folder.
find $backup_folder_path -maxdepth 1 -name "*.sql.gz" -type f | xargs -x ls -t | awk 'NR>5' | xargs -L1 rm
## 9: Delete all but 5 recent wordpress files back-ups (files having .tar.gz extension) in backup folder.
find $backup_folder_path -maxdepth 1 -name "*.tar.gz" -type f | xargs -x ls -t | awk 'NR>5' | xargs -L1 rm
Give the necessary permission to the shell script and assign the task in cron according to your time
sudo chmod +x /usr/local/bin/wpbackup.sh
crontab -e
#WordPress backup
0 0 * * * /usr/local/bin/wpbackup.sh
This script backs up the WordPress database and then gzips it and saves it in the backup folder.
The backups will follow this naming convention:
Database Backup: wp-db-backup-day-month-year-hour-minute.sql.gz eg: wp-db-backup-30-08-2021-08.55.sql.gz
How to configure Gdrive using rclone
Reference:
Really a great post. Appreciate the effort in educating us. I was searching for best cms for seo and came across this blog. Thanks for sharing.
ReplyDelete