Posts Tagged apache2

Let Python work with Apache2

Python is very good language with efficient execution and clean code. It will be nice to write server side scripts in Python. A simply search of the internet, ended the following use information.

The original information was from the following page: http://askubuntu.com/questions/29152/how-do-i-use-python-with-apache2

Install the packagelibapache2-mod-wsgi, then issue sudo a2enmod wsgi to enable it.

Just as a quick example to get it running, stuff something like this in your /etc/apache2/sites-enabled/default:

WSGIScriptAlias /test /path/to/python/file.py 

And in the file /path/to/python/file.py:

def application(environ, start_response):     start_response('200 OK', [('Content-Type', 'text/plain')])     return "Hello World" 

After re-starting Apache2, any request to /test will turn into a call of application() in your python file.

For further reading, look into WSGI (WebServer Gateway Interface), the way Python integrates with web-servers.

Bonus / Update:

Python (unsurprisingly) has a small WSGI server in the standard library for testing. If you add this in the bottom of your file, you can run it as any old executable for testing purposes, and then let Apache take over for production:

if __name__ == '__main__':     from wsgiref.simple_server import make_server     httpd = make_server('', 8080, application)     print "Serving on http://localhost:8080"     httpd.serve_forever()
Share

Tags: , , ,

Use mod_proxy to forward web traffic to other machines

Problem

The situation is there are multiple web servers behind a router. In the router, port forwarding can point to the normal http port (80) to one server. For other server, you have to open different http ports for them, such as 8080. When you use these server with non-standard port, you have to use like http://point.to.server.domain:8080/ to access it. This is not nice enough for people. All servers are running on Ubuntu servers. How to solve the problem.

install and enable mod_proxy

In the Ubuntu server with the web server running and the port forwarding pointed to, you need enable the mod_proxy module to make this work.

a2enmod proxy

The mod_proxy is located in /etc/apache2/mods-available. If somehow it is not there, you have to install it by yourself. Use the following command to do it.

apt-get install libapache2-mod-proxy-html

You also need to change the proxy settings in a configuration file in /etc/apache2/mods-enabled called proxy.conf that comes with a security rule of: “Deny All”. You will have to edit this (I changed it to Allow All since I wanted all traffic available) according to your specific situation.

modify the virtual host file

You can use mod_proxy along with virtualhosts to forward traffic to different servers based on the domain name. A particular virtual host file is the following.

ServerName SUB.DOMAIN.COM
ProxyRequests Off

Order deny,allow
Allow from all

ProxyPass / http://192.168.1.11/
ProxyPassReverse / http://192.168.1.11/

ErrorDocument 503 "The guest server is currently offline."

The virtualhost entry takes any requests for sub.domain.com, and forwards them to http://192.168.1.11 the internal IP for the guest OS. The response is sent back through the host (hence using mod_proxy, it’s acting as proxy here ) and displayed to the user.

ErrorDocument 503 is used when the guest doesn’t respond.

Working happily

After completed all these steps, you cannot wait to restart the server and test it. First restart the server.


/etc/init.d/apache2 stop
/etc/init.d/apache2 start

However, you may feel disappointed because all you get is Internal Server Error message. What should I do with it. Now follow the instruction to check the server error log, which is located /var/log/apache2/error.log.


tail /var/log/apache2/error.log

There is warning message in the log as the following.


[warn] proxy: No protocol handler was valid for the URL /. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

What I should deal with this warning. What I did is the following:


a2enmod proxy_http

You will get warnings and some messages. Ignore that and restart your apache2 server.

Try again, hooray, it works now.

Share

Tags: , , , , ,

Use GD library in php5 in Ubuntu server v10.04

After the new Ubuntu server 10.04 were installed, I tested my web applications. One of the web applications use Securimage from http://www.phpcaptcha.org/. However, no image was shown up. I followed the suggestion from Securimage and put the following command in securimage_show.php:

ini_set('display_errors', 'on'); error_reporting(E_ALL);

After browse the following page: http://www.usbargains.net/securimage/securimage_show.php. A fatal error occurs. It says that imagecreatetruecolor() could be found. I searched the PHP.net and find this function is part of GD library. This means that something related to GD library was wrong.

I created a simple php file call phptest.php, which includes the following statement.

<?php phpinfo() ?>

Browse this page in a browser, it uncovered that I do not have GD library support in PHP5 installation. The GD Graphics Library for dynamically manipulating images. We usually have to compile PHP with the GD library of image functions for this to work.

However, Ubuntu (and Debian) comes with package called php5-gd. Just type following commands to install this module and restart Apache2.

# apt-get install php5-gd
# /etc/init.d/apache2 restart

I found this trick at http://www.cyberciti.biz/faq/ubuntu-linux-install-or-add-php-gd-support-to-apache/. Thanks for sharing this.

Hooray, it works now. That took me a couple of hours to find the solution. It is tough to configure a new server.

Share

Tags: , , , , , , ,

Setup Apache2 server on an Ubuntu server

I encountered a series problems when I migrated my web server from one machine to another. The new machine has Ubuntu server v10.04 installed. There are a list of updated software installed. It make the migration hard. To get apache setting for the new machine exactly same as the old one , I spent fairly amount of time to fix warnings and errors. Here are some of them I encountered with solution. I’d like to write this down for myself and sharing with you.

Get Apache2 server version

Use the following command to get the version information of the installed apache2 server.

/etc/apache2# apache2 -v

Server version: Apache/2.2.14 (Ubuntu)
Server built: Apr 13 2010 19:28:27

Set ServerName

After apache2 server is properly installed. I found the warning information in my first reload.

apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

To solve it, add a statement in the httpd.conf like the following. You should use your preferred name to replace “aquamarine”.

ServerName aquamarine

Setup virtual hosting

After I copied all my site setting from another machine to this machine and put them in the appropriate directories. I tried to reload Apache2 server. I got he following error message.

[error] VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results

To solve the problem, I found that one of the site file in sites-available directory includes a statement at the front like

<VirtualHost *:80>

It is this statement causing the problem. I removed “:80” from the above statement. Reload the apache2. The error message disappeared.

Get rid of a tough warning

When I reload the server, I always find the following warning message. I googled the warning in hope to find a solution. Unfortunately I have not found a solution working for me. I will continue searching and seeking solutions.

[warn] NameVirtualHost *:80 has no VirtualHosts

Share

Tags: , , , ,

Install mod_geoip for Apache2 webserver

This guide explains how to set up mod_geoip for Apache2 on a Ubuntu server. mod_geoip looks up the IP address of the end client. This allows you discover who and from where the people are visiting your website. In addition, you can use the information to redirect or block users.

Use the following command to install mod_geoip:

apt-get install libapache2-mod-geoip

After the installation is complete, you can find the GeoIP databases (GeoIP.dat, GeoIPv6.dat) in the /usr/share/GeoIP directory. As the geographic allocation of IP addresses can change over time, it’s a good idea to download the newest GeoIP.dat now:

cd /usr/share/GeoIP/
mv GeoIP.dat GeoIP.dat_orig
mv GeoIPv6.dat GeoIPv6.dat_orig
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
gunzip GeoIP.dat.gz
gunzip GeoIPv6.dat.gz

Next we restart Apache:

/etc/init.d/apache2 restart

Hooray! Done. You have the lastest geoip database!

Share

Tags: , , , , ,