I summarized the procedure to create a Django deployment environment with mod_wsgi. Since it is a memorandum main, only the procedure is written. If you want to understand it properly, you should read other articles.
Connect to the Ubuntu server with ssh and prepare the environment.
apache
If you haven't installed apache yet, install it with the following command.
$ sudo apt-get install apache2
python , Django
Then install python from pyenv. See below. (Let's fix the python version to 3.5.1) http://qiita.com/tonkatu05/items/d0422a2050d669c72f54#1-%E7%92%B0%E5%A2%83%E6%A7%8B%E7%AF%89
$ python --version
Python 3.5.1
Also install Django.
$ pip install django==1.9.4
mod_wsgi
Then install mod_wsgi.
$ sudo apt-get install libapache2-mod-wsgi-py3
(* Another method [Install mod_wsgi from source](## Install mod_wsgi from source))
Create /home/hoge/hello.py.
hello.py
import sys
def application(environ, start_response):
status = '200 OK'
output = b'Hello World! python version : ' + sys.version.encode("utf-8")
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Create /etc/apache2/sites-available/python.conf.
python.conf
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /home/hoge/hello.py
# WSGIScriptAlias / /home/hoge/TestProject/TestProject/wsgi.py
WSGIPythonPath /home/hoge/TestProject:/home/hoge/.pyenv/versions/3.5.1/lib/python3.5/site-packages
<Directory /home/hoge>
Require all granted
</Directory>
Reflect the settings.
$ sudo a2ensite python.conf
Restart apache.
$ sudo service apache2 restart
Try accessing from a browser.
Hello World! python version : 3.4.3 (default, Oct 14 2015, 20:31:36)
[GCC 4.8.4]
Is displayed. (The python version is a mystery)
$ cd
$ django-admin startproject TestProject
Edit /home/hoge/TestProject/TestProject/wsgi.py.
wsgi.py
"""
WSGI config for TestProject project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('home/hoge/TestProject')
sys.path.append('home/hoge/TestProject/TestProject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TestProject.settings")
application = get_wsgi_application()
Edit /etc/apache2/sites-available/python.conf.
(Comment out WSGIScriptAlias / /home/hoge/hello.py
and
Uncomment WSGIScriptAlias / /home/hoge/TestProject/TestProject/wsgi.py
)
python.conf
LoadModule wsgi_module modules/mod_wsgi.so
# WSGIScriptAlias / /home/hoge/hello.py
WSGIScriptAlias / /home/hoge/TestProject/TestProject/wsgi.py
WSGIPythonPath /home/hoge/.pyenv/versions/3.5.1/lib/python3.5/site-packages
<Directory /home/hoge>
Require all granted
</Directory>
$ sudo service apache2 restart
Try accessing from a browser again Success when Django's "It worked!" Screen appears.
As you can see from the result of hello.py, python version 3.4.3 was used for some reason. If you install mod_wsgi with apt-get, you can't specify the version. I don't know how it works, but there may be some inconvenience due to the difference in version ...? (Knowledge around the version)
So I also tried installing mod_wsgi from source. I understand this quite well. I ended up installing python's 3.5-dev. I felt like I went through the error code and read stack overflow, and I didn't understand the meaning of it.
With the introduction finished, maybe ok with the following steps.
For the time being, delete the installed one. (If installed)
$ apt-get remove libapache2-mod-wsgi-py3
$ pyenv uninstall 3.5.1
Install apache2-dev
$ sudo apt-get install apache2-dev
When I try to install python3.5-dev
$ CONFIGURE_OPTS="--enable-shared" pyenv install 3.5-dev
Cloning https://hg.python.org/cpython...
error: please install `mercurial` and try again
So install mercurial
$ sudo apt-get install mercurial
$ CONFIGURE_OPTS="--enable-shared" pyenv install 3.5-dev
Install mod_wsgi
$ cd
$ wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.4.13.tar.gz
$ tar zxvf 4.4.13.tar.gz
$ mv mod_wsgi-4.4.13 /usr/local/src
$ cd /usr/local/src/mod_wsgi-4.4.13
$./configure LDFLAGS='-Wl,-rpath=/home/hoge/.pyenv/versions/3.5-dev/lib'
$ make
$ sudo make install
Apply module to apache
$ sudo a2enmod wsgi
Restart apache
$ sudo service apache2 restart
It was quite difficult and time-consuming to search for literature. I want to work with someone who is familiar with environment construction next to me ... (_ _) Please let me know if you are doing something strange.
Recommended Posts