Tuesday, 10 December 2013

The posgresql and postgis install on Debian Wheezy

Recently, for working reason. I install the postgresql and postgis on Our test sever. Actually, it contains some troubles, so I record the process for make it easy next time.

1, add apt source file:
Create /etc/apt/sources.list.d/pgdg.list. The distributions are called codename-pgdg. In the example, replace squeeze with the actual distribution you are using:
deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main
(You may determine the codename of your distribution by running lsb_release -c.)
Import the repository key from https://www.postgresql.org/media/keys/ACCC4CF8.asc, update the package lists, and start installing packages:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
 
2,install postgresql
apt-get install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 postgresql-server-dev-9.3
3, Change the default password of postgresql. The default user of postgresql is postgres, 
(1), root@somemachine:~# sudo -u postgres psql  
(2),  postgres=# ALTER USER postgres WITH PASSWORD 'postgres'; 
4, change linux password:
root@somemachine:~#sudo -u postgres passwd

5, change the postgresql configuration files, make it can accessible from remote client:
        root@server2-virtual-machine:~# vi /etc/postgresql/9.1/main/postgresql.conf

         1.Listen to all address access, modify connection permissions
          #listen_addresses = ‘localhost’ change to listen_addresses = ‘*’        
          2.
Enable password authentication
          #password_encryption = on  change to password_encryption = on
          3.the ip limitation of Users can access
           root@server2-virtual-machine:~# vi /etc/postgresql/9.1/main/pg_hba.conf,
 Add fellow consent:
       
# to allow your client visiting postgresql server
          host all all 0.0.0.0 0.0.0.0 md5
 or:
host    all             all             0.0.0.0/0            md5

6, change the encode of postgresql template:

root@somemachine:~# sudo -u postgres psql -U postgres
or
root@somemachine:$ psql -U postgres
psql (9.0.3)
Type "help" for help.
postgres=# update pg_database set datallowconn = TRUE where datname = 'template0';
UPDATE 1
postgres=# \c template0
You are now connected to database "template0".
template0=# update pg_database set datistemplate = FALSE where datname = 'template1';
UPDATE 1
template0=# drop database template1;
DROP DATABASE
template0=# create database template1 with template = template0 encoding = 'UTF8';
CREATE DATABASE
template0=# update pg_database set datistemplate = TRUE where datname = 'template1';
UPDATE 1
template0=# \c template1
You are now connected to database "template1".
template1=# update pg_database set datallowconn = FALSE where datname = 'template0';
UPDATE 1
7, create database:
1.create users
postgres=# create user “zhaofeng” with password ‘123456’ nocreatedb;            2.create database with special user           postgres=# create database “testdb” with owner=”zhaofeng”;             4.shell managment: root@somemachine:~# -u postgres createuser -D -P test1 root@somemachine:~# -u postgres createdb -O test1 db1
8,Install postgis 
apt-get install postgresql-9.3-postgis-2.1
9,enable postgis and postgresql extensions:
psql -d yourdatabase -c "CREATE EXTENSION postgis;"
psql -d yourdatabase -c "CREATE EXTENSION postgis_topology;"
psql -d yourdatabase -c "CREATE EXTENSION fuzzystrmatch;"
psql -d yourdatabase -c "CREATE EXTENSION postgis_tiger_geocoder;"
10 import sql data
postgres@wemet:/$ psql -d kct -f /opt/kct.sql

Monday, 9 September 2013

some scripts about debian(linux) boot-shell and swap

Recently, I manage a tiny vps with  debian, since it needs extension is swap for more programs. I do some research on how to mount a swap and add some customer shell on boot. This article just for record some shell script for next time to use.

1, the website  http://www.debian.org/doc/manuals/debian-reference/ very useful, but sometimes it goes wrong like:
the page:
http://www.debian.org/doc/manuals/debian-reference/ch03.en.html#_an_overview_of_the_boot_strap_process

it write:
The Debian system goes into runlevel N (none) to initialize the system by following the "/etc/inittab" description.
  1.  but actually it is in  /etc/event.d/somefile
2, the swap setting process.
    1, checking the current usage of each partition:

root@wemet:~# df -B M
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/vda 20031M 3323M 15684M 18% /
none 1M 0M 1M 0% /sys/fs/cgroup
udev 242M 1M 242M 1% /dev
tmpfs 50M 1M 49M 1% /run
none 5M 0M 5M 0% /run/lock
none 246M 0M 246M 0% /run/shm
none 100M 0M 100M 0% /run/user

2, build the large file
root@wemet:~# dd if=/dev/zero of=/swapadd bs=1024 count=524288
524288+0 records in
524288+0 records out

3,moving file to /mnt/swap/  that is just meet the mount standard.
4, build virtual memory
root@wemet:~# mkswap /mnt/swap/swapadd
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=a5c8b651-6f64-4414-bb5f-580b742acfce

5, make a shell file which will enable the swap
the bash file is:
#!/bin/sh
swapon /mnt/swap/swapadd
and make it have right and standard place:
root@wemet:~# mv swaponfile /etc/init.d
6, put it to standard multi-user runlevels (2 to 5)
root@wemet:/etc# cd /etc/rc2.d
root@wemet:/etc/rc2.d# ln -s /etc/swaponfile myswap
7, Changing the system swappiness value. The swap file will then only be used when my RAM usage is around 80 or 90 percent. open /etc/sysctl.conf as root, add fellowing:
vm.swappiness = 10 
 or you can also handle on shell script:
root@wemet:/etc# sysctl vm.swappiness=10
8,reboot and the new swap woulb be built.

Friday, 2 August 2013

How to make Android Webview support websocket

Background:

This comes from a chat project designing. Recently, someone (may) invite me to do a chat project, since it should execute on both web and android platform. I decided to use websocket (why applying websocket you can read http://en.wikipedia.org/wiki/WebSocket) to solve this problem, I think it would be make the problem simple as I can write java-script once, while it can execute on both platforms.

But after I read some document, I find it is pity that the android default Web browser do not support the websocket!!! And so the internal Webview is also do not support this feature. (http://caniuse.com/#feat=websockets) I think it is popular for android default webview class do not support many features and that features is just you need or you want to make. This article will show how to add an unsupported feature in default  webview for android app.

Principle:

About Webview class: (this part comes from Android develop reference)

 By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probably fine; the user won't need to interact with the web page beyond reading it, and the web page won't need to interact with the user. If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView.

A WebView has several customization points where you can add your own behavior. These are:
  • Creating and setting a WebChromeClient subclass. This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here (see Debugging Tasks).
  • Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
  • Modifying the WebSettings, such as enabling JavaScript with setJavaScriptEnabled().

Key points:

  • Injecting Java objects into the WebView using the addJavascriptInterface(Object, String) method. This method allows you to inject Java objects into a page's JavaScript context, so that they can be accessed by JavaScript in the page. (WebView can inject javascriptinterface to achieve js to java invocation. hehe, we can use this interface to support our defined javascript features. ;-)
This is also how  PhoneGap execute their many js features. Here I will bring some PhoneGap(https://github.com/anismiles/websocket-android-phonegap) code as example.

Finally, my main execution is:
   wv.addJavascriptInterface(new WebSocketFactory(wv), "WebSocketFactory")
(If your android api above API 17, you should also add  @JavascriptInterface  in every public methods which your js script can invoke, the details should be see in api)
and the js code is:
(function() {
// window object
var global = window;
 
// WebSocket Object. All listener methods are cleaned up!
var WebSocket = global.WebSocket = function(url) {
 // get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java)
 this.socket = WebSocketFactory.getInstance(url);
 // store in registry
 if(this.socket) {
  WebSocket.store[this.socket.getId()] = this;
 } else {
  throw new Error('Websocket instantiation failed! Address might be wrong.');
 }
};
...
} 
in this way, you can simple do:

var websocket = new WebSocket(wsUri);
(the ws server is use the example on: http://www.websocket.org/echo.html)
The final results:

source code:

I put source code  on my new github: https://github.com/thinksource/vw_websocket
the apk: https://github.com/thinksource/vw_websocket/blob/master/bin/vw_websocket.apk
(I wrote for my i9100 android 4.0.3, so only above this version the app can work.)


 

Sunday, 28 July 2013

django's 'post' metod: about csrf protection

The CSRF middleware in django provides easy-to-use protection against Cross Site Request Forgeries. it is also regards as default way of execution

how it works

1, the middle ware 'django.middleware.csrf.CsrfViewMiddleware' to your list of middleware classes.

2, On server side the csrf is csrf_token like:
<form action="." method="post">{% csrf_token %}
You may need manually update the csrf by:
    
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
    
def my_view(request):
    c = {}
    c.update(csrf(request))
    return render_to_response("a_template.html", c)
 
3,if you already added the the csrf middleware and you do not want to apply it, you should added
@csrf_exempt
 
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world') 
it is very import to added @csrf_exempt if you want to use ajax in django framework.

Sunday, 9 June 2013

visual studio 2012 ( vc11) enviornment setting path, lib and include path

Today, I installed the MySQL(mysql-python) for Python. I got lots of troubles on this, since the python invoke the c library and it compiled by VS2008 (vs 9.0), while my laptop's VS is 2012 (vs 11.0).

The two issues for this one:

1, python\Libs\distutils\msvc9compiler.py  the find_vcvarsall(version) function always received version 9 while it should be 11 in my laptop.
2,MySQL Connector C 6.0.2 (the mysql-python invoked c base library) do not provide the include path and lib path. I spent several hours to read and run the python and vcvarsall.bat( the all system variable file)

I find the VC/vcvarsall.bat is the VC variables all generated by this file, it include several vars file for different cpus.
for example, the part of vcvars32 is:

....
@if not "%WindowsSdkDir%" == "" (
    @set "PATH=%WindowsSdkDir%bin\x86;%PATH%"
    @set "INCLUDE=%WindowsSdkDir%include\shared;%WindowsSdkDir%include\um;%WindowsSdkDir%include\winrt;%INCLUDE%"
    @set "LIB=%WindowsSdkDir%lib\win8\um\x86;%LIB%"
    @set "LIBPATH=%WindowsSdkDir%References\CommonConfiguration\Neutral;%ExtensionSDKDir%\Microsoft.VCLibs\11.0\References\CommonConfiguration\neutral;%LIBPATH%"
)
....

you can seen the vc include path you can just set system environment variables INCLUDE and the lib you can just set LIB in your system environment variables LIB. The same as libpath setting.


Friday, 31 May 2013

Fixing 500 OOPS: vsftpd: refusing to run with writable root inside chroot ()

After I upgrade my vsftp to newest version. I find I could not log in my vsftp as follow error:


500 OOPS: vsftpd: refusing to run with writable root inside chroot ()
This is becasue the new added feature of upgrade:
- Add stronger checks for the configuration error of running with a writeable root directory inside a chroot(). This may bite people who carelessly turned on chroot_local_user but such is life.
To fix this problem you should remove write permissions on the users root directory
chmod a-w /www/ftp
or
For the standard vsFTPd build (vsftpd): (still error for upload file)

allow_writeable_chroot=YES

Tuesday, 28 May 2013

[Android]call to OpenGL ES API with no current context (logged once per thread)

Questions:
call to OpenGL ES API with no current context (logged once per thread)
for java you could not call the Opengl on main thread, you should build opengl running in subthread.
You can use  GLSurfaceView to do this by your own thread.

The wrong way:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            break;
        // ...
    }
}

The correct way:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            // GLSurfaceView.queueEvent
            surface.queueEvent(new Runnable() {
                @Override
                public void run() {
                    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                }
            });
            break;
        // ...
    }
}