Ubuntu 16.04
Sponsored Link

Python + mod_wsgi2016/06/13

 
Install mod_wsgi (WSGI : Web Server Gateway Interface) to make Python scripts be fast.
[1] Install mod_wsgi.
root@www:~#
apt-get -y install libapache2-mod-wsgi
[2] For example, configure mod_wsgi that it's possible to access to /test_wsgi that backend is /var/www/html/test_wsgi.py.
root@www:~#
vi /etc/apache2/conf-available/wsgi.conf
# create new

WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py
root@www:~#
a2enconf wsgi

Enabling conf wsgi.
To activate the new configuration, you need to run:
service apache2 reload
root@www:~#
systemctl restart apache2
[3] Create a test script which you set above.
root@www:~#
vi /var/www/html/test_wsgi.py
# create new

def application(environ,start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'mod_wsgi Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]
[4] Configure if you use Django. ( refer to installing Django )
For exmaple, configure "testapp" under the "/home/ubuntu/venv/testproject" which is owned by "ubuntu".
root@www:~#
vi /etc/apache2/conf-available/django.conf
# create new

WSGIDaemonProcess testapp python-path=/home/ubuntu/venv/testproject:/home/ubuntu/venv/lib/python2.7/site-packages
WSGIProcessGroup testapp
WSGIScriptAlias /django /home/ubuntu/venv/testproject/testproject/wsgi.py

<Directory /home/ubuntu/venv/testproject>
    Require all granted
</Directory>
root@www:~#
a2enconf django

Enabling conf django.
To activate the new configuration, you need to run:
service apache2 reload
root@www:~#
systemctl restart apache2
Matched Content