Jeśli jesteś właścicielem tej strony, możesz wyłączyć reklamę poniżej zmieniając pakiet na PRO lub VIP w panelu naszego hostingu już od 4zł!

Archive for Styczeń, 2012

Francois Zaninotto’s Blog: Node.js for PHP Programmers #1: Event-Driven Programming… and Pasta

Francois Zaninotto’s Blog: Node.js for PHP Programmers #1: Event-Driven Programming… and Pasta

On his blog today Francois Zaninotto has a post that aims to introduce those with a PHP background to how Node.js works. In this first part of his series, he tries to explain the idea of event-driven programming – parallel processing of different parts of an application based on some action taken in the code.

For a PHP developer, asynchronicity is the most puzzling aspect of the Node.js runtime. It’s simply a new way to write programs. And once you pass the first learning steps, event-driven programming opens a world of possibilities PHP programmers would never dream of. I’ll try to explain you how it works, but first, let’s talk about pasta.

In his “pasta” example, he shows how a typical PHP application would make a “Pan” object, call a “warm” action, “add olive oil”, etc. All of this happens in sequence, though and takes 29 “minutes” to complete. To help things along, he implements an “EventLoop” class that handles tracking the timing and includes two methods to execute callbacks and delayed methods. He expands on this example with asynchronous objects and method calls to handle multiple things at once. He relates this to what Node.js offers – a built in event handling system, an included EventLoop object and native blocking I/O operations.

Source: http://www.phpdeveloper.org/news/17450

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Django missing admin CSS stylesheets

Django missing admin CSS stylesheets

Follow up on three of my previous posts:

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 :)

Source: http://blog.richardknop.com/2012/01/django-missing-admin-css-stylesheets/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

PostgreSQL chear sheet

PostgreSQL chear sheet

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

Source: http://blog.richardknop.com/2012/01/postgresql-chear-sheet/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Install PostgreSQL on FreeBSD 8.2 and make it work with Django

Install PostgreSQL on FreeBSD 8.2 and make it work with Django

Follow up on my previous two blog posts:

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.

Source: http://blog.richardknop.com/2012/01/install-postgresql-on-freebsd-8-2-and-make-it-work-with-django/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

How to install and configure Django with mod_wsgi on FreeBSD 8.2

How to install and configure Django with mod_wsgi on FreeBSD 8.2

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:

Source: http://blog.richardknop.com/2012/01/how-to-install-and-configure-django-with-mod_wsgi-on-freebsd-8-2/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

How to install and configure mod_wsgi on FreeBSD 8.2

How to install and configure mod_wsgi on FreeBSD 8.2

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.

Source: http://blog.richardknop.com/2012/01/how-to-install-and-configure-mod_wsgi-on-freebsd-8-2/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Publish your new website the right way

Publish your new website the right wayInstalling a website is maybe a routine job, but you should check your site to avoid stupid mistakes. Many mistakes or shortcomings doesn’t have a huge effect on your website, but some of them may have some influence on your rankings in Google. This article is a checklist you should follow for all your new and updated websites.

Rating: 0.0/10 (0 votes cast)


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/KHbAfe9XMFQ/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Publish your new website the right way

Publish your new website the right wayInstalling a website is maybe a routine job, but you should check your site to avoid stupid mistakes. Many mistakes or shortcomings doesn’t have a huge effect on your website, but some of them may have some influence on your rankings in Google. This article is a checklist you should follow for all your new and updated websites.

Rating: 0.0/10 (0 votes cast)


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/KHbAfe9XMFQ/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

PHPMaster.com: MongoDB Revisited

PHPMaster.com: MongoDB Revisited

In this new post to PHPMaster.com today Ahmed Shreef continues on from his previous introduction to MongoDB and gets into more detail on things like cursors, query operators, queries on embedded documents and the sort/skip methods.

In my previous article Introduction to MongoDB I discussed installing Mongo, its PHP extension, and how to perform simple insert and find operations. Of course there are many, many more features than what I mentioned so I wanted to write another article to show you some of them.

Other topics mentioned include queries on arrays of data and running queries with indexes to improve their performance. Code is also included for each example.

Source: http://www.phpdeveloper.org/news/17407

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

PHPMaster.com: MongoDB Revisited

PHPMaster.com: MongoDB Revisited

In this new post to PHPMaster.com today Ahmed Shreef continues on from his previous introduction to MongoDB and gets into more detail on things like cursors, query operators, queries on embedded documents and the sort/skip methods.

In my previous article Introduction to MongoDB I discussed installing Mongo, its PHP extension, and how to perform simple insert and find operations. Of course there are many, many more features than what I mentioned so I wanted to write another article to show you some of them.

Other topics mentioned include queries on arrays of data and running queries with indexes to improve their performance. Code is also included for each example.

Source: http://www.phpdeveloper.org/news/17407

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>