Ubuntu18.04にPython(Djnago)環境の構築(備忘録

やりたいこと。

  • 最新版のPythonをインストール
  • pipenvでもってdjangoの環境構築

 

 

ここを参照

solarianprogrammer.com

Ubuntu18.04ではこっちのほうが良かった

blog.softhints.com

 

 

念の為これも実行した

(何かの開発用ライブラリらしい、くらいしか良くわかってない)

sudo apt-get install libffi-dev

sudo apt-get install zlib1g-dev

 

 

上記のリンク先にあったが、

リポジトリ追加するのがお手軽そう。

"Python3.7"としてインストールされる

Install latest python 3.7 on Ubuntu 18/Linux Mint 18

There are several ways of installing the latest python on Ubuntu:

Install by using PPA repository

There is a PPA which offers many versions of Python
New Python Versions

This is the description of the repo:

This PPA contains more recent Python versions packaged for Ubuntu.

Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.

If you want to install 3.7 in this way you can do it by:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7

After the installation is better to not change the default version for the OS because you can meet many problems and unexpected behavior. Better is to change the version of your IDE or specific programs.

Install by downloading and building from source

First you will need to install some dependencies for the new python version:

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

After that you need to download the version which you want to install. In this case we download Python 3.7 source code from python.org. You can also download the file by visiting the site: python.org donwloads

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

Now extract the downloaded package(add sudo if needed).

tar xvf Python-3.7.0.tar.xz

Now enter the newly extracted source directory, configure the build environment and install.

cd Python-3.7.0
sudo ./configure
sudo make altinstall

Note : If you use make altinstall instead of make install you will keep the default python version (otherwise you can face problems).

 

 

 

 

こちらは3.7で微妙にうまく行かなかった

Altinstallではない点も違う。

 

This is a short article about building Python 3.6 from source on Ubuntu 16.04 or Debian 9 Linux. At the time of this writing both Ubuntu LTS and Debian stable come with Python 3.5. Python 3.6 comes with a production ready asyncio module, new formatted string literals and many more improvements vs the old 3.5 version. You can read more about what’s new in Python 3.6 here. The procedure described in this tutorial also works with Windows Subsystem for Linux, WSL, aka Bash on Ubuntu on Windows.

As a side note, you should be able to use the same procedure on other Debian based systems, like Raspbian on Raspberry Pi, just replace apt with apt-get and you are good to go.

First, make sure your system is fully updated:

1 sudo apt update
2 sudo apt upgrade

Next, install the default GCC toolchain with:

1 sudo apt install build-essential

Next, we need to install a few prerequisites for building Python:

1 sudo apt install libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev 
2 sudo apt install libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev tk-dev

At the time of this writing, the latest stable version of Python is 3.6.1, if you want to use a newer version change the next instructions accordingly:

1 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
2 tar xf Python-3.6.1.tar.xz
3 cd Python-3.6.1
4 ./configure --enable-optimizations
5 make -j 8
6 sudo make altinstall

Please note the use of sudo make altinstall instead of the typical Linux/Unix sudo make install. Using altinstall will ensure that you don’t mess with the default system Python.

Once the above is finished, you could invoke the new Python interpreter with:

1 python3.6

python3 will invoke the system Python version, 3.5.

Time to write a small test program:

1 import sys
2 
3 name = "Solarian Programmer"
4 print(f"Hello {name}, from Python version {sys.version}")

This is what I see if I run the above code on my Debian machine:

1 ~$ python3.6 test.py
2 Hello Solarian Programmer, from Python version 3.6.1 (default, Jun 30 2017, 11:52:56)
3 [GCC 6.3.0 20170516]
4 ~$

 

 

 

pipのインストール方法

curl -kL https://bootstrap.pypa.io/get-pip.py | sudo python3.7

 

下記を参考にした

qiita.com

 

 

その後 pipenvのインストール

python3.7 -m pip install pipenv

 

Django用のフォルダ作成

mkdir ~/django

cd

cd django

 

 

djangoインストール

pipenv install django

バージョン指定する場合

pipenv install django==2.1.1

 

 

仮想環境に入る

pipenv shell

 

 

プロジェクト作成

django-admin startproject test_project .

コマンドの最後にピリオドを付けると、

test_projectというフォルダを生成しないで、

カレントディレクトリに直接ファイルを置いてくれる。

 

 

サーバ起動

python manage.py runserver $IP:$PORT

サーバ起動が非常に大変でセキュリティグループの設定が必要

EC2のダッシュボードから

セキュリティグループのところに行き、

新しいグループを作成(インバウンドtcp 8000 127.0.0.0/24を登録)

  適当に8888とか8080とかも入れておくと便利かも

 

インスタンスの画面でインスタンスを右クリック、

「ネットワーキング」のところから作成したセキュリティグループを追加。

 

 

--------------------------------

Djnago側の設定

その後、

下記のファイルに
{project_name}/settings.py

下記のように記述
ALLOWED_HOSTS = ['.amazonaws.com']

※この書き方だと色々と安全性に問題あるかもしれない。

 とりあえずFQDNやipではなく、部分一致でも問題ないということ

 

仮想環境から抜けるのは

exit

 

 

おわり

---------------------------------------

納得の行く環境を作ったら

AMI(アマゾンマシンイメージ)に登録すると超便利。

 

あとでAMIを呼び出して

同じ環境を量産できる。

秘伝の設定作業がなくなると思うとひとしお

 

Ubuntu18.04インスタンスにCloud9 を導入する(備忘録

基本的な手順は下記参照。

qiita.com

 

PuTTYで接続する際のSSHキーの作成方法。

docs.aws.amazon.com

 

  1. AWSで「新しいキーペアを作成」を選ぶ。
  2. pemファイルがダウンロードされる。
  3. pemファイルのままだとPuTTYで使えないので
  4. PuTTYgenでppkファイルに変換する。
  5. 変換したファイルを使ってSSH接続

 

 

PuTTYでpem > pkk 変換の流れ

To convert your private key

  1. Start PuTTYgen (for example, from the Start menu, choose All Programs > PuTTY > PuTTYgen).

  2. Under Type of key to generate, choose RSA.

    
							RSA key in PuTTYgen

    If you're using an older version of PuTTYgen, choose SSH-2 RSA.

  3. Choose Load. By default, PuTTYgen displays only files with the extension .ppk. To locate your .pem file, select the option to display files of all types.

    
							Select all file types
  4. Select your .pem file for the key pair that you specified when you launched your instance, and then choose Open. Choose OK to dismiss the confirmation dialog box.

  5. Choose Save private key to save the key in the format that PuTTY can use. PuTTYgen displays a warning about saving the key without a passphrase. Choose Yes.

    Note

    A passphrase on a private key is an extra layer of protection, so even if your private key is discovered, it can't be used without the passphrase. The downside to using a passphrase is that it makes automation harder because human intervention is needed to log on to an instance, or copy files to an instance.

  6. Specify the same name for the key that you used for the key pair (for example, my-key-pair). PuTTY automatically adds the .ppk file extension.

Your private key is now in the correct format for use with PuTTY. You can now connect to your instance using PuTTY's SSH client.

 

 

PuTTYAWSに接続する流れ

  • PuTTYを開く
  • ConnectionからSSHを開いて、Authを選ぶ。
  • Authの中にBrowseというボタンがあるので先程作ったキー(.ppk)を選ぶ。

 

In the Category pane, expand Connection, expand SSH, and then choose Auth. Complete the following:

  1. Choose Browse.

  2. Select the .ppk file that you generated for your key pair, and then choose Open.

  3. (Optional) If you plan to start this session again later, you can save the session information for future use. Choose Session in the Category tree, enter a name for the session in Saved Sessions, and then choose Save.

  4. Choose Open to start the PuTTY session.

 

 

PuTTYのSessionの設定

ホスト名:パブリックDNS

※サービス > EC2 > インスタンスで調べる

 

ユーザー名:

今回はUbuntuのAMI(Amazon マシンイメージ)を使ったので"ubuntu"

 

 

Host Nameに

user_name@public_dns_name

という書き方で書くといちいちユーザ名の認証がなくて楽。

 

In the Host Name box, enter user_name@public_dns_name. Be sure to specify the appropriate user name for your AMI. For example:

  • For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.

  • For a Centos AMI, the user name is centos.

  • For a Debian AMI, the user name is admin or root.

  • For a Fedora AMI, the user name is ec2-user or fedora.

  • For a RHEL AMI, the user name is ec2-user or root.

  • For a SUSE AMI, the user name is ec2-user or root.

  • For an Ubuntu AMI, the user name is ubuntu.

  • Otherwise, if ec2-user and root don't work, check with the AMI provider.

 

ここまででとりあえずログインできた。

 

 

 

一番上に示したQiitaの手順に従って、

sudo apt update
sudo apt upgrade
sudo apt install -y nodejs

 

 

自分の場合、nodejsの他に、

Python2.7が入ってないというエラーが出たのでPython2.7を導入。

(Python2.7が入ってないパターン初めて見た。Python3は入っていた。)

 

>|sh|

$ sudo apt install python

 

$ python --version
Python 2.7.15rc1

||<

 

そしたら、

Cloud9で

Create Environment > Connect and run in remote server (SSH)

を選択してpublic SSH keyをコピーする。

 

 

SSH keyをコピーしたらPuTTY側で、

~/.ssh/authorized_keysにCloud9用のキーを追記。

 

>|sh|

echo [public SSH key貼り付け]  >> ~/.ssh/authorized_keys

||<

 

 

もともと記述されていたAWSSSHキーと

Cloud9のSSHキーの両方があることを確認する

>|sh|

cat ~/.ssh/authorized_keys

||<

 

問題なければ、

あとはCloud9を適当に進めてインストールは完了。