WordPress Permalinks on Nginx

If you’re running WordPress on Nginx, you may have an issue with WordPress permalinks. This article explains nicely what you need to do to configure Nginx. What the article failed to do is mention the Nginx configuration filename and its location. The filename is nginx.conf, and it’s usually located in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx, depending on your Linux distro.

# WordPress installed on root directory
location / {
  index index.php index.html index.htm;
  try_files $uri $uri/ /index.php?$args;
# WordPress installed on /blog sub-directory
location /blog/ {
    try_files $uri $uri/ /blog/index.php?$args;

You need to reload Nginx for changes to take effect. For Ubuntu:

$ sudo service nginx restart
# or
$ sudo /etc/init.d/nginx restart

Migrations in Laravel

Migrations in Laravel is like having version control of your database. It gives you the ability to create, modify and share a schema, as well as drop tables. Since it has version control, you can migrate and rollback your database changes with just a couple of CLI commands. Here’s a couple of examples.

To migrate:

$ php artisan migrate

To rollback:

$ php artisan migrate:rollback

In addition, you can also create a new migration table.

$ php artisan make:migration create_users_table --create="users"

It will create a file similar to this:


which you can modify and add fields.

PSR-2 Coding Style Guide

Laravel recently started using the PSR-2 style guide. PSR-2 is a coding style agreed upon by many PHP framework developers. It’s an extension of the original PSR-1, which basically has 3 main conventions.

  1. Classes are upper Camel case.
  2. Methods are lower Camel case.
  3. Constants are written in caps like VERSION_NUMBER.

PSR-2 expands this by using these standards

  1. Indents are 4 spaces instead of tabs.
  2. Keep line lengths at 80 characters. Soft limit of 120 characters.
  3. Use one blank line after a namespace declaration. Use a blank line after a group of declarations.
  4. Opening braces for classes MUST go on the next line. Closing brace must be on its own line.
  5. Opening braces for methods must go on the next line, and a separate line for the closing brace.
  6. Closures must have a space after the function keyword.
  7. Never use the var keyword, this is used with  JavaScript and would cause some confusion.
  8. Control structures, like IF, WHICH, FOR, and FOREACH must have one space before the condition parenthesis.
  9. The case keywords should be indented from the switch keyword.
  10. Keywords, like true, false, and null show be in lower case.

See examples.

Laravel 5.1 with Forge

Laravel 5.1 was released in June 9, 2015. So, I updated my development environment with version 5.1 and updated it to GitHub. From there, I spun a server via Forge at Digital Ocean. Then, I deployed my development repository via GitHub.

However, I faced two issues:

  1. I was getting an error saying that there is not enough memory. The simple fix is to do a composer update in your dev environment and then upload your changes to GitHub. If you have automatic deploy turned on, then the changes will be updated on the server.
  2. The other error was a missing app key. You can fix this by simply running artisan key:generate on the server. You must have a .env file on the Laravel directory. I believe it’s under /home/forge/default. You can create one or copy the .env.example.

Hopefully, I save you some time with troubleshooting if you run into these issues.

Backup Rotation

After another downtime with my service provider, I wasn’t really happy with my backup strategy. Although, I have a backup plan in place, it’s not to the level of detail that I wanted. Back to the drawing board I went. I realized that what I really needed, was to back up my databases and keep copies of the last few days. Prior to that I was just relying on full file system backup and restore. Thankfully, backing up MySQL databases is not that difficult. The following pieces of code is the backup script I have put together to backup each MySQL database, as well rotate the backups and keep 5 days of copies of each database.

#! /bin/bash
# where sql files are stored
cd /home/user/mysql
# list all databases and assign to variable
# exclude 3 MySQL default databases (information_schema,performance_schema,mysql)
DATABASES=`mysql -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`
# loop through all databases
# remove db that's 5 days old
# rotate dbs. rename 1 day older
# perform backup today
for db in $DATABASES; do
  rm -rf $db-4.sql
  mv $db-3.sql $db-4.sql
  mv $db-2.sql $db-3.sql
  mv $db-1.sql $db-2.sql
  mv $db-0.sql $db-1.sql
  mysqldump $db > "$db-0.sql"

The for loop will execute everything inside it. The backup rotation gives me 5 days of backups of each database. You can schedule the script to run daily with cron. Just a side note. You need to create .my.cnf file in your home directory with your database credentials, so you don’t have to enter your username and password when running it from cron. Also script needs to be executable. A simple chmod +x will do the trick.