Comments

Tags Cloud

There are no tagged articles!

+ All tags

somertechs.com

A Web Server Software Written In Python TwistedWeb

Print PDF

Here i come with another goodie, in fact it is not new but too bad i recently realized that this server software exists somewhere.

 

Actually i have been using server softwares which have been written in C language but this is the first time that i`m using one with the python language.

 

So whats the TwistedWeb ?

 

http://twistedmatrix.com/trac/wiki/TwistedWeb#TwistedWeb

 

Too bad i missed this one for a long time...

 

Taken from the official site "Twisted Web includes an HTTP server and an HTTP client and additional libraries often useful in conjunction with these."

 

So i`ll be using only the HTTP server section and will be running php with it (Yeah, its actually pretty fast and easy to configure) , you can also develop your applications with their api and use the built-in HTTP server software written in python.

 

Ok, starting my little tutorial regarding running it on centos with php support.

 

First of all go get the twistedweb package from http://tmrc.mit.edu/mirror/twisted/Twisted/9.0/Twisted-9.0.0.tar.bz2

 wget http://tmrc.mit.edu/mirror/twisted/Twisted/9.0/Twisted-9.0.0.tar.bz2

Now you got it and extracted the tarball with the following

 

tar xjvf  Twisted-9.0.0.tar.bz2

 

You`ll need to configure and compile it now but before doing it, you need some software to be installed otherwise it wont compile.

To install some of the required software we need rpmforge repository

rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm 

 

yum install python-devel zope python-zope-interface

python setup.py build

python setup.py install

Now its installed we need to run the webserver the syntax follows like this

twistd web --port 8080 --path /home/user/public_html/

of course you can change the path and port to whatever you want.

when you want to shut the server down then you execute the following

kill `cat twistd.pid`

The twistd.pid is at the directory where you execute the startup command for example if you execute at /home then the pid will be there.

Lets add php support

Be advised twistedweb only works with standalone php so that means you cannot use a php that is compiled for a specific server software (apache)...

I used my php-fpm supported php-cgi binary for this task and it was located at /opt/php/bin/php-cgi

You need to edit one file called twcgi.py

since i was using python 2.4 it was at /usr/lib/python2.4/site-packages/twisted/web/twcgi.py

class PHP3Script(FilteredScript):

"""I am a FilteredScript that uses the default PHP3 command on most systems.

"""


filter = '/opt/php/bin/php-cgi'

resource.indexNames = ['index.php']


class PHPScript(FilteredScript):

"""I am a FilteredScript that uses the PHP command on most systems.

Sometimes, php wants the path to itself as argv[0]. This is that time.

"""


filter = '/opt/php/bin/php-cgi'

resource.indexNames = ['index.php']

So this is it, we are done, after editing and modifying values like above, you dont need to restart the twistedweb server, changes are instant.

 

I hope this helps you. Thanks. 

 

Hello,

This time i`m going to tell a very important tip about haproxy and nginx.

When you run a LB (Load Balancer) in front of any web server software (nginx, apache, etc...) the LB will send its ip address to the backend server so the server software (in my example thats nginx) will log the LB`s ip in the logs and within any application that does logging.

Whats the problem with that ?

Its very simple because we need to log the client`s ip address at the logs when they visit our site. In my case the geoip module was not functioning after i had installed the LB in front of two backend servers. (One server was having both LB and backend server and the other one was the second backend server, i used iptables port forwarding to have LB at the same ip on the first server).

So lets correct this problem

First things first

Correct the haproxy to send the x-forwarded-for header to the backend servers and close keep-alive.

Open haproxy.cfg and add the following at the global and listen section

option httpclose

option forwardfor

Now haproxy will send the x-forwarded-for header to the backend.

So haproxy is doing its job but how are we going to get the x-forwarded-for header information read by our backend server software, in my case thats nginx so i`ll talk about that here.  

You need a special module for nginx and compile nginx again to have it added.

Thats nginx http real ip module

http://wiki.nginx.org/NginxHttpRealIpModule

Use --with-http_realip_module option while you configure the nginx.  

When you finish installing the module then go to the corresponding vhost configuration file (example domain.conf) where you need to log the real client ip addresses from The LB.  

set_real_ip_from load balancer ip address;

real_ip_header X-Forwarded-For;

The above will tell nginx to get x-forwarded-for header from haproxy on the specific ip so it`ll log and read the correct client ip addresses on the vhost.  

Dont forget to restart nginx and haproxy after you make the changes.

For apache look for mod_extract or mod_rpaf.

Hope it helps you.

Thanks

Server management - Vps and dedicated

Many people wants to use ffmpeg installation on their servers but they dont know how to do this so the programmer at sherin.in created a script which automates the install of ffmpeg with other componenets such as mplayer, mencoder, flvtool ...
Complete list of the tools are
*MPlayer
*Codecs
*Flvtool
*Lame
*Libogg
*Libvorbis
*Vorbis-tools
*Libtheora
*FFMPEG
*AMR 3gp Encoder and decoder (amrnb,amrwb-)
*ffmpeg-php
*Ruby
*wmf Lib
*faad2
*facc
*a52dec-0.7.4
* xvid/div decoder
*x264  
You can get the script from
http://www.sherin.in/ffmpeg/index.php?option=com_content&task=view&id=25&Itemid=39
There are two versions of the script, one for the shared hosts and other one for vps/dedicated hosts (root account is not required).
Instructions
http://www.sherin.in/ffmpeg/index.php?option=com_content&task=view&id=14&Itemid=1
Ffmpeginstall is the FOSS India 2008 Award Winner Project

Vps and dedicated

Its very hard to transfer files between two servers when you have a slow connection speed so there are some scripts that can automate the transfers between servers without downloading to your pc first.
Here is the first one (A simple php script)
http://somertechs.com/ftptoftp.zip
Another one is startftp (More detailed php script)
http://www.startftp.com/startftp_personal_edition.zip

Server management - Vps and dedicated

If you have changed the shell default Port 22 on a cPanel powered server , restarting sshd from the WHM will fail.
You have to ssh to the server and issue the following command to restart sshd:
/sbin/service sshd restart
To, temporarily, reset your shell port back to 22, run the following command from the Address field in browser:
SERVER_MAIN_IP:2087/scripts2/doautofixer?autofix=safesshrestart
Now, you should be able to access shell, and you need to restart sshd at the prompt using the command mentioned above.