The goal is to "draw" Hello World "using mod-wsgi". What is mod-wsgi? It is also used to use the Python Web Framework Django.
As a premise, it is assumed that the following is done. --Install pyenv and pyenv-virtualenv
environment OS: Debian 9.0 stretch Python: 3.6.3
install apache2
sudo apt install -y apache2 apache2-dev
You need to be a little careful. This is because there are two ways to install it: apt-get and pip. In the case of apt-get, it is apache2's libapache2-mod-wsgi that is installed, but it will spit out warnings due to the difference between later compiled Python and running Python. Therefore, we recommend using pip to install it.
First, make sure you have selected the version of python you want to use
pyenv versions
If you can confirm, install mod-wsgi
pip install mod-wsgi
Check just in case
pip freeze | grep wsgi
mod-wsgi==4.6.5
It's OK.
Unlike the case where libapache2-mod-wsgi is installed, a little setting is required. The following files are created when mod-wsgi is installed with pip.
/home/USERNAME/.pyenv/versions/3.6.3/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
Let apache2 recognize this file.
sudo vim /etc/apache2/mods-available/wsgi.load
Write the following line. Don't forget to replace USERNAME.
LoadModule wsgi_module /home/USERNAME/.pyenv/versions/3.6.3/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
Then enable wsgi.
sudo a2enmod wsgi
It is OK if the following comments appear.
Enabling module wsgi.
To activate the new configuration, you need to run:
systemctl restart apache2
Please restart apache2.
sudo service apache2 restart
I will play with the Apache2 configuration files (/ etc / apache2 /), but make a backup just in case.
cd /etc/apache2/sites-available
sudo cp 000-default.conf 000-default.conf.bk
sudo vim 000-default.conf
Add the following to the last line:
By the way, in vim, G (shift + g)
moves to the last line.
WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py
Create the test_wsgi.py file specified in WSGIScriptAlias above.
sudo vim /var/www/html/test_wsgi.py
The contents of the file should be as follows.
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]
Restart apache2 when the above is complete.
sudo service apache2 restart
After this, access the domain name (or IP) / test_wsgi (Http ← be careful) and you should see Hello World.