pyenv virtualenv Django2.0 MariaDB Eclipse4.7 | Subaru's Husband

Subaru's Husband

python、linux、バイクなんかのネタを書きます。
一応security業界でご飯を食べてます。
備忘録がメインだと思うので、未来の自分が助かれば最高。

#========================
MacにDjangoの環境を構築
#========================

pyenvでpythonの各バージョン管理
virtualenvで個別の仮想環境に対応します。

・Django2.0
・Python3.5.2
・MariaDB
・Eclipse4.7

 ※前提としてhomebrewが使用できることです。

  https://brew.sh/index_ja.html

#========
1. MariaDBのインストール
#========

$brew install mariadb

#サービスの開始

$mysql.server start

#セキュリティの初期設定

$mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

#====
MariaDBにログインし初期設定
#====

$ mysql -uroot -p{パスワード}

MariaDB [(none)]> create database {データベース名} default charset utf8;
MariaDB [(none)]> grant all privileges on {データベース名}.* to {ユーザー名}@localhost identified by '{パスワード}';
MariaDB [(none)]> FLUSH PRIVILEGES;

#========================
2. pyenvのインストール(複数のPythonを共存する為です。)
#========================
$git clone https://github.com/yyuu/pyenv.git ~/.pyenv

#設定を.bashrcに追加

$nano .bashrc

#==== ここまで追加 ====

export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"

#==== ここまで追加 ====

#========================
3. pyenvでpython3.5.2をインストール
#========================

$ pyenv install 3.5.2
Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
Installing Python-3.5.2...
patching file Lib/venv/scripts/posix/activate.fish
Installed Python-3.5.2 to /Users/dummy/.pyenv/versions/3.5.2

# グローバルで使用するpythonを3.5.2に設定

$ pyenv global 3.5.2
$ python
Python 3.5.2 (default, Jan 30 2018, 12:09:38)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

#========================
4. virtualenvのインストールと仮想環境に入る方法
#========================
複数の環境を検証する為の仮想環境を構築する為

$ pip install virtualenv
Collecting virtualenv
Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

$ pip install --upgrade pip
Collecting pip
Using cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-9.0.1

#==== 任意の場所に仮想環境を作成 ====
ここではdjango2.0とpython3.5.2用を作成
#===============================
※dummyユーザーのDocuments/develop/virtual_conf/に作成します。

$ virtualenv django2.0_py3.5.2
Using base prefix '/Users/dummy/.pyenv/versions/3.5.2'
New python executable in /Users/dummy/Documents/develop/virtual_conf/django2.0_py3.5.2/bin/python3.5
Also creating executable in /Users/dummy/Documents/develop/virtual_conf/django2.0_py3.5.2/bin/python
Installing setuptools, pip, wheel...done.

#========
作成した仮想環境を有効にする
#========

$ cd django2.0_py3.5.2/
$ cd bin
$ source ./activate
(django2.0_py3.5.2) $

#===============================
Django2.0を仮想環境にインストール
#===============================

(django2.0_py3.5.2) $ pip install django==2.0

※自分はこれをインストール

$ pip install mod_wsgi
$ pip install django-debug-toolbar
$ pip install pytz
$ pip install python-dateutil
$ pip install mysqlclient

#========================
Eclipseのダウンロード
#========================
http://mergedoc.osdn.jp/

Eclipse4.7 Oxygen
Mac64bit版
Python

をダウンロード

#========================
Eclipseから新規 PyDev Djangoプロジェクト作成
#========================

1.メニュー -> 新規 -> その他 -> Pydev Djangoプロジェクト
2.プロジェクト名に{任意の名前}
3.文法バージョン 3.0 - 3.5
4.インタープリター
 「リストされていないインタープリターを構成するにはここをクリックしてください。」をクリック 
5.Python インタープリター -> 新規...
インタープリター名: django2.0_py3.5.2
インタープリター実行ファイル:/Users/dummy/Documents/develop/virtual_conf/django2.0_py3.5.2/bin/python3.5

 作成した仮想環境を選択したDjangoプロジェクトができる

 django2.0_py3.5.2を選択

6.次へ ※参照ページも次へ
7.Django Settings
 Django version:1.4 or later
 Database Engine MySQL

 ※他はあとから設定

 完了
#========================
作成プロジェクトにDB設定を行う
#========================
{プロジェクト名}-sesttings.pyのDATABASESを書き換える

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME' : '{データベース名}',
'HOST' : 'localhost',
'USER' : '{ユーザー名}',
'PASSWORD' : '{パスワード}',
'PORT' : '3306',
}
}

#========================
動作確認
#========================

プロジェクト(右クリック) - [実行] - [1: Pydev Django]

Performing system checks...

System check identified no issues (0 silenced).

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 30, 2018 - 04:54:47
Django version 2.0, using settings 'test2.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

http://127.0.0.1:8000/にアクセスすると

The install worked successfully! Congratulations!

と表示されればセットアップOK
後はアプリケーションを作ってMake Migrationsとかせんといかん。

#=======
eclipseでデバッグ時にWorningができる時の対応
#=======

$cd /Users/dummy/Documents/develop/virtual_conf/django2.0_py3.5.2/bin/
$./python3.5 /Applications/Eclipse_4.7.2.app/Contents/Eclipse/dropins/PyDev/eclipse/plugins/org.python.pydev_6.2.0.201711281614/pysrc/setup_cython.py build_ext --inplace