Skip to main content

How To Serve Django python Applications with Apache and mod_wsgi on Ubuntu 20 LTS with virtualenv

Best way for stability is direct web server without running extra software layers. No virtualization and containers whatsoever. But as an application is already quite elderly and not using latest versions of python. It can be complicated to install directly on the server. I am giving an example of a working setup and number of attempts that ended with failure. I repeat for me the recent versions of python3 Django and postgres and elastic work best directly on the server without virtualization. This method is only published for information for the old version of the application where only the hardware layer needs to be updated and no software upgrade is desired.

Environment setup example.


https://www.youtube.com/watch?v=N5vscPTWKOk 


https://code.google.com/archive/p/modwsgi/wikis/VirtualEnvironments.wiki 



vim wsgi.py

import os

import sys

import site



site.addsitedir('/home/sites/venv/lib/python2.7/site-packages')

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

os.environ["DJANGO_SETTINGS_MODULE"] = "jobfor.settings.settings"



activate_env=os.path.expanduser("/home/sites/venv/bin/activate_this.py")

execfile(activate_env, dict(__file__=activate_env))


#from raven.contrib.django.raven_compat.middleware.wsgi import Sentry

from django.core.wsgi import get_wsgi_application


#application = Sentry(get_wsgi_application())

application = get_wsgi_application()


Test performance with


apt-get install apache2-utils


If you need  configuration of static files and / or upload to media


ls -al /var/www/ 

 

 

chgrp -R www-data vindazo_de/

chmod -R g+w vindazo_de/


Example apache2 config


vim /etc/apache2/sites-enabled/000-default.conf


<VirtualHost 116.202.162.47:80>
ServerName dev.vindazo.de
ErrorLog /var/log/apache2/vindazo_de_http_error.log
LogLevel warn
CustomLog /var/log/apache2/vindazo_de_http_custom.log combined

LimitRequestBody 5120000
WSGIDaemonProcess dev.vindazo.de processes=100 threads=100 maximum-requests=100000 display-name=%{GROUP}
WSGIProcessGroup dev.vindazo.de
WSGIScriptAlias / /home/sites/development/de_vindazo/jobfor/wsgi.py


Alias /media/ /var/www/vindazo_de/media/
<Location "/media">
SetHandler None
Options -Indexes
AddType text/html php
#AllowOverride None
</Location>

Alias /static/ /var/www/vindazo_de/static/
<Location "/static">
SetHandler None
Options -Indexes
AddType text/html php
#AllowOverride None
</Location>
</VirtualHost>



Install Apache modules 


a2enmod headers


a2enmod expires


a2enmod rewrite


a2enmod ssl


systemctl restart apache2

 


Apache mod_wsgi without VirtualEnvirment and dockers


When you have decided to web server on Ubuntu without dockers en without VirtualEnvirment.

You can try but with me it has not worked recently because python 3 and python 2.7 could not run at the same time. While the method itself can be useful in some cases. Still, this method is only recommended with python and all recent Django and postgres packges.


Create requirements file


pip freeze > requirements-13-04-2021.txt


Copy all directories to new server development and deployment 


apt-get update


apt install python2


apt-get install vim


curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py


python2 get-pip.py


pip2 install -U pip setuptools wheel



Install all required package from requirements.xt



some problems with install  

pip2 install blis==0.7.4     

cryptography==1.7.2

cymem==2.0.5

De-core-news-lg ( spacy ) 

En-core-web-lg

murmurhash==1.0.5

oauth2client==4.1.3

preshed==3.0.5 

pycurl==7.43.0

reportlab==3.3.0

spacy==2.3.5



I did not install these packages. I get an error from pip.. 

Then have installed the Dev version with gcc. 


apt-get install python2-dev

apt install build-essential 



After that all packages are installed. 



Depending on the


pip2 install idna==2.10 

pip2 install oauthlib==3.1.0


Difference  


pip2 install psycopg2==2.6

pip2 install psycopg2==2.7


And problem with binary import error ( Problem loading psycopg2 with glibc 2.26, version GLIBC_PRIVATE psycopg2)



 pip2 install psycopg2-binary 



Then problem with 


Could not run curl-config: [Errno 2] No such file or directory


Fix it with 


apt install libcurl4-openssl-dev libssl-dev



Ok, about 1 hour to install django and all packages. 

Tomorrow I will run a server.. In development mode and move apache configs and install memcached and Elasticsearch. 


Apache configuration


apt install apache2

apt-get install libapache2-mod-wsgi


Forbidden

You don't have permission to access this resource.


Apache/2.4.41 (Ubuntu) Server at dev.vindazo.de Port 80




vim /etc/apache2/apache2.conf



<Files wsgi.py>

    Require all granted

</Files>




vim jobfor/wsgi.py



site.addsitedir('/usr/local/lib/python2.7/site-packages/')


When I have added it in the path and all not helped. This error  says that django is not installed and. It looks in default directory of operating system and its python3 


ImportError: No module named django.core.wsgi 


WSGIPythonHome


indicate to Python when it is initialised where its library files are installed 


>>> sys.path

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']



Crashed and unable to run mod_wsgi with python2

 

If they had activated the Python 3 virtual environment and its bin directory was in there path, then likely they could simply run 'pip' rather than 'pip3'. You should run 'which pip' to verify whether it was coming from the virtual environment you expect.

The 'mod_wsgi-httpd' package is specifically for installing a distinct instance of Apache itself to get around issues where the system wide Apache is out of date or missing development header files, or otherwise can't modify the system wide configuration. It is generally of more relevance when you don't have root access to fix system wide issues and are only interested in running on an unprivileged port and so do not need root access. You would also have to be using 'mod_wsgi-express' from the 'mod_wsgi' package which was 'pip' installed after 'mod_wsgi-httpd' had been installed as that is the only way to use 'mod_wsgi-httpd' installed version of Apache.

So, for 'mod_wsgi-httpd' you would really want to ignore it. If you have already installed it, then 'pip' uninstall both it and 'mod_wsgi'. The latter needs to be uninstalled as it will be bound to the 'mod_wsgi-httpd' Apache version and the 'mod_wsgi.so' will not work with the system wide.

As to separate users steps in link, they look correct, although I would say that in step 3, it should be highlighted that the LoadModule and WSGIPythonHome lines to be added are what is output when running mod_wsgi-express install-module command. What are output is customised to match what your installation should be. So don't make up values, just use what that command output.

If you have a wsgi.load file still around, run sudo a2dismod wsgi to get rid of it or otherwise manually remove it. It is a left over from old system packaged mod_wsgi.

 

https://pypi.org/search/?q=Non+standard+Apache+installations 

 

Non standard Apache installations 

 

https://pypi.org/search/?q=Connecting+into+Apache+installation&o=

 

Connecting into Apache installation 

 

mod_wsgi-express module-config 

 

 

Comments

Popular posts from this blog

Tekstverwerking python Text processing python SpaCy, TensorFlow, NLTK, Allen-NLP, Stanford-NLP

 Dit post maakt gebruik van spaCy, een populaire Python-bibliotheek die de taalgegevens en algoritmen bevat die je nodig hebt om teksten in natuurlijke taal te verwerken. Zoals u in dit post zult leren, is spaCy gemakkelijk te gebruiken omdat het containerobjecten biedt die elementen van natuurlijke taalteksten vertegenwoordigen, zoals zinnen en woorden. Deze objecten hebben op hun beurt attributen die taalkenmerken vertegenwoordigen, zoals delen van spraak. Op het moment van schrijven bood spaCy voorgetrainde modellen aan voor Engels, Duits, Grieks, Spaans, Frans, Italiaans, Litouws, Noors BokmÃ¥l, Nederlands, Portugees en meerdere talen gecombineerd. Bovendien biedt spaCy ingebouwde visualizers die u programmatisch kunt aanroepen om een grafische weergave van de syntactische structuur van een zin of benoemde entiteiten in een document te genereren.   De spaCy-bibliotheek ondersteunt ook native geavanceerde NLP-functies die andere populaire NLP-bibliotheken voor Python niet hebben. Spa

Google Closure

   Closure Library De Closure-bibliotheek is een JavaScript-bibliotheek, vergelijkbaar met andere moderne producten zoals jQuery, Angular, Vue.js, Dojo en MooTools. De coderingsstijl en het gebruik van opmerkingen in de Closure-bibliotheek zijn op maat gemaakt voor Closure Compiler. In vergelijking met andere JavaScript-bibliotheken is het de belangrijkste onderscheidende factor van Closure Compiler. Een eenvoudig compressie-experiment ontdekte dat wanneer Closure Compiler wordt gebruikt in plaats van YUI Compressor, de Closure Lib-code met 85% kan worden verminderd, wat een enorme impact kan hebben op de codecompressiecapaciteit van de compiler.    De implementatie van de  closure bibliotheek richt zich ook op leesbaarheid en prestaties. Wees zuinig bij het maken van objecten, maar wees genereus bij het benoemen en opnemen van objecten. Het heeft ook een prachtig gebeurtenissysteem, ondersteuning voor klassen en overerving en verschillende UI-componenten, waaronder bijvoorbeeld een ri

Elasticsearch install and configuration on Ubuntu

If you plan to use elastic integrated with Django then it would be better to use an old version of Elastic or replace Haystack with Django-Elasticsearch-DSL (Not tested) See old version install at bottom of this post.  New version Elasticsearch ( attention no haystack at this time )  The Elasticsearch components are not available in Ubuntu’s default package repositories. They can, however, be installed with APT after adding Elastic’s package source list. curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation apt-get install gnupg curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). OK echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list apt update apt