How to fix conflict of ui-router and karma

Share Button

When you use karma to do unit testing for angular application using ui-router module, you will likely get following error:

The part after “GET” depends on your setup of the application. The reason this happens is that there is a conflict of ui-router module and karma. The quickest fix is to defer  intercept of  urlRouterProvider by adding following to your unit test code:

 

Resolving “cannot connect to database” error of wordpress after upgrading from Ubuntu 12.04 LTE to 14.04 LTE

Share Button

I upgraded my server from Ubuntu 12.04 LTE to 14.04 LTE. But after that, I cannot access my wordpress site any more. This gave a headache for a whole night. After trying tons of methods, finally I figured it out how to resolve it. But still not sure what happened though. So my issue is I keep getting “cannot connect to database” error.

First of all, I tried to check if mysql is installed correctly, and see if I can log in mysql using root. The answer is yes. But strangely I cannot log in using the user name and password wordpress is using. I also checked mysql wp_* tables, and they are all there. So it must be something with user name and password. I basically tried all method on http://www.wpbeginner.com/wp-tutorials/how-to-fix-the-error-establishing-a-database-connection-in-wordpress/. But no matter I access www.ycshao.com or www.ycshao.com/wp-admin or http://www.ycshao.com/wp-admin/maint/repair.php, I always get “cannot connect to database”.

I know it’s something wrong with my wp-config.php, but I couldn’t figure out why. So I tried following solution which worked eventually for me.

  1. I backed up old wp-config.php, and it will let you generate new wp-config.php.
  2. When I accessed www.ycshao.com, the initial setup screen showed up! I tried to setup database name, user name, password, database host and table prefix according to DB_NAME, DB_USER, DB_PASSWORD, DB_HOST and table_prefix variables in wp-config.php. But no luck, I still could not connect to database.
    install-step3
  3. Then I created new mysql user and password
  4. Then I repeated step 1 and 2. Magically it worked! Everything comes back!

It took me 3 hours 22 minutes.

 

Fix “There was a problem with the editor ‘vi’” on Mac

Share Button

I encountered this issue when I run

and vi opens up. If I input some comment, save and quit. I’ll get this error msg:

and commit will fail.

I think this is because some thing wrong with default vi installed on Mac. I verified by running

This means quit vi returns error code 1. I have no idea why this happen. But I tried with Vim and it doesn’t happen. So the solution is really simple, set default edit to something other than this problematic vi.

To find which vim you are using:

Set up psql database in django.

Share Button

I use sqlite as my django database, but there are some limitations in sqlite. So I want to switch to mysql or psql(PostgreSQL). Both of them need a little bit more setup than sqlite, but still quick easy.

  1. Install database client.
  2. Create user.
  3. Create database.
  4. Modify django settings.
Let’s begin with mysql.

Now let’s see how to do it for psql.

Ok now we created user name, password and database, we need to modify settings.py accordingly.

We are all set!

Editing Python code in Vim.

Share Button

Python is relied on indentation, and if you edit python code in Vim, you may found it will mess up your indentation. Here is a simple way to fix it.

Open your file in Vim you will see something like this:

and enter Vim command

to see all white spaces. If you see something like this:

You know you have inconsistent indentation. “^I” means tab, but “print c” statement is indented by space.

How to fix this? Easy.

  1. Edit your .vimrc file to have following:
  2. For the files having indentation issue, run Vim command to fix them:
  3. Now check the file again using “:set list”, and you will see all tabs are gone. They are places by spaces.

Serve django static files for production service and solve missing admin page css problem.

Share Button

It works perfectly on dev version of my django site. But when I deploy it to production (served by apache), I realized that css is missing for admin pages. Then I digged a little deeper to see what I could do to solve this issue. It actually quite simple. There are two files you need to modify.

  1. Modify settings.py.

    here STATIC_ROOT will be the folder where django will copy all admin static files. STATIC_URL will corresponding to the alias name in your apache config file. Don’t worry, keep reading and it will be clear to you.
  2. Go to your django site directory and run

    This will copy all static files including admin css, image, js files into your STATIC_ROOT. Now check your “static” folder, you should see “admin” folder which has “css”, “img” and “js” sub-folders.
  3. Modify corresponding apache config files under /etc/apache2/sites-available/ folder.

    where “/static/” is your STATIC_URL and “/dir/to/your/static/” will be the location of your “static” folder which is your STATIC_ROOT.
  4. You are all set! Restart your apache and checkout your admin page.
References:

How to setup wsgi and Django on Ubuntu server.

Share Button

It actually very easy to setup wsgi and Django.

And wsgi.py will look like this:

 

How to setup name-based virtual host on Ubuntu server.

Share Button

I’m moving my site from fatcow.com to linode.com because I setup VPS on linode so I’ll have more control of it. The first step is to setup name-based virtual host so that I can link my sub-domains to my “www” sub-directories.

  1. Create DNS records for domains and sub-domains. The IP address of sub-domains can be the same as your main domain.
  2. Now the world know how to resolve your domain name to an IP address, next step is to create name-based virtual host on Ubuntu server so that we can host different sites on different sub-domains. I want to create 2 sites. One is my main domain which host my blog www.ycshao.com, another one is for testing purpose image.ycshao.com.
  3. Enable name-based virtual host on Ubuntu:

    and add following line to the end if it’s not there
  4. There are two folders in /etc/apache2, which are sites-available and sites-enabled. Usually sites-enabled folder will have soft link to files in sites-available folder. The folder names are pretty obvious, “available” means available sites and “enabled” means  sites which can be accessed. Let’s start from scratch.

    and modify first few lines to

    The most important things are your “ServerName” corresponding to url you want to set and “DocumentRoot” corresponding to which directory this url points to. Then make a soft link to enable this site:
  5. Now you should be able to access www.ycshao.com and it should go to my wordpress blog sitting in /var/www/wordpress.
  6. Now repeat step 4 for image.ycshao.com and other sites you want to configure.