WordPress: Tutorial & Best Practices

A Popular Content Management System

WordPress is a versatile and widely-used Content Management System (CMS) that allows you to create stunning websites with ease. Although it’s often associated with blogs, WordPress can power anything from e-commerce sites to portfolios. Let's dive into the nuts and bolts of WordPress on a Linux server.

What is WordPress?

WordPress is an open-source CMS written in PHP and paired with a MySQL or MariaDB database. It’s designed to be user-friendly, even for those without extensive technical knowledge. The platform boasts a vast library of plugins and themes, making it highly customizable and suitable for almost any kind of website.

Installing WordPress

Typically, WordPress isn't installed by default on a Linux server. Here’s how to get it up and running:

  1. Update Your Server: Keep your server updated to ensure you have the latest security patches.

    sudo apt update && sudo apt upgrade
    
  2. Install Apache, MySQL, and PHP (LAMP Stack): You need the LAMP stack to run WordPress.

    sudo apt install apache2
    sudo apt install mysql-server
    sudo apt install php libapache2-mod-php php-mysql
    
  3. Download WordPress: Get the latest version of WordPress.

    wget -c http://wordpress.org/latest.tar.gz
    tar -xzvf latest.tar.gz
    
  4. Configure Database: Create a database and a user for WordPress.

    sudo mysql -u root -p
    CREATE DATABASE wordpress;
    CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    
  5. Configure Apache: Set up Apache to serve your WordPress site.

    sudo cp -r wordpress /var/www/html/
    sudo chown -R www-data:www-data /var/www/html/wordpress
    sudo chmod -R 755 /var/www/html/wordpress
    sudo nano /etc/apache2/sites-available/wordpress.conf
    

    Add the following to wordpress.conf:

    <VirtualHost *:80>
        ServerAdmin admin@example.com
        DocumentRoot /var/www/html/wordpress
        ServerName example.com
        ServerAlias www.example.com
        <Directory /var/www/html/wordpress/>
            Options FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    Enable the new site and rewrite module:

    sudo a2ensite wordpress.conf
    sudo a2enmod rewrite
    sudo systemctl restart apache2
    
  6. Run WordPress Installation Script: Navigate to http://your_server_ip/wordpress and follow the on-screen instructions.

Typical Problems and Troubleshooting

  • Database Connection Errors: Ensure your database credentials in wp-config.php are correct.
  • Permission Issues: Double-check the ownership and permissions of your WordPress files.
  • High Load: Use caching plugins and consider a Content Delivery Network (CDN) to reduce server load.

Best Practices

  • Regular Updates: Keep WordPress, themes, and plugins up-to-date to avoid security vulnerabilities.
  • Backups: Regularly back up your site and database. Tools like rsync and plugins like UpdraftPlus can automate this.
  • Security: Use strong passwords, limit login attempts, and consider a firewall like ufw or iptables.
  • Performance Optimization: Use caching mechanisms like W3 Total Cache or WP Super Cache, and optimize your database using plugins like WP-Optimize.

Examples

Here's a simple example of creating a daily backup script for your WordPress site:

#!/bin/bash
DATE=$(date +%F)
BACKUP_DIR="/path/to/backup"
MYSQL_USER="wp_user"
MYSQL_PASSWORD="password"
MYSQL_DATABASE="wordpress"
WP_DIR="/var/www/html/wordpress"

# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR

# Backup MySQL database
mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE > $BACKUP_DIR/db_backup_$DATE.sql

# Backup WordPress files
tar -czf $BACKUP_DIR/wp_files_backup_$DATE.tar.gz $WP_DIR

# Optionally, you can sync this backup to a remote server using rsync
# rsync -avz $BACKUP_DIR remote_user@remote_host:/path/to/remote/backup

echo "Backup completed on $DATE"

Run this script daily using a cron job to ensure you have regular backups.