2018年4月29日 星期日

How to set up cgi module for apache2

At this moment, i am studying CGI(common gateway interface) script with python. Need to setup a temporary local website for test. Some notes here.

Environment
1. ubuntu 14 server
2. apache2 install

I am not sure any python copy should be installed or not necessary.

First need to edit /etc/apache2/conf-enabled/serve-cgi-bin.conf

<IfModule mod_alias.c>
 <IfModule mod_cgi.c>
  Define ENABLE_USR_LIB_CGI_BIN
 </IfModule>
 
 <IfModule mod_cgid.c>
  Define ENABLE_USR_LIB_CGI_BIN
 </IfModule>
 
 <IfDefine ENABLE_USR_LIB_CGI_BIN>
  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  <Directory "/usr/lib/cgi-bin">
   AllowOverride None
   Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
   Require all granted
  </Directory>
 </IfDefine>
</IfModule>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
substitute the directory after "ScriptAlias" with the directory you want to put your cgi 
script files. 
Note. this directory must fall in access range of apache2. You can edit apache2.conf under 
/etc/apache2/ directory to add your directory 
<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all
</Directory>

<Directory "/var/www/cgi-bin">
   Options All
</Directory>
cgi module is not enabled by default while apache2 installed. You have to manually add it by make a link to /etc/apache2/mods-enabled directory with below command

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/cgi.load
then reload apache2

$ sudo service apache2 reload
It's all set. You can now put your first cgi script to your cgi-bin directory and test it with your favorite browser under http://localhost/cgi-bin/xxx.py
oh! one more thing! Remember to change access mode to your script file.
sudo chmod +x /usr/lib/cgi-bin/xxx.py

2018年4月3日 星期二

Python install Modules

Besides standard Library, there are lots of modules developed by independent contributors and academy or science group. You can use commands to download and install these modules with built in pip install program.

python -m pip install --user modulename 

Here list some modules of categories

1. web framework

  • Django
  • Pyramid
  • Web2py
  • flask
2. Graphic processing
  • PIL
  • Pillow
3. Science and Math
  • numpy
  • Matplotlib
  • pandas
  • scikit-learn
4. command line operation
  • fabric
  • paramiko
5. Nature language processing
  • nltk
  • textblob
  • jieba
6. network client
  • requests
  • pycurl
7. database protocol
  • mysql-python
  • pymongo
  • psycopg2

Python Study Note (1)

Besides ordinary Python Manual reading, i would like to put some notes here to refresh my bad memory. There are many key words that i need to keep in mind when learning new computer language especially this language being a huge different compared to what i used for living(c, assembly).

Well skip the interpreter part which i see tiny chance of using it.

Typical python module definition file which have file extension of *.py have below format.

if you have multiple version of python install in your system, you can specify which version you want your program to run with.
#below  check your py.ini for defaults of "python" version

#! python  

#below specify the encoding of this python source file. utf-8 is default.

#-*-coding: utf-8-*-  


#then import some module you reference

import numpy

import sys,os


def fun1(a):

    pass


def main():

    pass


#below will use module file as script to run main, mainly for test purpose 

if __name__ == '__main__':

    main()  
There are some coding style that it would be nice to follow.
  • Use 4-space indentation, and no tabs.
    4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.
  • Wrap lines so that they don’t exceed 79 characters.
    This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.
  • Use blank lines to separate functions and classes, and larger blocks of code inside functions.
  • When possible, put comments on a line of their own.
  • Use docstrings.
  • Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).
  • Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument.
  • Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.
  • Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.