Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
vms:python:django-apache [2018/07/09 18:00]
admin [Mounting the host File System]
vms:python:django-apache [2018/08/27 23:52] (current)
admin [Using virtualenvwrapper]
Line 197: Line 197:
 As we've just experienced,​ mounting the directory through **SSHFS** the way we've just done it, we're asked for a password. Automating the process at startup will require authentication using an RSA key, so we'll first prepare [[https://​www.digitalocean.com/​community/​tutorials/​how-to-set-up-ssh-keys--2|RSA keys authentication]] between the VM and our host system: As we've just experienced,​ mounting the directory through **SSHFS** the way we've just done it, we're asked for a password. Automating the process at startup will require authentication using an RSA key, so we'll first prepare [[https://​www.digitalocean.com/​community/​tutorials/​how-to-set-up-ssh-keys--2|RSA keys authentication]] between the VM and our host system:
  
-<​Code:​bash|as **root** user in VM>+<​Code:​bash|as **django** user in VM>
 > cd ~/ > cd ~/
 > ssh-keygen -t rsa > ssh-keygen -t rsa
Line 226: Line 226:
 We now copy our user's ''​id_rsa.pub''​ key to our host system: We now copy our user's ''​id_rsa.pub''​ key to our host system:
  
-<​Code:​bash|as **root** user in VM>+<​Code:​bash|as **django** user in VM>
 > ssh-copy-id devuser@172.20.20.1 > ssh-copy-id devuser@172.20.20.1
 /​usr/​bin/​ssh-copy-id:​ INFO: Source of key(s) to be installed: "/​home/​ddi/​.ssh/​id_rsa.pub"​ /​usr/​bin/​ssh-copy-id:​ INFO: Source of key(s) to be installed: "/​home/​ddi/​.ssh/​id_rsa.pub"​
Line 333: Line 333:
 We keep python 2.7 as default to make sure nothing brakes as some packages, commands and utilities are relying on it. But we'll be able to use Python 3 for our Django development. We keep python 2.7 as default to make sure nothing brakes as some packages, commands and utilities are relying on it. But we'll be able to use Python 3 for our Django development.
  
 +==== Python 3.6(.4) ====
 +
 +In case you need to use Python 3.6, here is how to compile it on Debian 9. As this is quite a power hungry process, one might consider allowing sufficient resources to the VM before executing those commands, with an i7-2720QM CPU, a 4 core / 1 GB RAM VM takes about 20 minutes to complete the compilation...
 +
 +<​Code:​bash|As root user>
 +> apt-get update && apt-get upgrade
 +> apt-get install -y make build-essential libssl-dev zlib1g-dev
 +> apt-get install -y libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
 +> apt-get install -y libncurses5-dev ​ libncursesw5-dev xz-utils tk-dev
 +> wget https://​www.python.org/​ftp/​python/​3.6.4/​Python-3.6.4.tgz
 +> tar xvf Python-3.6.4.tgz
 +> cd Python-3.6.4
 +> ./configure --enable-optimizations
 +> make -j8
 +> make altinstall
 +</​Code>​
 +
 +To test that Python 3.6 is well installed and working, enter a Python shell:
 +
 +<​Code>​
 +> python3.6
 +Python 3.6.4 (default, Aug 27 2018, 23:04:48)
 +[GCC 6.3.0 20170516] on linux
 +Type "​help",​ "​copyright",​ "​credits"​ or "​license"​ for more information.
 +>>>​
 +</​Code>​
 +
 +The Python3.6(.4) binary will be located in ''/​usr/​local/​bin/​python3.6''​\\
 +You might want to add it to the available alternatives using the above mentioned method.
 ===== Configure the Python Virtual Environment ===== ===== Configure the Python Virtual Environment =====
 +
 +==== Using virtualenv ====
  
 We now need to create a Python virtual environment so that our Django project will be separated from the system'​s tools and any other Python projects we may be working on, we need to install the ''​virtualenv''​ command to create these environments,​ this can be done using the ''​pip''​ command. Lets' install it **for Python 3**: We now need to create a Python virtual environment so that our Django project will be separated from the system'​s tools and any other Python projects we may be working on, we need to install the ''​virtualenv''​ command to create these environments,​ this can be done using the ''​pip''​ command. Lets' install it **for Python 3**:
  
-<​Code:​bash>​+<​Code:​bash|As dev user>
 > update-alternatives --config python > update-alternatives --config python
 update-alternatives --config python update-alternatives --config python
Line 383: Line 414:
 Your prompt should change to indicate that you are now operating within a Python virtual environment,​ indicating the virtualenv name between parenthesis. Your prompt should change to indicate that you are now operating within a Python virtual environment,​ indicating the virtualenv name between parenthesis.
  
 +==== Using virtualenvwrapper ====
 +
 +''​virtualenvwrapper''​ keeps all your virtualenvs in one place, and provides convenient tools for activating and deactivating them.
 +
 +<​Code:​bash| As dev user>
 +> pip3 install virtualenvwrapper
 +> nano ~/.bashrc
 +
 +ADD
 +# LOAD VIRTUALENVWRAPPER AUTOMATICALLY
 +source virtualenvwrapper.sh
 +
 +> source ~/.bashrc
 +</​Code>​
 +
 +Let's create a ''​virtualenv''​ specifying the Python version to use:
 +
 +<​Code:​bash|As dev user>
 +> mkvirtualenv --python=python3.5 <​djangoenv>​
 +</​Code>​
 +
 +To **activate** the ''​superlist virtualenv'':​
 +<​Code:​bash|As dev user>
 +> workon <​djangoenv>​
 +(<​djangoenv>​) >
 +</​Code>​
 +
 +To **deactivate** the current ''​virtualenv'':​
 +<​Code:​bash|As dev user>
 +(<​djangoenv>​) > deactivate
 +>
 +</​Code>​
 +
 +In case you ever need to **remove** a ''​virtualenv'':​
 +<​Code:​bash|As dev user>
 +> rmvirtualenv <​djangoenv>​
 +</​Code>​
 ===== Install Django ===== ===== Install Django =====
  
Line 397: Line 465:
 Installing collected packages: pytz, django Installing collected packages: pytz, django
 Successfully installed django-2.0.6 pytz-2018.4 Successfully installed django-2.0.6 pytz-2018.4
 +</​Code>​
 +
 +=== Django version ===
 +
 +It is possible to select the version of Django you'd like to install using the ''<''​ or ''​=''​ option:
 +
 +<​Code:​bash|as dev user>
 +> cd ~/​your/​project/​path/​
 +> pip3 install "​django<​1.12"​
 </​Code>​ </​Code>​
  
Line 575: Line 652:
  
 We should now be able to access our dev environment typing by ''​myproject.dev''​ in our host's web browser. We should now be able to access our dev environment typing by ''​myproject.dev''​ in our host's web browser.
 +
 +===== Selenium, Firefox & Geckodriver =====
 +
 +If you plan to use Selenium for functional tests or to scrape websites content, you'll need to install Firefox and Geckodriver.
 +
 +Though Firefox can be launched in //​headless//​ mode using the ''​-headless''​ option, it still requires the ''​libgtk-3-0''​ and ''​xvfb''​ packages to be installed in order to run, this has been reported in Bugzilla ([[https://​bugzilla.mozilla.org/​show_bug.cgi?​id=1372998]]) but seems unlikely to ever be addressed by the Mozilla community :-(
 +
 +Here are the steps to get Firefox running on a headless (no X11) system:
 +
 +<​Code:​bash|As root>
 +> wget -O FirefoxSetup.tar.bz2 "​https://​download.mozilla.org/?​product=firefox-latest&​os=linux64&​lang=en-US"​
 +> tar xvf FirefoxSetup.tar.bz2
 +> mv firefox/ /opt/
 +> apt-get install libgtk-3-0 xvfb
 +> /​opt/​firefox/​firefox -headless
 +*** You are running in headless mode.
 +</​Code>​
 +
 +You can check for the latest Geckodriver version on [[https://​github.com/​mozilla/​geckodriver/​releases|this github page]]. Note that we will install the ''​geckodriver''​ binary in our local user path, so we'll update our user's ''​.bashrc''​ file to access it under ''​~/​.local/​bin'',​ because this is also where Python will install things when you use ''​pip install --user''​.
 +
 +<​Code:​bash|As dev user>
 +> mkdir -p ~/​.local/​bin
 +> cd ~/​.local/​bin
 +> wget https://​github.com/​mozilla/​geckodriver/​releases/​download/​v0.21.0/​geckodriver-v0.21.0-linux64.tar.gz
 +> tar xvf geckodriver-v0.21.0-linux64.tar.gz
 +> nano ~/.bashrc
 +
 +ADD
 +# LOCAL BINARIES
 +PATH=~/​.local/​bin/:​$PATH
 +
 +> geckodriver --version
 +geckodriver 0.21.0
 +...
 +...
 +</​Code>​
 +
 +