UPWORK/ODESK guru: Tips and Tricks for clients

skype: odesk.by / upwork.link , email: [email protected]/upwork.link

Additional version of Python 2.7.* from source code, local pip and mod_wsgi on Apache2.4

without comments

Python – bunch of difficult sets of scripts on 80%. It is have own pros and cons. Very often – it is problems of difficult dependencies of application, when in OS we see old version of Python interpretator.
Building own version from source is the most appropriate variant of solving if need to use it only for local application in environment of some user or web servers.
Here is sample of commands of shell for getting copy of installation of Python in special directory – /opt.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash

mkdir tmp.python                    
cd tmp.python                        
#mkdir /opt/python2.7.13 # !!!! will check permissions for write !!!!
wget --no-check-certificate http://python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
tar xvJf Python-2.7.13.tar.xz        
# chown root.root Python2.7.13 - !! If in env of root !!
cd Python-2.7.13                    
                                     
./configure --prefix=/opt/python2.7.13  --exec-prefix=/opt/python2.7.13 \
            --enable-shared --enable-ipv6 --enable-unicode --with-threads \
            --with-doc-strings      
                                     
make                                
# if need the restrictions of users
# chown user3033.grp_user3033 python2.7.13
                                     
make install                        
# make altinstall !! alternative command !!
                                     
# under root user account            
# ln -s /opt/python2.7.13 /opt/python                                                                                                                                  
#echo "/opt/python2.7.13/lib" >> /etc/ld.so.conf.d/opt-python2.7.conf
#ldconfig

btw, ‘altinstall’ skips creating the python link and the manual pages links in main system, ‘install’ will hide the system binaries and manual pages.

On next step need to update environment of users, which plan use this version:


1
2
export PATH=$HOME/.local/bin:$HOME/bin:/opt/python2.7.13/bin:$PATH
export PYTHONPATH=$HOME/.local/lib/python2.7:/opt/python2.7.13/lib/python2.7

Now user can continue work with python in own directory. On this stage can install pip locally.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py

[email protected]:~> python get-pip.py --user
Collecting pip
  Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 200kB/s
Collecting setuptools
  Downloading setuptools-34.3.0-py2.py3-none-any.whl (389kB)
    100% |████████████████████████████████| 399kB 195kB/s
Collecting wheel
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 222kB/s
Collecting six>=1.6.0 (from setuptools)
  Downloading six-1.10.0-py2.py3-none-any.whl
Collecting appdirs>=1.4.0 (from setuptools)
  Downloading appdirs-1.4.2-py2.py3-none-any.whl
Collecting packaging>=16.8 (from setuptools)
  Downloading packaging-16.8-py2.py3-none-any.whl
Collecting pyparsing (from packaging>=16.8->setuptools)
  Downloading pyparsing-2.1.10-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 346kB/s
Installing collected packages: pip, six, appdirs, pyparsing, packaging, setuptools, wheel
Successfully installed appdirs-1.4.2 packaging-16.8 pip-9.0.1 pyparsing-2.1.10 setuptools-34.3.0 six-1.10.0 wheel-0.29.0

[email protected]:~> which pip
/.../webapp01/.local/bin/pip

Installation virtualenv locally:


1
2
3
4
5
6
7
8
9
[email protected]:~> pip install --user virtualenv
Collecting virtualenv
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 374kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
[email protected]:~> which virtualenv
/.../webapp01/.local/bin/virtualenv
[email protected]:~>

And now we can get a pseudo-virtual environment of python. Python style for local modules of user. Of course, will be links on system modules, but installation with pip now will be use the local directories.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[email protected]:~/public_html/digital> virtualenv env
New python executable in /.../webapp01/public_html/digital/env/bin/python
Installing setuptools, pip, wheel...done.

[email protected]:~/public_html/digital> tree
.
`-- env
    |-- bin
    |   |-- activate
    |   |-- activate.csh
    |   |-- activate.fish
    |   |-- activate_this.py
    |   |-- easy_install
    |   |-- easy_install-2.7
    |   |-- pip
    |   |-- pip2
    |   |-- pip2.7
    |   |-- python
    |   |-- python-config
    |   |-- python2 -> python
    |   |-- python2.7 -> python
    |   `-- wheel
    |-- include
    |   `-- python2.7 -> /opt/python2.7.13/include/python2.7

...

Before we install our project’s Python requirements, we need to activate the virtual environment. You can do that by
typing

[email protected]:~/public_html/digital> source env/bin/activate
(env) [email protected]:~/public_html/digital> 

With your virtual environment active, install Django with the local instance of pip by typing:


1
2
3
4
5
6
7
(env) [email protected]:~/public_html/digital> pip install django
Collecting django
  Downloading Django-1.10.6-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 121kB/s
Installing collected packages: django
Successfully installed django-1.10.6
(env) [email protected]:~/public_html/digital>

Since we already have a project directory, we will tell Django to install the files here. It will create a second level directory
with the actual code, which is normal, and place a management script in this directory. The key to this is the dot at the end
that tells Django to create the files in the current directory:


1
2
3
4
5
6
7
(env) [email protected]:~/public_html/digital> django-admin.py startproject digital .
(env) [email protected]:~/public_html/digital> ll
total 12
drwxr-xr-x 2 webapp01 webapp01 4096 Mar  2 16:02 digital
drwxr-xr-x 5 webapp01 webapp01 4096 Mar  2 15:51 env
-rwxr-xr-x 1 webapp01 webapp01  805 Mar  2 16:02 manage.py
(env) [email protected]:~/public_html/digital>

The first thing we should do with our newly created project files is adjust the settings. Open the settings file with your text editor (digital/settings.py). We are going to be using the default SQLite database in this guide for simplicity’s sake, so we don’t actually need to change too much. We will focus on configuring the static files directory, where Django will place static files so that the web server can serve these easily. At the bottom of the file, we will add a line to configure this directory. Django uses the STATIC_ROOT setting to determine the directory where these files should go. We’ll use a bit of Python to tell it to use a directory called “static” in our project’s main directory:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Now, we can migrate the initial database schema to our SQLite database using the management script. Create an administrative user for the project… . Here you can find useful options for creation of password – https://docs.djangoproject.com/en/1.10/topics/auth/passwords/#password-validation

(env) [email protected]:~/public_html/digital> ./manage.py makemigrations
No changes detected
(env) [email protected]:~/public_html/digital> ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
(env) [email protected]:~/public_html/digital> 

(env) [email protected]:~/public_html/digital> ./manage.py createsuperuser
Username (leave blank to use 'webapp01'): 
Email address: [email protected]
Password: 
Password (again): 
Superuser created successfully.
(env) [email protected]:~/public_html/digital> 

Finally, we can test your project. In your web browser, visit your server’s domain name or IP address followed by :8000 :

./manage.py runserver 0.0.0.0:8000
(http://server_domain_or_IP:8000)

You should see the default Django index page. If you append /admin to the end of the URL in the address bar, you will be prompted for the administrative username and
password you created with the createsuperuser command. When you are finished exploring, hit CTRL-C in the terminal window to shut down the development server.

We’re now done with Django for the time being, so we can back out of our virtual environment by typing: deactivate

Next, preparation of Apache and module mod_wsgi you can find in other articles on site.

Written by spectre

March 2nd, 2017 at 11:07 am

Posted in Python snake

Leave a Reply

*