qshinoの日記

Powershell関係と徒然なこと

python webapp django with mysql

Django

必要な物

  1. centos
  2. python3
  3. apache
  4. Django
  5. python mysql
  6. mod_wsgi
  7. MySql

setupするもの

  1. Django
  2. mysql db作成他
  3. apache config

導入編

centos,python3,apache, mysqlは導入済みとする。残りは、

  1. Django
  2. python-mysql
  3. mod_wsgi

Django

sudo pip3 install django 

python-mysql

sudo pip3 install PyMySQL

mod_wsgi

sudo pip3 install mod_wsgi

or

# for apxs
sudo yum install httpd-devel 

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.5.tar.gz
tar xvfz 4.5.5.tar.gz
cd mod_wsgi-4.5.5 

# Install

./configure --with-apxs=/usr/sbin/apxs --with-python=/usr/local/bin/python3

sudo make install | tee make.log

setup編

セットアップするもの

  1. mysqldb
  2. Django
  3. apache config

基本設定

mysqldb

mysql_install_db --datadir=/var/lib/mysql --user=mysql 
service mysqld start
mysql_secure_installation

# Django用DB作成

mysql 
mysql> CREATE DATABASE django DEFAULT CHARACTER SET utf8;
CREATE USER 'djuser'@'%' IDENTIFIED BY 'djpw';
GRANT ALL PRIVILEGES ON django.* TO 'djuser'@'localhost';

Django

dwk=/opt/django

mkdir $django
cd $django

django-admin startproject project

project/project/settings.py

DATABASES = { 'default': 
  { 'ENGINE': 'django.db.backends.mysql',   
    'NAME': 'django', 
    'USER': 'djuser', 
    'PASSWORD': 'djpw', 
    'HOST': 'localhost', 
    'PORT': '3306', } }

project/manage.py

import pymysql

 pymysql.install_as_MySQLdb()
python3 project/manage.py migrate 
python3 project/manage.py createsuperuser

project/wsgi.py

import sys import pymysql

pymysql.install_as_MySQLdb()

sys.path.append('/opt/django/project')

apache config

/etc/httpd/conf/httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so 

WSGIScriptAlias /test /opt/django/project/project/wsgi.py 

<Directory /opt/django/project/project> 
  <Files wsgi.py> 
   Order deny,allow 
   Allow from all 
  </Files> 
</Directory>

hello world

後で

test

curl http://localhost/test

参考

https://kimagureneet.hatenablog.com/entry/2016/08/24/134344

https://dev.mysql.com/doc/refman/5.6/en/mysql-install-db.html

heroku and django https://qiita.com/hibit/items/d9b2ed158ac3b0ff7b64