Here are some frequently used commands when developing with Python/Django.
I would like to take this opportunity to thank you for solving the problem by borrowing the wisdom of our predecessors, and I am very sorry to say that I will summarize it here as my own memo.
(Production environment)
(Development environment)
Build a WEB server, AP server, and DB server on Amazon EC2 (Elastic Computing Cloud): Amazon Linux 2.
(Diagram)
--Execute the following commands with the SSH connection to the EC2 instance.
Terminal
$ sudo yum update -y
The default is UTC (Coordinated Universal Time), so change it to Japan Standard Time (JST).
Terminal
$ sudo taimedatectl set-timezone Asia/Tokyo
$ date
Thursday, December 10, 2020 11:23:45 JST
The "date" command gets the current date and time. If it has been changed to Japan Standard Time (JST), "JST" will be displayed at the end.
Since the default is US specification, change it to Japanese character code (jz_JP.UTF-8).
Terminal
$ sudo localectl set-locale LANG=ja_JP.UTF-8
$ localectl status
System Locale: LANG=ja_JP.UTF-8
VC Keymap: n/a
X11 Layout: n/a
Since the default user (ec2-user) is a well-known user name, it is too high a security risk to continue using it as it is. Therefore, a new working user will be created with an arbitrary user name, and ec2-user will be deleted.
Terminal
$sudo useradd <arbitrary user name>
Terminal
$ sudo cp -arp /home/ec2-user/.ssh /home/<Arbitrary user name>
Terminal
$ sudo chown -R <Arbitrary user name>/home/<Arbitrary user name>/.ssh
Terminal
$ sudo visudo -f /etc/sudoers.d/90-cloud-init-users
/etc/sudoers.d/90-cloud-init-users
# User rules for ec2-user
# ec2-user ALL=(ALL) NOPASSWD:ALL comment out
<Arbitrary user name> ALL=(ALL) NOPASSWD:ALL added
Make an SSH connection with
Terminal
$ sudo su -
root #
If the display switches to "root #", it is successful. Once confirmed, type exit to log out of Root.
Terminal
root # exit
Log out
$
Also add the "-r" option to delete the ec2-user home directory.
Terminal
$ sudo userdel -r ec2-user
Terminal
$ sudo yum install python3
$ python3 --version
Python 3.7.9
Terminal (development environment)
(venv_<Project name>)$ pip freeze > requirements.txt
Terminal
$ python3 -m venv venv_<Project name>
Terminal
$ source venv_<Project name>/bin/activate
Terminal
$ pip install -r requirements.txt
This module is always taken care of when creating websites and web applications that use user authentication or set up inquiry forms.
Terminal
$ pip install boto django-ses
Terminal
$ pip install gunicorn
Terminal
$ amazon-linux-extras list | grep postgresql
5 postgresql9.6 available \
6 postgresql10 available [ =10 =stable ]
41 postgresql11=latest enabled [ =11 =stable ]
It turned out that ver.11 is the highest version that can be installed. Execute the following to install.
Terminal
$ sudo amazon-linux-extras install postgresql11
Check the version.
Terminal
$ psql --version
psql (Postgres) 11.5
Terminal
$ sudo yum install postgresql-server
Terminal
$ sudo postgresql-setup initdb
The following WARNING message may be displayed. This is because PostgreSQL automatically optimized the options, and there is no problem with the initialization process itself.
Terminal
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
Terminal
$ sudo systemctl enable postgresql
Terminal
$ sudo systemctl start postgresql
Terminal
$ sudo -u postgres -i psql
psql (11.5)
"help"Get help with.
postgres=#create user <arbitrary user name> with password'<Arbitrary password>';
CREATE ROLE
postgres=#create database <database name> owner <arbitrary user name>;
CREATE DATABASE
Terminal
postgres=# \q
Terminal
$ sudo yum install git
Terminal
$ cd ~/venv_<Project name>
Terminal
$ git clone https://<Bitbucket account>@bitbucket.org/<Bitbucket account>/<Bitbucket remote repository name>.git
Cloning into '<Remote repository name>'...
Password for 'https://<Bitbucket account>@bitbucket.org':
remote: Counting objects: 123, done.
remote: Compressing objects: 100% (123/123), done.
remote: Total 123 (delta 25), reused 0 (delta 0)
Unpacking objects: 100% (123/123), done.
Terminal
$ cd ~/venv_<Project name>/<Project name>
Terminal
$ git pull
Password for 'https://<Bitbucket account>@bitbucket.org':
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (123/123), done.
From https://bitbucket.org/<Bitbucket account>/<Bitbucket remote repository name>
29912a3..c5aac44 master -> origin/master
fatal: refusing to merge unrelated histories
Terminal
$ mkdir ~/venv_<Project name>/<Project name>/logs
[Python/Django] Summary of frequently used commands (1) <Create virtual environment, project, application>
[Python/Django] Summary of frequently used commands (2)
You can use EC2 as a development environment as well as during production operation, so I would like to consider using Docker as well.
Recommended Posts