Run python3 Django1.9 with mod_wsgi (deploy)

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.

environment

Environment

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))

First from Hello World

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)

Run Django

$ 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.

image

Another way

Install mod_wsgi from source

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

Summary

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

Run python3 Django1.9 with mod_wsgi (deploy)
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Django 1.11 started with Python3.6
Run Python with VBA
Run prepDE.py with python3
Run Blender with python
Run iperf with python
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Python compilation with pyqt deploy
Getting Started with Python Django (3)
Run Label with tkinter [Python]
Getting Started with Python Django (6)
Run DHT22 with RasPi + Python
Getting Started with Python Django (5)
Run Rotrics DexArm with python API
Run mruby with Python or Blender
Deploy a Django application with Docker
Run XGBoost with Cloud Dataflow (Python)
Run Aprili from Python with Orange
Until you run python with apache
Run servo with Python on ESP32 (Windows)
Deploy flask app with mod_wsgi (using pipenv)
Django + Apache with mod_wsgi on Windows Server 2016
Run a Python web application with Docker
FizzBuzz with Python3
Scraping with Python
Django Heroku Deploy 1
Statistics with python
Python Django Tutorial (5)
Python Django Tutorial (2)
Scraping with Python
Python with Go
Internationalization with django
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Django Heroku Deploy 2
CRUD with Django
Bingo with python
Zundokokiyoshi with python
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Excel with Python
Microcomputer with Python
Python Django Tutorial (4)
Cast with python
Project cannot be created with Python3.5 (Windows) + django1.7.1