40分でUbuntu Nginx PostgreSQL Django Gunicornの環境構築
40分でUbuntu Nginx PostgreSQL Django Gunicornの環境構築 VPSへのログイン(新規タブ) OSの初期設定(アップデート、アップグレード) 新規ユーザーを追加 新しく追加したユーザー […]
Filter by Category
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:
mkdir ~/.ssh
chmod 700 ~/.ssh
ls -la
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
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
sudo apt install postgresql
psql -V
psql (PostgreSQL) 12.4 (Ubuntu 12.4-0ubuntu0.20.04.1)
which psql
/usr/bin/psql
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
sudo apt install -y python3-pip
sudo apt -y install git
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
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
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic
python manage.py createsuperuser
Alembic API argparse Beautiful Soup black Channels charset CodeCommit datetime Django REST framework Docker enumerate f-string git GitHub glob Google Colaboratory i18n Internship Jupyter Lambda Matplotlib Nginx OpenCV pandas PIL Pillow PostgreSQL PyCharm PyCon pyenv PyTorch Redis Rembg ReportLab requests S3 Selenium Sentry slack tqdm uWSGI venv Vue.js youtube
群馬県でPythonを使ったAIやソフトウェアを開発している株式会社ファントムが運営しています。
メーリングリストに登録するとファントムの最新情報をお届けします
お客様のメールアドレスを共有することはありません
Comments