Practical Web Programming

Saturday, July 26, 2014

How Make Symbolic Link in Linux/Unix

Every now and then I find myself creating more symbolic link especially when deploying application to production, and I always forget (more like in doubt) the order of parameter when issuing a ln command. I always search for it in google and have to visit more than a couple site to find a example. So, to save time, here is it.

To create a soft link (most common form), use this command:

$ ln -s source destination

Where:

- source = the file or directory you want to link to
- destination = the name of the link that points to the source

Ex:

$ ln -s /home/kabalweg/my_app /var/www/my_app

Wednesday, June 18, 2014

Better Way to Generate Rails Application

Generate project with RMV.

$ mkdir myapp                                                           # create the project folder
$ cd myapp
$ rvm use ruby-2.1.1@myapp --ruby-version --create     # create a new project-specific gemset
                                                                                 # option “—ruby-version” creates .ruby-version and .ruby-gemset files in the root directory
$ gem install rails --pre                                                # installs the most recent release of Rails 
$ rails new .                                                               # generate application in the current directory.


If you have no RVM in your system, follow command below.  

$ \curl -L https://get.rvm.io | bash -s stable --ruby        # install rvm and ruby

$ rvm get stable --autolibs=enable                              # if you have rvm already, update it
$ rvm install ruby                                                      # install Ruby

$ rvm --default use ruby-2.1.1                                    # use and set it as default

$ gem update --system                                             # update gem


Backup/Restore MySQL Database

Backup mysql database to a sql file.

$ mysqldump -u root -p[root_password] [database_name] > filename.sql

Restore from sql file to database.

$ mysql -u root -p[root_password] [database_name] < filename.sql

Tuesday, April 15, 2014

Using scp (Secure Copy) to Copy Files from Remote Server

Copy a file from a remote host to a local machine

$ scp username@example.com:/home/kabalweg/my_archive.ta.gz /home/kabalweg/local

Copy a file from a local machine to a remote host

$ scp /home/kabalweg/local/my_archive.tar.gz username@example.com:/home/kabalweg
 

Compress/Uncompress in Linux/Unix

Compress the folder named sites inside the /home/kabalweg directory:

$ tar -zcvf my_archive.tar.gz /home/kabalweg/sites

Uncompress file into the current directory:

$ tar -zxvf my_archive.tar.gz

Where: 
z = compress archive using gzip program
c = create archive 
v = verbose i.e display progress while creating archive
f = archive File name 
x = extract files

Watching Log Files Using tail and less

Watch the error log and filter the output to the screen (using grep).

tail -f /var/log/apache/error.log | grep -v Exception | grep -v 'PHP Notice'

Output the error log to the screen, filter it before doing so (using grep).

less +F /var/log/apache/error.log | grep -v Exception | grep -v 'PHP Notice'

Note:

-v parameter in grep is reverse look up. It mean, all text except 'Exception' and 'PHP Notice'


Forward and Backward Search in less

/ – search for a pattern which will take you to the next occurrence. 
? – search for a pattern which will take you to the previous occurrence. 
n – for next match in backward direction 
N – for previous match in forward direction
Shift F - similar to tail -f

How to Create User in Mysql

Create the user that can access mysql via localhost:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';


If you want the user to access the database from anywhere, then use this:


CREATE USER 'username'@'%' IDENTIFIED BY 'password';


Give the new user permission to access all databases and all its tables:

GRANT ALL PRIVILEGES ON * . * TO 'username'@'localhost';


Reload the privileges:

FLUSH PRIVILEGES;

Tuesday, March 25, 2014

Git: Merge Develop Branch to Master

Normally when developing, you would make your changes in develop branch and when releasing, merge develop to master branch. Here's the best way to do it:

$ git checkout master                 # make sure you are in master branch
$ git fetch                                  # download commit from remote branch
$ git reset --hard origin/master     # removes changes/commits in local master branch (if any)
$ git merge origin/develop           # merge remote develop to master (the current branch)
$ git push origin master              # push the changes to remote repo

Git: Delete All Local Branches That Were Already Merged to Master

In Git, to clean up your local repository of already merged branches to master (aka pruning), run this command:

$  git checkout master
$  git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d

This is three commands in one, made possible via pipes ("|").

1) git branch --merged master     # list all the branches that have been merged to master
2) grep -v "\* master"                 # select all the branches that does not match "* master"
3) xargs -n 1 git branch -d          # execute git branch -d with 1 parameter - the branches that was returned by #2

Wednesday, March 05, 2014

Fix Repeating Content in Email using Handlebars Template

Recently, I reformat our email template and ran into an issue wherein the middle part of the email body is re-populating at the end of the body resulting in duplicate content. The email template was created using Handlebars. My suspect is the character encoding of the production mysql database. But what ever I do (copy the prod DB locally with same encoding, etc), I could not replicate it in my local machine.

Removing the non-printable characters before rendering the content with template fixed it. Here's how I did it.

$cleaned_text = preg_replace('/[\x00-\x1F\x80-\xFF]/', ' ', $text_with_non_printable_chars);

/[\x00-\x1F\x80-\xFF]/ are characters from 0-31, 128-255 in ASCII character code.

Tuesday, February 18, 2014

How to enable remote access in a MySQL server

Let say you are developing locally and wanted to connect to a remote mysql server for testing, if that remote server is not configured to allow access from a different machine, you won't be able to connect to it. Assuming you want to allow 'the_user' user to connect remotely, here's to do it. 

Edit your my.conf file (usually located at /etc/mysql/ folder), and change the bind-address value 

from: bind-address = 127.0.0.1
to:     bind-address = 0.0.0.0

Login as a root user to MySQL. If you don't have the password run this command:

sudo mysql --defaults-extra-file=/etc/mysql/debian.cnf

Once you are logged in, run these commands

USE mysql;
UPDATE user SET host='%' WHERE User='the_user';
UPDATE db SET Host='%' WHERE User='the_user';
FLUSH PRIVILEGES;

Then, restart mysql

sudo service mysql restart

Recent Post