Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Installing Python

Prerequisites
Before installing new packages, it is always a good practice to update your system packages and
package indexes. To do that execute:

apt-get update
apt-get upgrade

Installing Apache
Since this text focuses on using Apache to serve the application, the server itself is necessary. To install
the necessary packages execute:

apt-get install apache2

Straight after installation Apache will already be running. You can check whether the Apache web
server has been properly set-up by opening your web browser and pointing it to the server IP address.
You should see a simple It works! page on the screen.

Installing pip and virtualenv


To begin working with Python and Django on a webserver, pip and virtualenv must be installed first.
Pip is a Python package manager that facilitates installing Python software packages such as Django
itself, whereas virtualenv makes it possible to create separate virtual environments for Python
applications in order to separate libraries needed for different applications and avoid version clash
between them.

To do that execute:

apt-get install python-pip python-virtualenv

This command will install pip and virtualenv from the Debian package repository. You can verify that
both tools have been properly installed by running them with
--version
switch.

root@django:~# virtualenv --version


1.7.1.2
root@django:~# pip --version
pip 1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
root@django:~#

Creating a virtual environment using virtualenv


Upon Apache installation a
/var/www
directory is automatically created in which the default web server
root is set up. We will put our new Django application there with all its dependencies.

Let's create a new directory called


sampleapp
inside that directory and enter the new directory:

cd /var/www
mkdir sampleapp
cd sampleapp

Then let's create a new virtual environment using


virtualenv
. A Python virtual environment is basically a
directory in which the Python interpreter and a local instance of
pip
resides. The local instance of
pip
installs all packages inside the virtual environment. That way no installed packages pollute global
Python installation and also there is no possibility of package version clash in a hypothetical scenario of
two applications running two different versions of Django or any other library.

To create a new virtual environment enter:

virtualenv env

where
env
is the virtual environment name - it could be any other word. The output from this
command should look like this:

root@django:/var/www/sampleapp# virtualenv env


New python executable in env/bin/python
Installing
distribute...................................................................................................................................................................
..........................done.
Installing pip...............done.
root@django:/var/www/sampleapp#

The virtual environment is now ready and can be used in two different ways.

One way is to run commands using virtual environment interpreter directly. With this method it is
necessary to always remember to execute the correct interpreter or
pip
instance, as there is a
possibility to run a system-wide one.

root@django:/var/www/sampleapp# env/bin/pip --version


pip 1.1 from /var/www/sampleapp/env/lib/python2.7/site-packages/pip-1.1-py2.7.egg (python 2.7)
root@django:/var/www/sampleapp# env/bin/python --version
Python 2.7.3
root@django:/var/www/sampleapp#

The other way is to


activate
the environment first, using

source env/bin/activate

the environment name will then be prepended to the command line as such

root@django:/var/www/sampleapp# source env/bin/activate


(env)root@django:/var/www/sampleapp#

and all commands executed will be using local virtual environment versions

(env)root@django:/var/www/sampleapp# pip --version


pip 1.1 from /var/www/sampleapp/env/lib/python2.7/site-packages/pip-1.1-py2.7.egg (python 2.7)
(env)root@django:/var/www/sampleapp# python --version
Python 2.7.3
(env)root@django:/var/www/sampleapp#

it is easier to work that way; however it is necessary to


deactivate
the environment after the work is
done using the following command

deactivate

it will return the shell to normal

(env)root@django:/var/www/sampleapp# deactivate
root@django:/var/www/sampleapp#

The freshly created environment will be used to store all necessary dependencies, including Django
and related libraries. It will also be used by Apache and mod_wsgi later on to serve the application
using correct dependencies.

Installing Django inside virtual environment


Next necessary step is to install Django inside the virtual environment. Let's do that without activating
the environment beforehand using:

env/bin/pip install django

The last messages shown after executing this command should look like this

Successfully installed django


Cleaning up...

Django is now installed inside virtual environment and is not available from within system-wide Python
installation. You can verify that behavior by importing
django
module using both interpreters

root@django:/var/www/sampleapp# python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
>>> exit()

Import using system-wide interpreter failed, whereas

root@django:/var/www/sampleapp# env/bin/python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>

the one executed inside the virtual environment succeeded.

Creating first Django project


To create a simple, basic example project we can use
django-admin.py
script as follows

env/bin/django-admin.py startproject sampleapp .

Please note the trailing


.
in the command - without it the project will be created in an additional
subdirectory. After executing that command a new
sampleapp
directory and
manage.py
script will be
created in
/var/www/sampleapp
. The
manage.py
script is used to execute Django commands for this
particular project. One of the possible uses of
manage.py
is to run a test server instance to verify that
everything is working as intended.

Please execute:

env/bin/python manage.py runserver 0.0.0.0:8000

This will run a test server bound to all interfaces on port 8000. The output should look like this:

Validating models...
0 errors found
April 08, 2014 - 12:29:31
Django version 1.6.2, using settings 'sampleapp.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

If you open your server IP address with port 8000 in your browser (the address should look
like
http://<ip address>:8000/
) you should see the It worked! example Django page. This is the result we
will work towards using Apache web server instead of the built-in Django development server.

Since the Django application is working properly, we can proceed to pair the application with Apache.

Installing mod_wsgi for Apache


The easiest and also recommended way to serve Python applications using Apache is to use
mod_wsgimodule. It is not installed by default with neither Python nor Apache, so we have to install an
additional package.

apt-get install libapache2-mod-wsgi

The next step will be to configure default Apache virtual host that at the beginning of the article served
It works! page to serve our Django application.

Configuring mod_wsgi in a default virtual host


The idea behind configuring mod_wsgi for any other virtual host in Apache is the same as the one
presented here. We will use the default virtual host for simplicity, since it is the one already provided
by a clean Apache installation.

Open the default virtual host configuration file in


nano
editor

nano /etc/apache2/sites-enabled/000-default

and add three following lines just below


<VirtualHost *:80>

WSGIDaemonProcess sampleapp
python-path=/var/www/sampleapp:/var/www/sampleapp/env/lib/python2.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias / /var/www/sampleapp/sampleapp/wsgi.py

The first line spawns a WSGI daemon process called


sampleapp
that will be responsible for serving
ourDjango application. The daemon name can be basically anything, but is good practice to *use
descriptive names such as application names here.

If we were using global Python installation and global Django instance, the
python-path
directive would
not be necessary. However, using virtual environment makes it obligatory to specify the alternate
Python path so that mod_wsgi will know where to look for Python packages.

The path must contain two directories: the directory of Django project itself -
/var/www/sampleapp
and directory of Python packages inside our virtual environment for that project
-
/var/www/sampleapp/env/lib/python2.7/site-packages
. Directories in path definition are delimited
using a colon sign.

The second line tells that particular virtual host to use the WSGI daemon created beforehand, and as
such, the daemon name must match between those two. We used
sampleapp
in both lines.

The third line is the most important, as it tells Apache and mod_wsgi where to find WSGI configuration.
The
wsgi.py
supplied by Django contains the barebone default configuration for WSGI for
servingDjango application that works just fine and changing the configuration in this file is out of this
article scope.

After these changes it is necessary to restart Apache

service apache2 restart

After that, upon opening the web browser on your server IP address, without any additional ports, you
should see the same Django page as before instead of initial It works! page that we have seen earlier.

That makes our configuration complete.

Installing PostgreSQL
By default PostgreSQl is available in Ubuntu repository. To install PostgreSQL in Ubuntu Server 13.10 is
simple, just run the following commands:
sudo apt-get update
sudo apt-get install postgresql

You can also install postresql by adding the following PPA.


sudo apt-get install python-software-properties
sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
sudo apt-get install postgresql
If youre getting an error message, make sure you already install libpq-dev.The libpq-dev package is for
compiling wrappers/clients against libpq.
sudo apt-get install postgresql-9.1 libpq-dev
Configuring PostgreSQL
Enabling TCP connections. By default TCP/IP connections is disabled due to which users will not be
able to access remotely PostgreSQL server from another computers. to enable it edit file
/etc/postgresql/9.1/main/postgresql.conf.
sudo nano /etc/postgresql/9.1/main/postgresql.conf
Change #listen_addresses = localhost to listen_addresses =192.168.1.8 it allowing remote access
only from specific ip address, if you want remote access from any computers in your network
setlocalhost to listen_addresses =*
uncomment #password_encryption = on to password_encryption = on
Save and exit ( Ctrl + O, Ctrl + X), Restart postgreSQl daemon:
sudo /etc/init.d/postgresql restart
After editing file
/etc/postgresql/9.1/main/postgresql.conf.
you need to setup the root password
PostgreSQL. In PostgreSQL, root user is postgres which by default, does not have any password. Enter
following line in terminal to set a password for the default root user postgres:
$ sudo -u postgres psql

Now psql will ask for a new password twice. Enter the new password and continue. Type \q and hit
enter to quit.
To create a database the following command can be run from the terminal:
$ sudo -u postgres createdb mydb
Installing and Configuring PhpPgAdmin
phpPgAdmin
is a php-based web application that provides a GUI interface for the postgresql. It
performs a similar function to phpMyAdmin, which allows users to manipulate database information in
a visual program in MySQL.
Execute the following command to installing PhpPgadmin in ubuntu server:
sudo apt-get install phppgadmin
By default you can only access
phppgadmin
locally. If you want to access remotely from another
computers change the following file:
sudo nano /etc/apache2/conf.d/phppgadmin
Comment out the line:

127.0.0.0/255.0.0.0 allow from :: 1/128


uncomment the line:
allow from all
Create a symbolic link /var/www/phppgadmin which would point to /usr/share/phppgadmin
sudo ln -s /usr/share/phppgadmin /var/www/
Restart apache2 daemon:
sudo service apache2 restart
Probably when trying to log into phppgadmin you receive the following message Login disallowed for
security Reasons and this can be solved by changing a configuration variable in Archiving below
sudo nano /usr/share/phppgadmin/conf/config.inc.php
By default comes as :
$conf['extra_login_security']= true;
just switch to :
$conf['extra_login_security']=false;
Now, Access phpPgadmin using web browser from any computer in your network by typing in address
bar http://host_or_ip/phppgadmin/. You should be able to login using any users youve setup in
PostgreSQL. It will be appear like the following screenshot:

You might also like