I set up Django from scratch (Vagrant, Centos, Python3)

Introduction

The other day, I tried using Anaconda in a local environment, but I thought, "Is it possible to display this in a browser ...?", So I decided to create an environment where Django can be used.

This time, I went from launching Vagrant's box to displaying the Django virtual server screen on the browser.

Eventually, Fill out the form in the browser → Machine learning model is calculated on the server side based on the sent data → Return the result to the browser I would like to make an app like that.

Execution environment

Host OS: Windows10 Guest OS: Centos7 (Vagrant)

Make a Vagrant Box

This is based on Hello World in Vagrant + Python3 + Django environment. Work at the command prompt.

vagrant box add centos/7
vagrant init centos/7

Add the following two lines to the generated Vagrantfile.

config.vm.synced_folder "./", "/home/vagrant/work"
config.vm.network "forwarded_port", guest: 8000, host: 18000

Go back to the command prompt and start vagrant.

vagrant up
vagrant ssh

Install Python3

Only python2 series is included by default, so I want to include python3. Considering the future, I thought it would be a good idea to use version control software, so I decided to use pyenv. It seems to be deprecated ... It is based on Installing Python3 using pyenv.

Prepare everything you need. From here, I'm working on Teraterm. vagrant ssh may be fine.

$ sudo yum groupinstall "Development tools"
$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel git

Install pyenv

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source .bash_profile

Update pyenv to the latest version

$ pyenv --version
pyenv 1.2.3-43-g35f7ef8
$ pyenv install --list #A lot of python that can be installed comes out
$ pyenv install 3.8.3 #Make sure you have the version you want to use and install
$ pyenv global 3.8.3

Check the version of pyenv, python

$ pyenv versions
  system
* 3.8.3 (set by /home/vagrant/.pyenv/version)
$ python --version
Python 3.8.3

Install Django

Delete old Django (hopefully you can confirm it doesn't exist)

$ python -c "import django; print(django.__path__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named django

Install virtualenv, virtualenvwrapper, django

$ pip install virtualenv
$ pip install virtualenvwrapper
$ pip install Django

Django version check

$ python -m django --version
3.0.6

SQLite3 version upgrade

SQLite3, which is included by default in Vagrant, seems to be an old version, and if you try to start the server as it is, an error will occur, so install a new one. Introduction to Django (Tutorial)-Introduction to Tohoho's WWW was helpful.

$ yum install -y wget gcc make
$ wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
$ tar zxvf ./sqlite-autoconf-3290000.tar.gz
$ cd ./sqlite-autoconf-3290000
$ ./configure --prefix=/usr/local
$ make
$ make install
$ cd ..
$ rm -rf ./sqlite-autoconf-3290000 ./sqlite-autoconf-3290000.tar.gz
$ mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
$ ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
$ echo 'export LD_LIBRARY_PATH="/usr/local/lib"' >> ~/.bashrc
$ source ~/.bashrc

Connect to Django's virtual server

Create a project under / home / vagrant / work. Since it was set with Vagrantfile, the files and apps created here will be shared locally and in the virtual environment.

config.vm.synced_folder "./", "/home/vagrant/work"

Django project creation

$ cd /home/vagrant/work
$ django-admin startproject mysite #mysite is your favorite project name
$ cd mysite #Move into project
$ python manage.py runserver 0:8000 #Server startup

Then type http://127.0.0.1:8000 into your browser to open the page!

django.jpg

in conclusion

Initially, the server was running, but the browser couldn't display anything, which was a lot of work. I'm glad I managed to solve it on my own. However, I feel like I've done it, but this is just the preparation stage. There seems to be a long way to go until the application is complete.

reference

From Vagrant to Django installation in general: Hello World in Vagrant + Python3 + Django environment

SQLite3 version upgrade: Introduction to Django (Tutorial)-Introduction to Tohoho's WWW

Installation and management of python: Installing Python3 using pyenv

Recommended Posts

I set up Django from scratch (Vagrant, Centos, Python3)
Set up Python environment on CentOS
Django memo # 1 from scratch
[Vagrant] Set up a simple API server with python
Set up Python 3.4 on Ubuntu
Django starting from scratch (part: 2)
Django starting from scratch (part: 1)
Set Up for Mac (Python)
Install Python 3.7 and Django 3.0 (CentOS)
Switch from python2.7 to python3.6 (centos7)
Set up a web server with CentOS7 + Anaconda + Django + Apache
Set up social login with Django
I want to use jar from python
Business efficiency starting from scratch with Python
I have a question! (Python, django) Easy
Connecting from python to MySQL on CentOS 6.4
I tried using UnityCloudBuild API from Python
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
What I got from Python Boot Camp
Use Django from a local Python script
[Python] [Natural language processing] I tried Deep Learning ❷ made from scratch in Japanese ①
[Deep Learning from scratch] Speeding up neural networks I explained back propagation processing
[Python] I tried the same calculation as LSTM predict with from scratch [Keras]
Set up a simple HTTPS server in Python 3
Image processing from scratch with python (5) Fourier transform
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I tried Python! ] I graduated today from "What is Python! Python!"!
Set up a test SMTP server in Python.
I tried debugging from Python via System Console
Image processing from scratch with python (4) Contour extraction
PHP and Python integration from scratch on Laravel
Set up python Tornado environment on raspbian jessie
I want to use ceres solver from python
Run a Python file from html using Django
Set up a simple SMTP server in Python
Set up a Python development environment on Marvericks
Python vs Ruby "Deep Learning from scratch" Summary
What I did when updating from Python 2.6 to 2.7
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!