Help us understand the problem. What is going on with this article?

AWSにdjango開発環境構築メモ(VSCode Remote SSH用)

前提

この構築のゴールは、AWS環境ではsshのみ開けて、サーバサイドの開発、デバックをClientPCのVSCodeでできるようにすることです。

Client
  Windows10
サーバ 
 CentOS7(AWSで動作)

※手順全部書いたつもりです。参考になるところあったら使ってください。
※毎回これするのも大変なのでansible化するかdokerイメージにしたい。
※VSCodeでのdjangoデバック環境は次の機会に。

ssh 設定

.ssh/sshconfigにAWSで起動したCentOSののログイン情報を記載してSSHでパス無しでログインできるようにする。
LocalForwardの行は、Djangoのポートへの。PCでlocalhost:8000を開くとサーバ側に転送されて便利。

# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host myserver
    HostName xxx.xxx.xxx.xxx
    User centos
    IdentityFile ~user/.ssh/xxxx.pem
    LocalForward 8000 localhost:8000

VSCodeのRemote SSHで接続できる事を確認しておく
VSCodeにpythonのプラグインをインストールする

サーバ側の変更

CentOS7のGitをVersion2に変更する

$ sudo yum -y remove git
$ sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
$ sudo yum -y install git2u

venv環境を作ってdjangoをインストール。後で使う、mysqlclientもインストール

$ sudo pip3.6 install --upgrade pip
$ cd ~/envs/activate
$ python3 -m venv django
$ source ~/envs/django/bin/a
$ pip install django
$ sudo yum install python-devel mysql-community-devel
$ pip install mysqlclient

djangoのサンプルアプリ作成

$ cd ~/web/
$ django-admin startproject mysite

Cent7のSQliteのバージョンだとdjangoが動かない。
SQLiteは諦めMysqlをインストール

$ sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
$ yum info mysql-community-server
$ yum -y install mysql-community-server
$ sudo yum -y install mysql-community-server
$ sudo systemctl enable mysqld.service
$ sudo systemctl start mysqld.service
$ sudo systemctl status mysqld.service
$ sudo cat /var/log/mysqld.log|grep password

Mysqlのセキュリティ設定

[root@ip-172-31-33-248 ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 
Re-enter new password: 
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : No

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@ip-172-31-33-248 centos]# 

MysqlのユーザとDB作成

mysql> create database django01;
mysql> create user django@localhost identified by '************';
mysql> grant all on django01.* to django@localhost;

venvの適用方法(メモ)

[centos@ip-172-31-33-248 web]$ source venv/bin/activate
(venv) [centos@ip-172-31-33-248 web]$ python -V
Python 3.6.2
(venv) [centos@ip-172-31-33-248 web]$ pip list
Package    Version
---------- -------
Django     2.2.7  
pip        19.3.1 
pytz       2019.3 
setuptools 28.8.0 
sqlparse   0.3.0  

djangoのDBをMysqlに変更

$ git diff
diff --git a/mysite/settings.py b/mysite/settings.py
index 94fdb96..cf0a816 100644
--- a/mysite/settings.py
+++ b/mysite/settings.py
@@ -75,8 +75,15 @@ WSGI_APPLICATION = 'mysite.wsgi.application'

 DATABASES = {
     'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+        'ENGINE': 'django.db.backends.mysql',
+        'NAME': 'django01',
+        'USER': 'django',
+        'PASSWORD': '********',
+        'HOST': 'localhost',
+        'PORT': '3306',
+        'OPTIONS': {
+                'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
+                },
     }
 }
python manage.py migrate
python manage.py runserver

クライアントPCでhttp://localhost:8000を開くとdjangoサーバにつながります。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away