Ubuntu18.04にPython(Djnago)環境の構築(備忘録
やりたいこと。
ここを参照
Ubuntu18.04ではこっちのほうが良かった
念の為これも実行した
(何かの開発用ライブラリらしい、くらいしか良くわかってない)
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 VersionsThis is the description of the repo:
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:
Next, install the default GCC toolchain with:
Next, we need to install a few prerequisites for building Python:
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:
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:
python3 will invoke the system Python version, 3.5.
Time to write a small test program:
This is what I see if I run the above code on my Debian machine:
pipのインストール方法
curl -kL https://bootstrap.pypa.io/get-pip.py | sudo python3.7
下記を参考にした
その後 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 を導入する(備忘録
基本的な手順は下記参照。
- AWSで「新しいキーペアを作成」を選ぶ。
- pemファイルがダウンロードされる。
- pemファイルのままだとPuTTYで使えないので
- PuTTYgenでppkファイルに変換する。
- 変換したファイルを使ってSSH接続
To convert your private key
Start PuTTYgen (for example, from the Start menu, choose All Programs > PuTTY > PuTTYgen).
Under Type of key to generate, choose RSA.
If you're using an older version of PuTTYgen, choose SSH-2 RSA.
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 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.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.
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.
In the Category pane, expand Connection, expand SSH, and then choose Auth. Complete the following:
Choose Browse.
Select the
.ppk
file that you generated for your key pair, and then choose Open.(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.
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
orroot
.For a Fedora AMI, the user name is
ec2-user
orfedora
.For a RHEL AMI, the user name is
ec2-user
orroot
.For a SUSE AMI, the user name is
ec2-user
orroot
.For an Ubuntu AMI, the user name is
ubuntu
.Otherwise, if
ec2-user
androot
don't work, check with the AMI provider.
ここまででとりあえずログインできた。
一番上に示したQiitaの手順に従って、
sudo apt update sudo apt upgradesudo 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/authorized_keysにCloud9用のキーを追記。
>|sh|
echo [public SSH key貼り付け] >> ~/.ssh/authorized_keys
||<
Cloud9のSSHキーの両方があることを確認する
>|sh|
cat ~/.ssh/authorized_keys
||<
問題なければ、
あとはCloud9を適当に進めてインストールは完了。