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.






2018年12月26日水曜日

systemctl restart httpd で「Error: No space left on device」

# systemctl restart httpd
Error: No space left on device
# echo 32768 > /proc/sys/fs/inotify/max_user_watches
で一時的に直る。
恒久的には、
# vi /etc/sysctl.conf
fs.inotify.max_user_watches = 32768 # 追加
# cat /proc/sys/fs/inotify/max_user_watches

※inotify-toolsでファイル、ディレクトリ操作のイベントを監視。
# yum install inotify-tools
# inotifywait ファイル名&      -----監視開始
# inotifywatch ファイル名&      -----統計情報がとれる。



2018年12月25日火曜日

cgiでpython3を動かす。

# vi /etc/httpd/conf/httpd.conf
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options ExecCGI
    Require all granted
    AddHandler cgi-script .py .pyc
</Directory>

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

# systemctl restart httpd

#vi /var/www/cgi-bin/oracle.py
#!/usr/bin/env python36import cx_Oracle
import osimport sys
import io
os.environ["NLS_LANG"] = "JAPANESE_JAPAN.JA16SJISTILDE"
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
print("Content-Type: text/plain;charset=utf-8")
print()

HOST = '192.168.xx.xxx'
PORT = '1521'
SID = 'xxxxxxxx'

tns = cx_Oracle.makedsn(HOST, PORT, SID)
conn = cx_Oracle.connect("xxxxxxx", "xxxxxxx", tns)

print( conn.version)
cur = conn.cursor()
cur.execute("""select column_name,data_type,data_length from all_tab_columns where table_name='CCMxxx'""")
rows = cur.fetchall()

for r in rows:
   print("%s %s %s" % (r[0],r[1],r[2]))

cur.execute("""select * from CCMxxx""")
rows = cur.fetchall()
for r in rows:
   print("%s %s %s %s" % (r[0],r[1],r[2],r[3]))

2018年12月6日木曜日

CenOS7 gradleインストール

Gradle v4.10.3 2018.12.5
ここからダウンロード
https://gradle.org/releases/

# mv gradle-4.10.3-all.zip /opt
# cd /opt
# unzip gradle-4.10.3-all.zip
# rm gradle-4.10.3-all.zip
# ln -s gradle-4.10.3 gradle
# vi /etc/profile
export GRADLE_HOME=/opt/gradle
export PATH=$GRADLE_HOME/bin:$PATH
$ source /etc/profile
$ gradle -version
Welcome to Gradle 4.10.3!
Here are the highlights of this release:
- Incremental Java compilation by default
- Periodic Gradle caches cleanup
- Gradle Kotlin DSL 1.0-RC6
- Nested included builds
- SNAPSHOT plugin versions in the `plugins {}` block
For more details see https://docs.gradle.org/4.10.3/release-notes.html
------------------------------------------------------------
Gradle 4.10.3
------------------------------------------------------------
Build time: 2018-12-05 00:50:54 UTC
Revision: e76905e3a1034e6f724566aeb985621347ff43bc
Kotlin DSL: 1.0-rc-6
Kotlin: 1.2.61
Groovy: 2.4.15
Ant: Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM: 1.8.0_191 (Oracle Corporation 25.191-b12)
OS: Linux 3.10.0-957.1.3.el7.x86_64 amd64


■使ってみる
$ cd Make/sample
$ mkdir src
$ mkdir src/main
$ mkdir src/main/java
$ mkdir src/main/java/sample
$ mkdir src/main/jdava/sample/gradle
$ vi src/main/sample/gradle/GradleMain.javapackage sample.gradle;
public class GradleMain {
public static void main(String[] args) {
System.out.println("Hello Gradle!!");
}
}
$ vi build.gradle
apply plugin: 'java'
$ gradle compileJava
$ java -cp build/classes/java/main sample.gradle.GradleMain
Hello Gradle!!
$ find * -print
build
build/classes
build/classes/java
build/classes/java/main
build/classes/java/main/sample
build/classes/java/main/sample/gradle
build/classes/java/main/sample/gradle/GradleMain.class
build/tmp
build/tmp/compileJava
build.gradle
src
src/main
src/main/java
src/main/java/sample
src/main/java/sample/gradle
src/main/java/sample/gradle/GradleMain.java
$ gradle clean
$ find * -print
build.gradle
src
src/main
src/main/java
src/main/java/sample
src/main/java/sample/gradle
src/main/java/sample/gradle/GradleMain.java
$

■使ってみる(java project)
$ gradle init --type java-library
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
$ find . -print
.
./src
./src/test
./src/test/java
./src/test/java/LibraryTest.java
./src/main
./src/main/java
./src/main/java/Library.java
./.gradle
./.gradle/buildOutputCleanup
./.gradle/buildOutputCleanup/cache.properties
./.gradle/buildOutputCleanup/buildOutputCleanup.lock
./.gradle/buildOutputCleanup/outputFiles.bin
./.gradle/4.10.3
./.gradle/4.10.3/fileChanges
./.gradle/4.10.3/fileChanges/last-build.bin
./.gradle/4.10.3/fileHashes
./.gradle/4.10.3/fileHashes/fileHashes.lock
./.gradle/4.10.3/fileHashes/fileHashes.bin
./.gradle/4.10.3/taskHistory
./.gradle/4.10.3/taskHistory/taskHistory.bin
./.gradle/4.10.3/taskHistory/taskHistory.lock
./.gradle/4.10.3/gc.properties
./settings.gradle
./build.gradle
./gradlew
./gradlew.bat
./gradle
./gradle/wrapper
./gradle/wrapper/gradle-wrapper.properties
./gradle/wrapper/gradle-wrapper.jar

$ vi build.gradle <<__EOF__
O
##########以下を修正############
id 'java-library'の下に
id 'eclipse'を追加

id 'eclipse'
################################
←ESCコード(cntl+v esc)
ZZ
__EOF__
$ vi build.gradle
$ gradle eclipse
$ ls -la
合計 56
drwxrwxr-x. 6 takahab takahab 4096 12月 10 14:14 .
drwxrwxr-x. 3 takahab takahab 4096 12月 10 13:52 ..
-rw-rw-r--. 1 takahab takahab 4308 12月 10 14:05 .classpath
drwxrwxr-x. 5 takahab takahab 4096 12月 10 14:04 .gradle
-rw-rw-r--. 1 takahab takahab  391 12月 10 14:05 .project
drwxrwxr-x. 2 takahab takahab 4096 12月 10 14:05 .settings
-rw-rw-r--. 1 takahab takahab 1051 12月 10 14:05 build.gradle
drwxrwxr-x. 3 takahab takahab 4096 12月 10 13:53 gradle
-rwxrwxr-x. 1 takahab takahab 5296 12月 10 13:53 gradlew
-rw-rw-r--. 1 takahab takahab 2260 12月 10 13:53 gradlew.bat
-rw-rw-r--. 1 takahab takahab  367 12月 10 13:53 settings.gradle
drwxrwxr-x. 4 takahab takahab 4096 12月 10 13:53 src

eclipseを起動してインポート




CentOS7 mavenインストール

■mavenインストール
CentOS Linux release 7.6.1810
maven-3.6.0
openjdk version "1.8.0_191"

# cd /var/tmp
# curl -OL https://archive.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
# tar -xzvf apache-maven-3.6.0-bin.tar.gz
# mv apache-maven-3.6.0 /opt/
# rm /var/tmp/apache-maven-3.6.0-bin.tar.gz
# cd /opt
# ln -s /opt/apache-maven-3.6.0 apache-maven
# vi /etc/profile
export PATH=/opt/apache-maven/bin:$PATH
# source /etc/profile
# mvn -v
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T03:41:47+09:00)
Maven home: /opt/apache-maven
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.1.3.el7.x86_64", arch: "amd64", family: "unix"

■JDKインストール
# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
# dirname $(readlink $(readlink $(which java)))
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin
# vi / etc / profile
export JAVA_HOME=/usr/lib/jvm/java
export PATH=$PATH:$JAVA_HOME/bin
# source /etc/profile

■mavenプロジェクト作成と実行
$ mkdir test
$ cd test
$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DgroupId=com.sample -DartifactId=hello
$ find . -print
.
./hello
./hello/pom.xml
./hello/src
./hello/src/main
./hello/src/main/java
./hello/src/main/java/com
./hello/src/main/java/com/sample
./hello/src/main/java/com/sample/App.java
./hello/src/test
./hello/src/test/java
./hello/src/test/java/com
./hello/src/test/java/com/sample
./hello/src/test/java/com/sample/AppTest.java
$ cd hello
$ mvn compile
$ java -cp target/classes/ com.sample.App
$ mvn test
$ mvn clean













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

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