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を呼び出して
同じ環境を量産できる。
秘伝の設定作業がなくなると思うとひとしお