lightsail_nginx_uwsgi_django - ntuf/Tips GitHub Wiki

proj: djangoプロジェクト名
yyy.xxx.com: ドメイン(yyyがサブドメイン)
DATABASE: データベース名
app-user: アプリユーザ名
#####: アプリユーザpass

-Lightsailの固定IP(静的パブリックIP)を取得しておく
—Route53にレコードを作成しておく
-
# レコード名 yyy.xxx.com

  1. レコードタイプ A
  2. 値 静的IP

— yumの最新化
sudo yum update

— MariaDBの削除
sudo yum remove mariadb-libs

— 各種インストール
sudo yum install git
sudo yum install python3-pip
sudo pip3 install django
#(sudo yum install nginxではできない)
sudo amazon-linux-extras install nginx1

— uwsgi
sudo yum install python3-devel
sudo yum groupinstall “Development Tools”
#上二つは、uwsgiインストールに必要
sudo pip3 install uwsgi

- OSの文字コードを変更
sudo localectl set-locale LANG=ja_JP.utf8
-
サーバ再起動して確認
echo $LANG
—ja_JP.UTF-8になっていること

—nginx自動起動を設定
sudo systemctl enable nginx.service
systemctl is-enabled nginx.service
sudo systemctl start nginx.service

- MySQL
#リポジトリの追加
sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y
-
#mysql5.7を有効にする
—sudo yum-config-manager —disable mysql80-community
—sudo yum-config-manager —enable mysql57-community
#インストール
sudo yum install mysql-community-server -y
#5.7であることを確認する
mysqld —version
#文字コードを設定する
sudo vi /etc/my.cnf
#末尾に以下を追加する

  1. character-set-server=utf8
  2. [client]
  3. default-character-set=utf8
    #自動起動設定
    sudo systemctl start mysqld.service
    sudo systemctl enable mysqld.service
    systemctl status mysqld.service
    #rootパスワードの確認
    sudo cat /var/log/mysqld.log | grep password
    #初期設定
    mysql_secure_installation
    #ログイン
    mysql -u root -p
    #DataBaseの作成
    create database DATABASE;
    #Djangoからmysqlへ接続するためのユーザの作成
    CREATE USER ‘app-user’'localhost' IDENTIFIED BY '#####'; #ユーザへの権限付与 GRANT ALL on DATABASE.* to 'app-user'‘localhost’;
    exit

-ssh関連
#Install EPEL repository for RHEL 7(certbotのため)
##https://aws.amazon.com/jp/premiumsupport/knowledge-center/lightsail-install-certbot-package/
sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
#certbotのインストール
sudo yum install -y certbot
#Let’sEncryptで証明書のリクエスト
sudo certbot -d yyy.xxx.com —manual —preferred-challenges dns certonly
##その後,ACMEの検証用TXTレコードを作成するよう促されるのでRoute53にTXTレコードを作成
#レコード名 _acme-challenge.yyy.xxx.com
#値は渡されたもの
#格納ディレクトリ作成
sudo mkdir /etc/nginx/ssl
#証明書を移す
sudo cp /etc/letsencrypt/live/yyy.xxx.com/fullchain.pem /etc/nginx/ssl
sudo cp /etc/letsencrypt/live/yyy.xxx.com/privkey.pem /etc/nginx/ssl
#ディレクトリ権限設定
sudo chmod 400 /etc/nginx/ssl/*
#nginxの設定が正しいかチェック。successfulが出るか。
sudo nginx -t
#nginxへ証明書反映リロード
sudo systemctl reload nginx または sudo systemctl start nginx
#cronで自動更新設定
crontab -e
#以下を編集 13時起動とする。
0 13 * * * DOMAIN=yyy.xxx.com
0 13 * * * certbot renew —manual-public-ip-logging-ok
0 13 * * * cp /etc/letsencrypt/live/$DOMAIN/privkey.pem /etc/nginx/ssl
0 13 * * * cp /etc/letsencrypt/live/$DOMAIN/fullchain.pem /etc/nginx/ssl
0 13 * * * systemctl reload nginx
#テストGetリクエスト
curl -svo /dev/null https://yyy.xxx.com
—uwsgiの自動起動設定
-
/var/www/uwsgi.iniを配置する(内容はページ下を参照)
—サービス設定ファイルの作成(内容はページ下を参照)
sudo vi /etc/systemd/system/uwsgi.service
sudo systemctl enable uwsgi.service
sudo systemctl start uwsgi.service

—プロジェクトのデプロイ
cd /home/ec2-user
git clone https://xxxxxxxxxx.git
cd proj
python3 manage.py migrate

sudo yum install mysql-devel
sudo pip3 install mysqlclient

python3 manage.py collectstatic
#static以下を参照できるようにotherに実行権限を与える
sudo chmod o+x /home/ec2-user/

—終了


/etc/nginx/conf.d/****_nginx.conf
--

  1. the upstream component nginx needs to connect to
    upstream django {
    #ip_hash;
    #server yyy.xxx.com:8001;
    #server nnn.nnn.nnn.nnn:8001;
    #server 127.0.0.1:8001;
    #server unix:///home/ec2-user/proj/mysite.sock;
    server 0.0.0.0:8001;
    }
  1. configuration of the server
    server {
  2. the port your site will be served on
    listen 443 ssl http2;
    #listen 80;
  3. the domain name it will serve for
    server_name yyy.xxx.com; # substitute your machine’s IP address or FQDN
    #server_name nnn.nnn.nnn.nnn;
    #root /home/ec2-user/proj;
    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
charset utf-8;
  1. max upload size
    client_max_body_size 75M; # adjust to taste
location /static { #alias /static; alias /home/ec2-user/proj/web_graphs/static; }
  1. Finally, send all non-media requests to the Django server.
    location / {
    uwsgi_pass django;
    include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }

}

server {
listen 80;
server_name yyy.xxx.com;
return 301 https://$host$request_uri;
}
--


/etc/nginx/nginx.conf
--

  1. For more information on configuration, see:
  2. * Official English Documentation: http://nginx.org/en/docs/
  3. * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

  1. Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ’
’$status $body_bytes_sent “$http_referer” ’
’"$http_user_agent" “$http_x_forwarded_for”’;

access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf;

}
--


/var/www/uwsgi.ini
--
[uwsgi]
chdir = /home/ec2-user/
socket = :8001
module = analysis_project.wsgi
chmod-socket = 666
--


/etc/systemd/system/uwsgi.service
--
[Unit]
Description = uWSGI
After = syslog.target

[Service]
ExecStart = /usr/local/bin/uwsgi —ini /var/www/uwsgi.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target
--

⚠️ **GitHub.com Fallback** ⚠️