Blog

WordPress 5 Minute Install

This setup assumes that you are running a web server and mysql on the same server.

Step 1

cd into your root directory.

cd /your/root/directory

Step 2

Download and unzip.

wget https://wordpress.org/latest.tar.gz && tar -xzvf latest.tar.gz && rm -rf latest.tar.gz && cd wordpress && mv * .. && cd .. && rm -rf wordpress

Step 3

Create database and user within mysql.

Note: Update database_name, username and password fields.

mysql -u root -p
mysql> CREATE DATABASE database_name;
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON databasename.* TO '<username>'@'localhost';

Step 4

Complete WordPress install.

http://example.com/wp-admin/install.php

You should see the following screen (click Let’s go!).

wordpess-setup01

Fill out Database Name, User Name and Password. Leave the rest as default.

wordpess-setup02

Once you’ve filled out your database information, it will prompt you to create a  wp-config.php file within your root directory. Copy and paste the auto-generated code into wp-config.php and save it.

vi wp-config.php

Step 5

Setup Permalinks.

This is not really part of the install steps, but I’m adding it here because I think it’s pretty important and convenient to setup at this point of the installation.

Once logged in to WordPress, go to Settings > Permalinks.

Select Day and name.

wordpress-permalink

You will need to create an .htaccess file within your root directory and copy the code from the bottom of the page.

vi .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

That’s it!

WordPress Full Width Page Template twentysixteen-child

If you’ve read my previous blogs, you’d know I am using a child theme. I noticed that the theme didn’t come with a full width page template, so here are the simple steps to create one within your child theme.

Step 1

Copy page.php from the twentysixteen folder into your twentysixteen-child theme folder as a new file named page-wide.php.

cp /var/www/itsmetommy.com/public_html/wp-content/themes/twentysixteen/page.php /var/www/itsmetommy.com/public_html/wp-content/themes/twentysixteen-child/page-wide.php

Step 2

Edit page-wide.php within your child theme folder.

Add the following at the top of the page – remove all other lines between /** and */.

/**
Template Name: Wide Template
*/

Delete the following line.

<?php get_sidebar(); ?>

Change the class from content-area to content-area-wide.

<div id="primary" class="content-area-wide">

Step 3

Add the following to style.css within your child theme folder.

.content-area-wide {
 float: left;
 margin-right: -100%;
 width: 100%;
 }

You will now have the option to choose Wide Template within WordPress Pages.

wordpress-wide-template

Password Protect Website

There are two files that must be inside your root directory in order to password protect your site.

  • .htaccess — the .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.
  • .htpasswd — .htpasswd is a flat-file used to store usernames and password for basic authentication on an Apache HTTP Server.

Step 1

cd into the main directory you want to password protect.

cd /your/root/directory

Step 2

Create the .htaccess file

Replace /your/root/directory.

vi .htaccess
AuthType Basic 
AuthName "restricted area" 
AuthUserFile /your/root/directory/.htpasswd 
require valid-user

Step 3

Create the .htpasswd file

Create new htpasswd file and add user (you will be asked to enter your password).

Replace username.

htpasswd -c .htpasswd username

OR

Add additional users.

htpasswd .htpasswd username

Not only does password protecting your website keep users out, but it also removes your site from search results.

WordPress Staging Website

There are many methods to create a staging site with software plugin-ins, but I’m going with a more manual approach in this blog. I am keeping the staging site on the same server as my live site to keep the server environments the exact same, not to mention it makes backups a little easier.

Create a staging directory within your root web directory

mkdir /var/www/itsmetommy.com/public_html/staging

Copy all files from your root directory into your staging directory

cp -R /var/www/itsmetommy.com/public_html/ /var/www/itsmetommy.com/public_html/staging

Note: You will get the expected following message.

cp: cannot copy a directory, `staging', into itself, `/var/www/itsmetommy.com/public_html/staging'

Password protect the staging directory

Not only does password protecting your site keep users out, but it also removes your site from search results.

cd into your staging directory.

cd /var/www/itsmetommy.com/public_html/staging

Create .htacess

IMPORTANT: Add a mod_rewrite. Update the RewriteRule line if your sub-folder is not named staging.

  • .htaccess — the .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.
vi .htaccess
AuthType Basic 
AuthName "restricted area" 
AuthUserFile /your/root/directory/.htpasswd 
require valid-user

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /staging/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /staging/index.php [L]
</IfModule>
# END WordPress

Create .htpasswd

  • .htpasswd — .htpasswd is a flat-file used to store usernames and password for basic authentication on an Apache HTTP Server.

Replace tommy with your name.

htpasswd -c .htpasswd tommy

Create staging database

Here comes the good part.

You will need to create a new staging database, export your live database, import your exported database into your newly created staging database, then modify the staging database to reflect its new location. Did you get all that? Good. Now lets get to it.

Step 1 – Create a new staging database

mysqladmin -u root -p create itsmetommy_staging

Step 2 – Export your live database

mysqldump -u root -p itsmetommy > itsmetommy.sql

Step 3 – Import your exported database into your newly created staging database

mysql -u root -p itsmetommy_staging < itsmetommy.sql

Step 4 – Modify the staging database to reflect its new location

Login to mysql as root.

mysql -u root -p

Give user access to new staging database.

mysql> GRANT ALL PRIVILEGES ON itsmetommy_staging.* TO 'tommy'@'localhost';

Or if your website and database is on a separate server.

mysql> GRANT ALL PRIVILEGES ON itsmetommy_staging.* TO 'tommy'@'10.176.11.65';

Switch to your staging database.

mysql> use itsmetommy_staging;

View current home and siteurl.

mysql> SELECT * FROM wp_options WHERE option_name = 'home' OR option_name = 'siteurl';
+-----------+-------------+------------------------+----------+
| option_id | option_name | option_value | autoload |
+-----------+-------------+------------------------+----------+
| 37 | home | http://itsmetommy.com/ | yes |
| 1 | siteurl | http://itsmetommy.com | yes |
+-----------+-------------+------------------------+----------+
2 rows in set (0.00 sec)

Update home and siteurl.

mysql> UPDATE wp_options SET option_value = replace(option_value, 'http://itsmetommy.com', 'http://itsmetommy.com/staging') WHERE option_name = 'home' OR option_name = 'siteurl';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0

Confirm the new home and siteurl value.

mysql> SELECT * FROM wp_options WHERE option_name = 'home' OR option_name = 'siteurl';
+-----------+-------------+--------------------------------+----------+
| option_id | option_name | option_value | autoload |
+-----------+-------------+--------------------------------+----------+
| 37 | home | http://itsmetommy.com/staging/ | yes |
| 1 | siteurl | http://itsmetommy.com/staging | yes |
+-----------+-------------+--------------------------------+----------+
2 rows in set (0.00 sec)

We now want to fix the urls of posts and pages.

Update guid.

mysql> UPDATE wp_posts SET guid = replace(guid, 'http://www.itsmetommy.com','http://itsmetommy.com/staging');

If you have linked internally within blog posts or pages with absolute URLs, these links will point to wrong locations after you move the blog location.

Update post_contet.

mysql> UPDATE wp_posts SET post_content = replace(post_content, 'http://www.itsmetommy.com', 'http://itsmetommy.com/staging');

Update meta_value.

mysql> UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.itsmetommy.com','http://itsmetommy.com/staging');

Update your WordPress config file to reflect the new database

vi wp-config.php
define('DB_NAME', 'itsmetommy_staging');

Confirm that everything works by going to http://yourdomain.com/staging.

That’s it! Give yourself some credit if you made it this far.

WordPress There has been an error cropping your image

I ran into an issue when cropping my header image within WordPress. Turns out, WordPress uses the php-gd library to crop the images.

wordpress_header_crop_error

Redhat / CentOS

yum install php-gd

Ubuntu

apt-get install php5-gd

Restart your web server (apache, httpd or nginx)

/etc/init.d/httpd restart