2018年12月27日木曜日

DjangoのインストールとApache連携

【インストール無しの開発環境】
# cd ~/Make
# git clone https://github.com/okoppe8/instant-django.git
# cd instant-django
# python36 -m venv ~/.venv36
# source ~/.venv36/bin/activate
# cat requirements.txt
Django==2.1.2
django-crispy-forms==1.7.2
django-filter==2.0.0
pytz==2018.5
# pip3 install -r requirements.txt     # <-- Django環境のインストール
# python36 manage.py migrate
# python36 manage.py createsuperuser
# python36 manage.py runserver

URL=http://127.0.0.1:8000/

【Djangoのインストール】
# python36 -m pip install Django
# ls /usr/local/lib/python3.6/site-packages/django # 確認
$ django-admin.py startproject myapp               # プロジェクト作成

$ python36
Python 3.6.6 (default, Aug 13 2018, 18:24:23)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'2.1.2'
$ httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 5 2018 01:47:09

# pip3 install
# pip3 install pymysql

■仮想環境の構築
# mkdir /usr/local/venv36
# pip3 -m venv /usr/local/venv36
# source /usr/local/venv36/bin/activate      ※環境設定
# deactivate
                                ※環境を戻す
■Apache連携
VENV仮想環境を構築してから下記を行う。

【WSGIインストール】
# pip3 install mod-wsgi
# pip3 freeze|grep wsgi
mod-wsgi==4.6.5
mod-wsgi-httpd==2.4.35.1
# ls /usr/local/venv36/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
# vi /etc/httpd/conf.modules.d/20-wsgi.conf
LoadModule wsgi_module /usr/local/venv36/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
# httpd -M|grep wsgi
wsgi_module (shared)

# vi /etc/httpd/conf.d/wsgi.conf
WSGISocketPrefix /var/run/wsgi

WSGIScriptAlias  /cti /var/www/wsgi/cti/Project/wsgi.py
WSGIDaemonProcess cti user=apache group=apache processes=10 threads=15 \
       home=/var/www/wsgi/cti \
       python-home=/usr/local/venv36 \
       python-path=/var/www/wsgi/cti:/usr/local/venv36/lib64/python3.6/site-packages

WSGIScriptAlias  /analyze /var/www/wsgi/analyze/Project/wsgi.py
WSGIDaemonProcess analyze user=apache group=apache processes=10 threads=15 \
       home=/var/www/wsgi/analyze \
       python-home=/usr/local/venv36 \
       python-path=/var/www/wsgi/analyze:/usr/local/venv36/lib64/python3.6/site-packages

Alias /static/  /var/www/wsgi/analyze/static/

WSGIPassAuthorization on

<Location /cti>
       WSGIProcessGroup cti
</Location>
<Location /analyze>
       WSGIProcessGroup analyze
</Location>

# vi /etc/sysconfig/httpd
LD_LIBRARY_PATH=/opt/libs/oracle:$LD_LIBRARY_PATH

# apachectl configtest
Syntax OK
# curl localhost/myapp
# systemctl restart httpd

※wsgiからstaticが参照できない。
  回避????
# vi  /etc/httpd/conf.d/wsgi.conf
Alias /static/  /var/www/wsgi/Project/static/
# cd /var/www/wsgi/Project/static
# ln -s /usr/local/venvs/lib/python3.6/site-packages/django/contrib/admin/static/admin admin

  試してない
# vi Project/settings.py
STATICFILES_DIRS = [
    BASE_DIR,
]


※manage.pyに記述した内容をwsgi.pyにも記入。
import io
import pymysql
import cx_Oracle
os.environ["NLS_LANG"] = "JAPANESE_JAPAN.JA16SJISTILDE"
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

pymysql.install_as_MySQLdb()


参考:https://qiita.com/nachashin/items/d3f9cd637a9cecbda72c


【lddコマンドで確認】
$ ldd /usr/local/venv36/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
linux-vdso.so.1 =>  (0x00007ffd62fe1000)
libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 (0x00007fa46eebb000)
libpthread.so.0 => /usr/lib64/libpthread.so.0 (0x00007fa46ec9f000)
libc.so.6 => /usr/lib64/libc.so.6 (0x00007fa46e8d2000)
libdl.so.2 => /usr/lib64/libdl.so.2 (0x00007fa46e6ce000)
libutil.so.1 => /usr/lib64/libutil.so.1 (0x00007fa46e4cb000)
libm.so.6 => /usr/lib64/libm.so.6 (0x00007fa46e1c9000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa46f61f000)


# $ pip3 install mod-wsgi --proxy xxx.x.x.xxx:3128
Collecting mod-wsgi
  Using cached mod_wsgi-4.7.1.tar.gz (498 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/venv36-d22/bin/python36 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-j5eekw_5/mod-wsgi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-j5eekw_5/mod-wsgi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-j52nyjp6
         cwd: /tmp/pip-install-j5eekw_5/mod-wsgi/
    Complete output (5 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-j5eekw_5/mod-wsgi/setup.py", line 168, in <module>
        'missing Apache httpd server packages.' % APXS)
    RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in the documentation for this package and install any missing Apache httpd server packages.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

→これで解決。
# yum install httpd-devel
# source /usr/local/venv36-d22/bin/activate
# pip3 install mod-wsgi --proxy xxx.x.x.xxx:3128

※wsgiのインストールは、rootでないとPermission denied.






0 件のコメント:

コメントを投稿

シャットダウン時の後処理 (shutdown)

# vi /etc/systemd/system/drop.service [Unit] Description= stop httpgwd DefaultDependencies=no Before=shutdown.target RefuseManualStart=true ...