40分でUbuntu Nginx PostgreSQL Django Gunicornの環境構築


press
40分でUbuntu Nginx PostgreSQL Django...

40分でUbuntu Nginx PostgreSQL Django Gunicornの環境構築

VPSへのログイン(新規タブ)

OSの初期設定(アップデート、アップグレード)

ssh ubuntu@133.125.51.105
ubuntu@133.125.51.105's password:
sudo apt update
apt list --upgradable
sudo su -

新規ユーザーを追加

adduser vpsadmin
New password: Retype new password: passwd: password updated successfully Changing the user information for vpsadmin Enter the new value, or press ENTER for the default Full Name []: VPS Admin Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y

新しく追加したユーザーにsudo権限を持たせ、sudo権限を持ったユーザーを表示して確認します。

gpasswd -a vpsadmin sudo
cat /etc/group | grep sud

追加したユーザがsudo権限を持つことが確認できました。

sudo:x:27:ubuntu,vpsadmin

追加したユーザーでログイン

ssh vpsadmin@133.125.51.105
vpsadmin@133.125.51.105's password:

SSHログイン

sshフォルダの作成

mkdir ~/.ssh
chmod 700 ~/.ssh
ls -la

SSHログインの設定

SSH鍵の作成

cd .ssh
ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/nobukazuishii/.ssh/id_rsa):
sakura_vps
Enter passphrase (empty for no passphrase):
sakura_vps
Enter same passphrase again:
sakura_vps
Generating public/private rsa key pair. Enter file in which to save the key (/Users/nobukazuishii/.ssh/id_rsa): sakura_vps Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in sakura_vps. Your public key has been saved in sakura_vps.pub. The key fingerprint is: SHA256:MFVLyulTj/GKQKX34pxNM8AyHM4h9WcLrP55KPYrlww nobukazuishii@MBA.local The key's randomart image is: +---[RSA 3072]----+ | ..+ o.o | | = @ + . | | @ @ * | | . O B * | | o S B o | | .E+ B + | | .o=oo | | +.=.. | | . ==o | +----[SHA256]-----+

鍵をサーバーにコピー

scp sakura_vps.pub vpsadmin@133.125.51.105:/home/vpsadmin/.ssh/authorized_keys
vpsadmin@133.125.51.105's password:
sakura_vps.pub 100% 577 23.2KB/s 00:00

ログイン設定(新規タブ)

ssh vpsadmin@133.125.51.105
sudo vim /etc/ssh/sshd_config
Port 12322
PermitRootLogin no
PubkeyAuthentication yes コメントアウト
PasswordAuthentication no
:wq
sudo service sshd restart

ポートを開ける

sudo ufw enable
y
sudo ufw allow 12322
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 8000
sudo ufw status verbose
Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip

To Action From

12322 ALLOW IN Anywhere
80 ALLOW IN Anywhere
443 ALLOW IN Anywhere
8000 ALLOW IN Anywhere
12322 (v6) ALLOW IN Anywhere (v6)
80 (v6) ALLOW IN Anywhere (v6)
443 (v6) ALLOW IN Anywhere (v6)
8000 (v6) ALLOW IN Anywhere (v6)
sudo ufw reload

ログインショートカットの設定(新規タブ)

.ssh/configにショートカット用の設定を追記

vim .ssh/config
Host vpstest
  HostName 133.125.51.105 
  User vpsadmin 
  Port 12322
  IdentityFile ~/.ssh/sakura_vps
ssh vpstest

インストール

Nginxのインストール

sudo apt install nginx -y
sudo vim /etc/nginx/sites-available/testvps
server {
        server_name 133.125.51.105;

        root /var/www/testvps/;

        location / {
           proxy_pass <http://unix>:/run/gunicorn/socket;
       }

       location /static/ {
           alias /var/www/testvps/static/;
       }
}
sudo ln -s /etc/nginx/sites-available/testvps /etc/nginx/sites-enabled
sudo /etc/init.d/nginx restart

PostgreSQLのインストール

sudo apt install postgresql
psql -V
psql (PostgreSQL) 12.4 (Ubuntu 12.4-0ubuntu0.20.04.1)
which psql
/usr/bin/psql

PostgreSQLの設定

sudo -u postgres psql
CREATE DATABASE djangodeploy;
CREATE USER djangoadmin WITH PASSWORD 'password1234';
ALTER ROLE djangoadmin SET client_encoding TO 'utf8';
ALTER ROLE djangoadmin SET default_transaction_isolation TO 'read committed';
ALTER ROLE djangoadmin SET timezone TO 'Asia/Tokyo';
GRANT ALL PRIVILEGES ON DATABASE djangodeploy TO djangoadmin;
ctrl + d
sudo /etc/init.d/postgresql restart
cd /etc/postgresql/12/main
sudo vim pg_hba.conf
# "local" is for Unix domain socket connections only
local all all peer   >>>   local all all md5
:wq
sudo vim postgresql.conf
#listen_addresses = 'localhost'   >>>   listen_addresses = '*'
:wq
sudo service postgresql restart
cd

pipのインストール

sudo apt install -y python3-pip

Gitのインストール

sudo apt -y install git

アプリケーションの準備

GitHubの設定

mkdir /var/www/testvps
cd /var/www/
sudo chown vpsadmin testvps
cd testvps
sudo git init
sudo git remote add origin https://github.com/nobnov/deploy_django.git
sudo git pull origin master

仮想環境の作成

sudo apt-get install python3-venv
python3 -m venv venv

モジュールのインストール

pip install -r requirements.txt

Gunicornの設定

sudo vim /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn/socket

[Install]
WantedBy=sockets.target
sudo vim /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
WorkingDirectory=/var/www/testvps
ExecStart=/var/www/testvps/venv/bin/gunicorn --workers 2 --bind unix:/run/gunicorn/socket deploy_django.wsgi:application

[Install]
WantedBy=multi-user.target
sudo /etc/init.d/nginx configtest
sudo systemctl enable --now gunicorn.socket
sudo systemctl start gunicorn.service

Djangoの初期設定

python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic
python manage.py createsuperuser

人気のタグ

API Beautiful Soup black Channels charset ChromeDriver CodeCommit datetime Django REST framework Docker enumerate f-string gettext git GitHub glob Google Colaboratory i18n Internship Jupyter Matplotlib Nginx OpenCV pandas PIL Pillow PostgreSQL psycopg2 PyCharm PyCon pyenv PyTorch Redis Rembg ReportLab requests S3 Selenium Sentry slack tqdm uWSGI venv Vue.js youtube


株式会社ファントムへのお問い合わせ

群馬県でPythonを使ったAIやソフトウェアを開発している株式会社ファントムが運営しています。




    Show Comments (0)

    Comments

    Related Articles

    Python

    Appleシリコン搭載MacのDocker環境でTensorflowを使う

    Appleシリコン搭載MacのDocker環境でTensorflowを使う Intel搭載のMacで作成したTensorflowを使ったプロジェクトを移して、M1やM2などのAppleシリコンを搭載しているMacでTen […]

    Posted on by press
    Django

    ダウンロードするファイルの文字コードを動的に変更

    ダウンロードするファイルの文字コードを動的に変更 ウェブアプリケーションの中でCSVやテキストファイルをダウンロードさせた時に文字化けが発生する事があります。使用する文字コードが決まっていれば予め固定してしまう事で文字化 […]

    Posted on by press

    最新情報をお届けします!

    メーリングリストに登録するとファントムの最新情報をお届けします

    お客様のメールアドレスを共有することはありません