Follow up on three of my previous posts:
- How to install and configure mod_wsgi on FreeBSD 8.2
- How to install and configure Django with mod_wsgi on FreeBSD 8.2
- Install PostgreSQL on FreeBSD 8.2 and make it work with Django
I have been following the official Django tutorial and creating the polls app. I have decided to test my app in a FreeBSD virtual machine with properly configured Apache instead of the development server though.
I got to the part where I am supposed to log in into the automatically generated admin area. The http://IP_OF_MY_TEST_VM/admin/ works and I get the admin sign in page but the links to stylesheets are not working. It’s simple to fix that.
My app is located in /usr/local/www/myapp folder and it’s document root is set via httpd.conf to /usr/local/www/myapp/public directory. So:
#cd /usr/local/www/myapp/public #mkdir static #cd static #mkdir admin #cp -r /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/* /usr/local/www/myapp/public/static/admin
That should copy free directories (css, img, js) into your document root. One more thing, edit your virtual host configuration to tell mod_wsgi to match /static/ to /usr/local/www/myapp/public/static/:
WSGIPythonPath /usr/local/www <VirtualHost *:80> ServerName localhost.home ServerAlias localhost.home DocumentRoot /usr/local/www/myapp/public Alias /static/ /usr/local/www/myapp/public/static/ <Directory /usr/local/www/myapp/public> Order allow,deny Allow from all </Directory> WSGIScriptAlias / /usr/local/www/myapp/wsgi.py <Directory /usr/local/www> Order allow,deny Allow from all </Directory> </VirtualHost>
Restart Apache:
#/usr/local/sbin/apachectl restart
And go to http://IP_OF_MY_TEST_VM/admin/. CSS stylesheets should now get loaded properly
Since I have been using MySQL for a long time, transition to PostgreSQL is a little bit difficult. I will be posting some useful commands here for my own reference. This post will be getting updated a lot.
I have explained how to install PostgreSQL on FreeBSD 8.2 in my previous post.
To create a database:
#su pgsql $ createdb db_name $ exit
To drop a database:
#su pgsql $ dropdb db_name $ exit
To list all databases:
#psql -l
To open psql prompt for a database and a user:
#psql db_name -U user
After opening psql prompt with psql db_name -U user
To list all tables in a database:
\d
To describe a table structure:
\d table_name
Psql help:
\?
To quit psql prompt:
\q
Follow up on my previous two blog posts:
- How to install and configure mod_wsgi on FreeBSD 8.2
- How to install and configure Django with mod_wsgi on FreeBSD 8.2
I am going to write down installation steps for PostgreSQL on FreeBSD 8.2 so it can be used with django.db.backends.postgresql_psycopg2 adapter.
#cd /usr/ports/databases/postgresql91-server #make #make install
Add this line to /etc/rc.conf:
postgresql_enable="YES"
Next, initialize a PostgreSQL database cluster:
#/usr/local/etc/rc.d/postgresql initdb
Now add this line to /usr/local/pgsql/data/postgresql.conf:
listen_addresses = '*'
Thirdly, add this line to /usr/local/pgsql/data/pg_hba.conf:
host all all 127.0.01/32 md5
Reboot. Now let’s create a new user for our PostgreSQL database:
#su pgsql $ createuser -sdrP username Enter password for new role: ****** Enter it again: ****** $ exit
Finally, we need to install psycopg2 client in order to be able to connect to the PostgreSQL server from a Django web application.
#pip install psycopg2 #/usr/local/sbin/apachectl restart
Cool. Now you should be able to connect to the PostgreSQL server from your Django app. Make sure to create a database first:
#su pgsql $ createdb myapp $ exit
Now go to your Django application folder and edit settings.py. Use django.db.backends.postgresql_psycopg2 as ENGINE, also set NAME, USER and PASSWORD fields. Reload the Django app in the browser and it should work fine.
This is a follow up on my previous post: How to install and configure mod_wsgi on FreeBSD 8.2
I will assume you have followed all steps in the previous blog post and have FreeBSD 8.2 with Apache, Python and mod_wsgi installed and configured properly. We will use the same directory as we used for the hello world python WSGI web app in the previous article. However, we will re-create it with Django so let’s delete if first:
#rm -rf /user/local/www/myapp
Let’s install Django:
#cd /usr/ports/devel/py-pip #make #make install
Either refresh your shell path or reboot your virtual machine and install Django via pip:
#pip install django
Test Django installation:
#python >>>import django
Next, I used UNIX find comman to find django-admin.py file:
#find / -name django-admin.py /usr/local/bin/django-admin.py /usr/local/lib/python2.7/site-packages/django/bin/django-admin.py
Let’s go to the /usr/local/www directory:
#cd /usr/local/www
And type:
#/usr/local/bin/django-admin.py startproject myapp
This will create a myapp directory with couple of files inside it, basically a standard Django project.
The only thing remaning is to configure mod_wsgi. First, edit the Apache configuration file:
#ee /usr/local/etc/apache22/httpd.conf
It should look exactly like the one in my previous blog post, the only thing changed is that I have moved the virtual host into a separate file. So delete the virtual host configuration and add an Include directive:
Include /usr/local/www/myapp/httpd.conf
I am basically telling the Apache to include contents of another httpd.conf file which will be inside the /usr/local/www/myapp folder. This way the virtual host configuration for our app will be inside the app home folder which makes way more sense. So create a new httpd.conf file:
#cd /usr/local/www/myapp #ee httpd.conf
Configure an Apache virtual host for our Django app:
WSGIPythonPath /usr/local/www <VirtualHost *:80> ServerName localhost.home ServerAlias localhost.home DocumentRoot /usr/local/www/myapp/public <Directory /usr/local/www/myapp/public> Order allow,deny Allow from all </Directory> WSGIScriptAlias / /usr/local/www/myapp/wsgi.py <Directory /usr/local/www> Order allow,deny Allow from all </Directory> </VirtualHost>
Finally, you might have noticed that our Django app is missing the wsgi.py file mentioned in the httpd.conf. Let’s create it:
#ee wsgi.py
The simplest Django app will look like this:
import os os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
That’s it. Open the app in your browser and you should see a page like this:
I will cover installation and configuration process of mod_wsgi on a fresh installation of FreeBSD 8.2. After that I will show you how to create a Hello World Python web application using the just installed and configured mod_wsgi.
There are multiple ways to use Python for web development (for example, mod_python) but mod_wsgi is recommended as the best approach in the Python documentation.
I have set up a fresh installation of FreeBSD 8.2 for i386 platform in VirtualBox on my MacBook. You can get FreeBSD here.
Enough talking, let’s start. First, let’s update the port collection. Run these commands:
#portsnap fetch #portsnap extract #portsnap update
Ok. Let’s install Apache:
#cd /usr/ports/www/apache22 #make config
Make sure THREADS option is selected.
#make #make install
Make sure Apache is started with every reboot:
#ee /etc/rc.conf apache22_enable="YES"
You will also edit one more file:
#ee /boot/loader.conf accf_http_load="YES"
Now the important part. Let’s install mod_wsgi:
#cd /usr/ports/www/mod_wsgi3 make make install
Cool. Let’s create a directory for our hello world application (and a public directory inside it – that will be internet accessible directory where you can put your stylesheets or javascript files):
#mkdir /usr/local/www/myapp #mkdir /usr/local/www/myapp/public #cd /usr/local/www/myapp
Let’s create a virtual host in the directory. Before editing httpd.conf file, make sure to make a backup in case something goes wrong:
#cp /usr/local/etc/apache22/httpd.conf /usr/local/etc/apache22/httpd.conf.backup
After that, remove or comment out ServerRoot, DocumentRoot and their corresponding Directory Apache directives and add this line on the bottom of the the /usr/local/etc/apache22/httpd.conf file:
Include /usr/local/www/myapp/httpd.conf
Create the file httpd.conf inside the /usr/local/www/myapp folder. It will contain virtual host configuration for our application:
#ee /usr/local/www/myapp/httpd.conf
Add this inside the just created httpd.conf:
WSGIPythonPath /usr/local/www/myapp <VirtualHost *:80> ServerName localhost ServerAlias localhost DocumentRoot /usr/local/www/myapp/public <Directory /usr/local/www/myapp/public> Order allow,deny Allow from all </Directory> WSGIScriptAlias / /usr/local/www/myapp/myapp.wsgi <Directory /usr/local/www/myapp> Order allow,deny Allow from all </Directory> </VirtualHost>
There are few important lines:
- WSGIPythonPath will be added to sys.path. Make sure to set this variable in httpd.conf so you can load modules and packages in Python.
- WSGIScriptAlias is an URLI alias that will tell Apache to call the /usr/local/www/myapp/myapp.wsgi. I have put there just a forward slash so http://localhost/ will be the URI which will set off the myapp.wsgi script. If I were to put /myapp there, http://localhost/myapp would be the URI which would set off the wsgi script.
You might be wondering what should the /usr/local/www/myapp/myapp.wsgi script look like. Here is a simplest example:
import wsgiref
def application(environ, start_response):
response_status = '200 OK'
response_body = 'Hello!'
response_headers = []
content_type = ('Content-type', 'text-plain')
content_length = ('Content-Length', str(len(response_body)))
response_headers.append(content_type)
response_headers.append(content_length)
start_response(response_status, response_headers)
return [response_body]
Now restart Apache:
#/etc/local/sbin/apachectl restart
Go to http://localhost/ and enjoy your first working Python WSGI app
Of course, substitute localhost for your IP address if you are not working on your local development machine. If you are using VirtualBox, find out the VirtualBox instance IP address with ifconfig and use that.
Just add this line to eclipse.ini:
-Dfile.encoding=UTF-8
PHP is usually used together with Apache web server but sometimes, mainly during development, it is easier to work on a Windows PC with IIS 7. Fortunately, PHP and IIS 7 are friends and it’s easy to set up a Zend Framework project under IIS 7 webs server. I have decided to merge together few of my older posts which dealt with smaller parts of the whole process. This post should cover installation of IIS 7, PHP, configuration of PHP in IIS manager and finally setting up a Zend Framework project. Let’s begin.
First, you need to install PHP. Download it from here and install it with default settings. On Windows 7 default installation path should be:
C:\Program Files (x86)\PHP\v5.3
Second, install IIS 7. That’s too easy so I won’t give you step by step instructions.
Third, we need to set handler mappings for PHP in IIS manager. Click on Start, type IIS and open Internet Information Services (IIS) Manager:
In IIS Manager click on Handler Mappings:
Click on Add Module Mapping. In Request Path field type “*.php”, choose “FastCgiModule” as Module and type path to your php-cgi.exe file into Executable (Optional) field, in my case “C:\Program Files (x86)\PHP\v5.3\php-cgi.exe”. Type “PHP_via_FastCGI” into Name field. Click on OK. The handler mapping should look like this:
Next, we need to tell IIS manager to treat index.php files as default opening points for PHP websites. Open command line and execute this command:
%windir%\system32\inetsrv\appcmd.exe set config ^ -section:system.webServer/defaultDocument /+"files.[value='index.php']" ^ /commit:apphost
You should have PHP installed and configured correctly in IIS 7 now. As a checkpoint, create a new website with different port than the default one (e.g. 81), add index.php file with some simple echo statement to its root directory and open it in a web browser.
In the case of the image above, the URI of that website would be http://localhost:82/. Open it in your web browser. If everything works fine, we can move on.
Create your usual directory structure for Zend Framework in the root of your newly created website. The only thing left is rewriting URIs. Since IIS 7 does not support .htaccess files we are all used to from Apache environments, you will need to install the URL Rewrite module for IIS.
You can use my Zend blank project to create a quick basic structure for Zend Framework project with already configured index.php and bootstrap files. Just download the zipped file and unzip its contents inside the root directory of your newly created website. In case of this tutorial that would be C:\inetpub\test_website. The only thing you need to edit is the include path in index.php, so it will point to your Zend Framework library folder.
Create a file named web.config in the public directory of your Zend Framework project and put this XML code there:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<configuration>
-
<system.webServer>
-
<rewrite>
-
<rules>
-
<rule name="Imported Rule 1" stopProcessing="true">
-
<match url="^.*$" />
-
<conditions logicalGrouping="MatchAny">
-
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="false" />
-
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="false" />
-
</conditions>
-
<action type="None" />
-
</rule>
-
<rule name="Imported Rule 2" stopProcessing="true">
-
<match url="^.*$" />
-
<action type="Rewrite" url="index.php" />
-
</rule>
-
</rules>
-
</rewrite>
-
</system.webServer>
-
</configuration>
Open your website in the browser and it it should work correctly. In case you used my Zend blank project you should see a page like this:
Final tip. You might have noticed that IIS 7 by default does not show you any PHP application errors. As a developer surely you need to see the errors so you can fix them. Just open your php.ini file, find display_errors directive and set it to On:
This is an interesting way to hide/show an AJAX loading gif image. Once an ajax request starts, the div containing the loading image shows up, once the request is done, the div is hidden again. I saw this on Stack Overflow and found it pretty:
-
$('#ajax_loading_div')
-
.hide() // hide it initially
-
.ajaxStart(function() {
-
$(this).show(); // show it when an AJAX request starts
-
})
-
.ajaxStop(function() {
-
$(this).hide(); // hide it when an AJAX request ends
-
});
Merging two or more PDF files with Zend_Pdf component of the Zend Framework is really simple. You just need to load the documents into Zend_Pdf instances and then iterate over their pages:
-
$pdf1 = Zend_Pdf::load('first.pdf');
-
$pdf2 = Zend_Pdf::load('second.pdf');
-
// we will merge our two PDF files into a new Zend_Pdf object
-
$pdfMerged = new Zend_Pdf();
-
-
// add all pages from the first PDF to our new document
-
foreach($pdf1->pages as $page){
-
$clonedPage = clone $page;
-
$pdfMerged->pages[] = $clonedPage;
-
}
-
// add all pages from the second PDF to our new document
-
foreach($pdf2->pages as $page){
-
$clonedPage = clone $page;
-
$pdfMerged->pages[] = $clonedPage;
-
}
-
unset($clonedPage);
-
-
// send the merged PDF document to browser
-
header('Content-type: application/pdf');
-
echo $pdfMerged->render();
A little bonus, if you need to load archived PDF documents (as I needed to recently) and merge them, you can use ZipArchive:
-
$zip = new ZipArchive;
-
$res = $zip->open('zipped_PDF_File.zip');
-
$pdf = Zend_Pdf::parse($zip->getFromIndex(0));
Hope somebody finds it useful
Many times PL/SQL won’t tell you what is wrong with your procedure and it will just print this cryptic message:
Warning: Procedure created with compilation errors.
If you don’t see any error in your code, use the following command:
SHOW ERRORS PROCEDURE <procedure_name>;
It will output something like which makes it much easier to debug the procedure:
SQL> SHOW ERRORS PROCEDURE add_book
Errors for PROCEDURE ADD_BOOK:
LINE/COL ERROR
-------- -----------------------------------------------------------------
28/3 PL/SQL: SQL Statement ignored
29/20 PL/SQL: ORA-00984: column not allowed here
You can use similar command not only with procedures:
SHOW ERRORS FUNCTION
SHOW ERRORS PACKAGE
SHOW ERRORS PACKAGE BODY
SHOW ERRORS TRIGGER
SHOW ERRORS VIEW
etc








