GCE+CentOS7+Nginx+uWSGI+Django備忘録

history貼っただけ

GCEインスタンス詳細画面からのSSH接続

```shell
sudo passwd root
sudo adduser xxxx
sudo passwd xxxx
sudo gpasswd -a xxxx wheel
su xxxx
```
HTTP, SSH, 開発用ポート開放
公開鍵の登録(https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys?authuser=2&hl=ja)

```shell:con.sh
ssh -i ~/path/to/key -p 12345 xxxx@your_external_ip
```
後述するsshdの変更後`sh con.sh`で接続可能に(追記: .ssh/configまわりの設定を使ったほうが楽そうです)

```shell
    1  cd ../xxxx
    6  sudo cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.org #バックアップ作成
    7  sudo vi /etc/ssh/sshd_config
#Port 22 → Port 12345
rootログインとパスワードログインの禁止は元から設定されていた
    8  systemctl restart sshd
   11  sudo yum update
   13  sudo yum install https://centos7.iuscommunity.org/ius-release.rpm
   15  sudo yum list|grep python3
   16  sudo yum install python36u python36u-pip
   17  python3.6 --version
   18  sudo pip3.6 install Django
   21  sudo pip3.6 install --upgrade pip
   22  pip3.6 --version
   23  sudo yum list|grep nginx
   24  cat /etc/redhat-release
   26  sudo yum install nginx
   27  sudo pip3.6 install uwsgi
Exception: you need a C compiler to build uWSGI
   30  which gcc #gcc未インストール
   32  sudo yum install gcc
   33  sudo pip3.6 install uwsgi #成功
   34  nginx -v
   35  uwsgi --version
   37  mkdir django
   38  cd django/
   39  sudo django-admin startproject twi
   43  sudo yum install tree #tree便利
   44  tree
   45  cd twi
   47  sudo vim twi/settings.py #allowed_host変更
   48  sudo python3.6 manage.py runserver 0.0.0.0:8000
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
   49  yum list|grep mariadb
   52  sudo vim twi/settings.py
   60  sudo python3.6 manage.py startproject myapp
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
   61  sudo yum install mariadb
   62  mysql -u root -p
   64  sudo yum install mariadb-server
   66  sudo rpm -qa | grep maria
   67  sudo vim /etc/my.cnf
   68  systemctl enable mariadb.service
   69  systemctl start mariadb.service
   70  mysql -u root -p
dbに権限を持つユーザーmxxxxの作成 rootパス変更
   75  sudo vim twi/settings.py
DATABASE設定(https://qiita.com/tfrcm/items/27d2c9e4b3334447b6af)
   77  sudo pip3.6 install PyMySQL
   79  sudo vim manage.py
(同上)
   80  sudo python3.6 manage.py runserver
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
https://yukun.info/django-mysqlclient-error/
を参考に以下実行
   83  sudo pip3.6 uninstall pymysql
   84  sudo pip3.6 install mysqlclient
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
https://qiita.com/narupo/items/6892a3a076e86a6a3983 を参考にmysql-community-develの代わりにmariadb-develインストール
   85  sudo yum install mariadb-devel
   86  sudo pip3.6 install mysqlclient #成功
   89  sudo vim manage.py
pymysqlの設定を取り除く
   91  sudo python3.6 manage.py migrate
MySQLdb._exceptions.OperationalError: (1049, "Unknown database 'django'") db作るの忘れてた…
   94  sudo systemctl status mariadb
   96  mysql -u root -p
   97  sudo python3.6 manage.py migrate #成功
   98  sudo python3.6 manage.py runserver
ローカルからのcurlに失敗する
  102  sudo python3.6 manage.py runserver 0.0.0.0:8000
https://qiita.com/melos/items/91dc3b45a589ea5a369b 以下とても参考にした
  103  uwsgi --http :8000 --module twi.wsgi #成功
  108  sudo vim uwsgi_params
  109  sudo systemctl start nginx
  110  sudo vim twi_nginx.conf
  112  sudo ls -la /etc/nginx
  115  sudo vim twi/settings.py #static設定
  116  sudo python3.6 manage.py myapp
  117  sudo python3.6 manage.py startapp myapp
  118  tree
  119  sudo vim twi/settings.py
  121  sudo vim myapp/views.py
  122  sudo vim twi/urls.py
  125  sudo python3.6 manage.py migrate
  126  sudo python3.6 manage.py runserver 0.0.0.0:8000 #成功
  127  sudo uwsgi --http :8000 --module twi.wsgi #成功
  131  sudo mkdir /etc/nginx/sites-enabled/
  137  sudo ln -s /home/xxxx/django/twi/twi_nginx.conf /etc/nginx/sites-enabled/
  138  sudo ls -l /etc/nginx/sites-enabled/
  141  sudo vim /etc/nginx/nginx.conf
/etc/nginx/sites-enabled/* を読み込むよう変更
  142  sudo tree /etc/nginx/
  145  sudo vim twi/settings.py
  146  sudo python3.6 manage.py collectstatic
  147  tree #集まっていることを確認
  150  sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
  151  sudo systemctl status nginx
nginx: [emerg] open() "/etc/nginx/sites-enabled/twi_nginx.conf" failed (13: Permission denied) in /etc/nginx/nginx.conf:37
  152  sudo ls -l /etc/nginx/               #
  153  sudo ls -l /etc/nginx/sites-enabled/ #
  154  sudo ls -l twi_nginx.conf            #権限確認、問題なし
  155  getenforce
  157  sudo setenforce 0
SELinux無効化 （http://dqn.sakusakutto.jp/2016/04/nginx_open_failed_permission_denied.html）
  158  getenforce
  159  sudo systemctl restart nginx #成功
  160  uwsgi --socket :8001 --module twi.wsgi
  161  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
adminのcssが読み込まれない
80番でアクセスするもnginxのnot found（こちらはtwi_nginx.confのlistenを8000から80に変更することで解決）
  162  sudo less /var/log/nginx/access.log
ログ見るも有用そうな情報なし　以下試行錯誤
参考:
http://djandjan.hateblo.jp/entry/2018/03/08/225014
https://stackoverflow.com/questions/33168308/getting-404-for-all-static-files-during-wsgi-setup-with-django
https://docs.djangoproject.com/en/2.2/howto/static-files/
https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve
https://docs.djangoproject.com/en/2.2/howto/static-files/deployment/
https://docs.djangoproject.com/ja/2.2/howto/deployment/wsgi/
https://docs.djangoproject.com/ja/2.2/howto/deployment/wsgi/uwsgi/
https://teratail.com/questions/124738
他多数
  163  sudo vim twi_nginx.conf
  164  sudo systemctl restart nginx
  165  sudo uwsgi --socket: 8001 --module twi.wsgi
  167  sudo vim twi/settings.py
  168  sudo python3.6 manage.py runserver 0.0.0.0:8000 #こっちでは表示される
  170  sudo uwsgi --socket :8001 --module twi.wsgi
  171  sudo vim twi/settings.py
  172  sudo python3.6 manage.py collectstatic
  173  tree
  174  sudo twi_nginx.conf
  175  sudo vim twi_nginx.conf
  176  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
  177  sudo less /var/log/nginx/access.log
  181  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi --static-map /home/xxxx/django/twi/static/
  182  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi --check-static /home/xxxx/django/twi/static/
  183  sudo vim twi/settings.py
  184  sudo vim twi/urls.py
  185  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
  186  sudo python3.6 manage.py runserver 0.0.0.0:8000
  187  sudo vim twi/settings.py
  188  sudo vim twi/urls.py
  189  sudo python3.6 manage.py collectstatic
  190  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
  191  sudo uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
  192  sudo uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi --check-static /home/xxxx/django/twi/
  193  sudo less /var/log/nginx/access.log
  194  sudo systemctl restart nginx
  195  sudo uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi --check-static /home/xxxx/django/twi/
  196  sudo less /var/log/nginx/access.log
  197  sudo vim twi_nginx.conf
  198  sudo vim /etc/nginx/nginx.conf
  199  sudo uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi --check-static /home/xxxx/django/twi/
  200  sudo vim /etc/nginx/nginx.conf
http://nshiba.hatenablog.com/entry/2014/12/05/042018 の記事を参考にuser nginx→xxxx
  201  sudo systemctl restart nginx
  202  sudo uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi --check-static /home/xxxx/django/twi/ #成功
  210  sudo uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
  211  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi
  212  sudo vim /etc/nginx/nginx.conf #もとに戻す
  213  sudo systemctl restart nginx
  214  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi #やっぱり動かない 
  215  sudo less /var/log/nginx/access.log
https://qiita.com/xKxAxKx/items/f43d1bddbc4fb31013b1 を参考にpermission確認
  216  groups nginx
  217  groups xxxx
  218  groups root
  219  ls -l ~
  220  ls -l ~/..
  221  sudo chmod o+x /home/xxxx #drwx------
  222  ls -l ~/..
  223  uwsgi --master --socket :8001 --chdir=/home/xxxx/django/twi/ --module twi.wsgi #成功
  224  curl 127.0.0.1:8000
  225  pwd
  226  sudo systemctl status nginx
  227  sudo systemctl is-enabled nginx
  228  sudo systemctl enable nginx
  231  sudo systemctl status nginx
  237  history
```