Change WordPress Existing Images Path Using mySQL Database Strings

wordpress

I rebuilt my WordPress blog and had to change the image path, instead of changing it back straight away I kept using it, some images ended up in /wpcontents/uploads/* and some ended up in /wp-contents/uploads/*

So, naturally I had to change this back to /wp-contents/uploads and move all the existing files back, moving the files back wasn’t a problem but changing the image locations in the database was (without doing this completely manually and spending days on it)!

There are two ways of doing this, which will produce the same result, one is using phpMyAdmin and the other is using the command line and issuing sql commands. I will be only demonstrating the command line way as I don’t have phpMyAdmin installed, nor do I want to install it.

 

1. Shell to the server

2. Log into mySQL

mysql -u root -p
<enter password>

3. Select database

use <database>;

4. Run the following

You will need to run both commands, change example.com and your path to the new ones you want to use)

Update wp_posts (post_content)

UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://www.example.com/image/path','http://www.example.com/new/image/path');

Update wp_posts (guid)

UPDATE wp_posts SET guid = REPLACE (guid,'http://www.example.com/image/path','http://www.example.com/new/image/path');

You should get a result like:

wordpress wp_posts sql update

Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.

Sending Files to Linux Servers using SCP

linux-logo-300x300You might need to send files between Linux servers, a great way to send files quickly and securely is by using the SCP command.

Sending to a Host

scp /home/user/file.ext [email protected]:/home/user/folder

scp = The secure file transferring program
/home/user/file.ext = The full path of the file you want to send
[email protected] = Username at host you want to send files to
:/home/user/folder = The full path of where you want to place the file

Receiving from a Host

scp [email protected]:/home/user/folder/file.ext /home/user/folder

scp = The secure file transferring program
[email protected] = Username at host you want to connect to
:/home/user/folder/file.ext = The full path of the file you want to retrieve
/home/user/folder = The full path of where you want to place the file

Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.

Install Varnish on Linux Debian or Ubuntu for WordPress

varnishcache_rgb-gimp2-alpha

 

What is Varnish?

(From Wikipedia) Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.

My Implementation

I implemented Varnish on this WordPress blog, the technical layout is as follows:

Varish Design

1. Install Varnish on Debian or Ubuntu:

apt-get update
apt-get install varnish

2. Edit /etc/varnish/default.vcl

backend default {
            .host = "localhost";
            .port = "8080";
            .max_connections = 30;
            .connect_timeout = 4.0s;
            .first_byte_timeout = 600s;
            .between_bytes_timeout = 600s;
}
# Drop any cookies sent to WordPress.
sub vcl_recv {
            if (!(req.url ~ "wp-(login|admin)")) {
                       unset req.http.cookie;
            }
}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
            if (!(req.url ~ "wp-(login|admin)")) {
                       unset beresp.http.set-cookie;
            }
}

3. Edit /etc/default/varnish

START=yes
NFILES=131072
DAEMON_OPTS="-a :80 
             -T localhost:6082 
             -b localhost:8080 
             -u varnish -g varnish 
             -s malloc,1G"

4. Kill the service

pkill varnishd

5. Edit /etc/apache2/ports.conf and change the listening ports to 8080

NameVirtualHost *:8080
Listen 8080

6. Edit all your vhosts to match the port 8080

Example:

<VirtualHost *:8080>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/vhosts/example.com
       <Directory /var/www/vhosts/example.com>
               Options Indexes FollowSymLinks MultiViews
               AllowOverride all
       </Directory>
        ErrorLog /var/log/apache2/example.com-error.log
        CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>

Default:

[code language="bash"]
<VirtualHost *:8080>
        ServerAdmin [email protected]

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        <</Directory>
        <<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

7. Launch Varnish

/etc/init.d/apache2 restart
varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000

Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.

Setting Locale in Linux Debian or Ubuntu

linux-logo-300x300

Setting the locale in Linux Debian or Ubuntu is quite easy, usually it is because you get the following errors:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
 LANGUAGE = (unset),
 LC_ALL = (unset),
 LANG = "en_AU.UTF-8"
 are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Module status already enabled

To fix the problem, run the following command:

dpkg-reconfigure locales

Select the location

Reconfigure Locales

Select the locale as default

Default Locale

 

Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.

Mount Amazon S3 Bucket on Ubuntu

aws

Mount Amazon S3 Buckets on Ubuntu

I wanted the ability to mount a S3 bucket straight onto my Linux server so I wouldn’t have to rely on local storage, I have a write up on how to do this:

You can do this without installing FUSE just by installing s3fs but that only allows the user who mounted it to access the bucket and it is also not possible to mount using /etc/fstab

Before you run any commands, make sure your system is up to date by running:

apt-get update && apt-get -y upgrade

Ubuntu Dependancies

apt-get install build-essential pkg-config libfuse-dev libcurl4-openssl-dev libxml2-dev automake

Check Fuse Package

pkg-config --modversion fuse

Install s3fs

Download the latest verion of s3fs from the Google Code here

Download the latest version of S3fs from Github

unzip master.zip

Change directory into the newly extracted s3fs folder

./autogen.sh
./configure
make
make install

Configure time

This is important and missing from most how-to guides! If the time of the local server isn’t within 15 minutes of the Amazon server time, it won’t connect.

Install NTP

apt-get install ntp

Edit ntp.conf

vi /etc/ntpd.conf

Comment out the existing time servers and add the following

server 0.amazon.pool.ntp.org iburst
server 1.amazon.pool.ntp.org iburst
server 2.amazon.pool.ntp.org iburst
server 3.amazon.pool.ntp.org iburst

Restart ntpd

/etc/init.d/ntp restart

Check the time of the Amazon server

telnet s3.amazonaws.com 80
GET / HTTP/1.0
<enter>

It should give you some output like this example:

telnet s3.amazonaws.com 80
Trying 54.231.244.4...
Connected to s3-2.amazonaws.com.
Escape character is '^]'.
GET / HTTP/1.0

HTTP/1.1 307 Temporary Redirect
x-amz-id-2: iXm0zQv6nf7WAeXYC3rnenEAxPjgnGHKtQP9nNeU82D1hYqDlFvItwFtpJ2u2fP6
x-amz-request-id: FB13695A8C2A8539
Date: Tue, 25 Nov 2014 23:49:00 GMT
Location: http://aws.amazon.com/s3/
Content-Length: 0
Connection: keep-alive
Server: AmazonS3

Connection closed by foreign host.

Change the timezone on your server to GMT time (as this is the timezone of the Amazon server)

dpkg-reconfigure tzdata

Select “None of the above”

Select “GMT”

Set the date and time on your server

Change the values to the same as the Amazon server time

date --set YYYY-MM-DD
date --set HH:MM:SS

Configuring s3fs

Create a file called “passwd-s3fs under /etc

vi /etc/passwd-s3fs

Put your access key and secret access key seperated by a “:”

Example: ACCCESS_KEY_ID:SECRET_ACCESS_KEY

Change the permissions for security reasons

chmod 640 /etc/passwd-s3fs

Create a tmp folder for caching

mkdir -p /tmp/cache

Create a mount point

mkdir -p /mnt/aws

Change permissions on cache and mount point folders

chmod 777 /mnt/aws /tmp/cache

Mounting s3 Bucket

Mount bucket

s3fs -o allow_other -o use_cache=/tmp/cache your-bucket-name /mnt/aws

Allow a few minutes to sync

Check to see if it is mounted:

df -h

Example:

aws

Automounting

To mount automatically at boot, your /etc/fstab should look like this:

s3fs#bucketname /mnt/aws fuse allow_other 0 0

A brief description of the mount arguments:

  • Allow_other = allow all users to access the mount point
  • _netdev = The filesystem resides on a device that requires network access
  • nosuid = Do not allow set-user-identifier or set-group-identifierbits to take effect
  • nodev = Do not interpret character or block special devices on the file system
  • url = Use HTTPS instead of HTTP when configure as above

Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.

Scripts to mount bucket

Mounting

Replace “bucketname” with the bucket you wish to mount and change the mount location (/mnt/aws)

#!/bin/bash
/etc/init.d/ntp start
sleep 1m
s3fs -o allow_other -o use_cache=/tmp/cache bucketname /mnt/aws

Unmounting

#!/bin/bash
fusermount -u /mnt/aws

Errors

RequestTimeTooSkewed

The difference between the request time and the current time is too large

This is caused because the Amazon server time is not within 15 minutes of your local server time, check to make sure you have set the correct timezone, date and time in the instructions above.

Troubleshooting

1. The best way to find out if there are any problems is to run s3fs with the -f switch as it will give you full output of the what it is trying to do.

2. Check /var/log/syslog for any messages