Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup mod_wsgi on a centos 6 server

I have recently completed the development of a django 2 app and part of the requirements is to deploy it to an apache 2 server on a centos machine. The django app is written with python3 and the centos machine has python2 pre-installed.

So far, I have been having a hard time installing mod_wsgi on the apache2 server and deploying the app.

Any help would be appreciated.

EDIT I have resolved the Issue. See my answer below

like image 303
Ohia George Avatar asked Dec 17 '22 22:12

Ohia George


1 Answers

1. Clean install of python 3.6.5 to usr/local and usr/local/lib

I ensured YUM was upto date:

$ yum update

Compilers and related tools:

$ yum groupinstall -y "development tools"

libraries needed during compilation to enable all features of Python:

    $ yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite- 
devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel 
expat-devel

installing Python 3.6.5:

$ wget http://python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
$ tar xf Python-3.6.5.tar.xz
$ cd Python-3.6.5
$ ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath 
/usr/local/lib"
$ make && make altinstall

2. Installed mod_wsgi using pip install

using the just installed python3.6 i ran the following command

$python3.6 -m pip install mod_wsgi

3. Load Mod_wsgi into apache module

locate apache config file /etc/apache2/conf/http.conf and add following command to load mod_wsgi by specifying the installation path. In my case, added the the loadModule command to /etc/apache2/conf.d/includes/pre_main_global.conf

$ LoadModule wsgi_module /usr/local/lib/python3.6/site- 
packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so

4. Restart Apache Server and confirm that mod_wsgi is loaded

$apachectl restart
$apachectl -M

look through the list and ensure that mod_wsgi is part of the modules that is loaded by apache.

5. configure Virtual host and perform a simple hello.py test

i have a server that is already hosting 7 different sites. i simply created a new subdomain and modified the subdomain virtualhost to look like

<VirtualHost mysite.com_ip:80>
ServerName wsgitest.mysite.com
ServerAlias www.wsgitest.mysite.com
DocumentRoot /home/account_username/public_html/wsgitest
ServerAdmin [email protected]

<Directory /home/account_username/public_html/wsgitest>
Order allow,deny
Allow from all
</Directory>

WSGIScriptAlias / /home/account_username/public_html/wsgitest/hello.py

ErrorLog /home/account_username/public_html/wsgitest/wsgitest_error.log
</VirtualHost>

restart apache after modifying the virtualhost

create a hello.py wsgi application according to this site - http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

hello.py

def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'

response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

so my wsgitest application looks like this on the server /home/account_username/public_html/wsgitest/hello.py

Finally, test the site on the browser like so - wsgitest.mysite.com you should see 'Hello World'

I hope this helps someone

like image 149
Ohia George Avatar answered Dec 20 '22 10:12

Ohia George