
uwsgi+nginx+djangoデプロイ
1.LNMP
sudo apt update
sudo apt upgrade
sudo apt install net-tools
sudo apt-get install open-vm-tools-desktop
sudo apt install openssh-server
sudo systemctl start ssh
sudo apt install unzip
sudo apt install redis
sudo apt install nginx
sudo systemctl status nginx
nginx -v
sudo nginx -t
sudo apt install mysql-server mysql-client
sudo systemctl status mysql
mysql --version
sudo systemctl start mysql
sudo systemctl enable mysql
sudo mysql_secure_installation
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '<password>';
CREATE DATABASE django_app;
exit;
sudo apt install php-fpm php-mysql php-gd php-xml
php -v
systemctl status php8.3-fpm2.Conda
# conda download
sudo wget https://repo.anaconda.com/archive/Anaconda3-2024.06-1-Linux-x86_64.sh
bash ./Anaconda3-2024.06-1-Linux-x86_64.sh -b
sudo rm -rf Anaconda3-2024.06-1-Linux-x86_64.sh
source ~/.bashrc
export PATH="~/anaconda3/bin:$PATH"
source ~/.bashrc
conda --version
conda create --name django_env python=3.10
conda init
source ~/.bashrc
conda activate django_env
conda install uwsgi
pip install -r requirements.txt3.プロジェクトパッケージ化
# データベース導出
mysqldump -uroot -p django_app > django_app.sql
# デーブル導出
mysqldump -uroot -p database table > table.sql
# データベース導入
mysql -uroot -p django_app < django_app.sql
# テーブル導入
mysql -uroot -p fdatabase < /path/to/table.sql
# 仮想環境導出
cd YOUR DJANGO PROJECT PATH
pip freeze > requirements.txt
# プロジェクトアップロード
scp 「your_project.zip」 root@ip:/var/www/django_app
# 仮想環境導入
pip install -r requirements.txt4.uWSGI
ダンロード
conda install uwsgiテスト
sudo vim test.py
# i
def application(env,start_response):
start_response('200 ok',[('Content-Type','text/html')])
return [b"Hello World"]Esc キーを押して「wq」と入力し、ファイルを保存して終了する。
アクセスの実行をテスト
uwsgi --http :8080 --wsgi-file test.py
ブラウザ で http://<IP>:8080 と入力して、テスト ファイルにアクセスし、以下のページが表示されれば、uWSGI環境が正常に構築されている。

5.Django
sudo vim settings.pyi を入力して編集モードに入り、ALLOWED_HOSTS = [ ] を ALLOWED_HOSTS = ["*"] に変更し、DEBUG = Falseに変更する。変更が完了したら、Esc キーを押して「wq」と入力し、ファイルを保存して終了する。
["*"] は、どの IP アドレスでもアクセスが許可されることを意味し、それ以外の場合はアクセスが拒否される。これは実際の環境によって異なる。


アプリケーションの移行
python manage.py migrate移行を適用した後、次のコマンドを使用して、すべての移行が適用されたことを確認する。
python manage.py showmigrations
Django 開発サーバーを起動
python manage.py runserver 0.0.0.0:8000問題なければ、ブラウザで http://<パブリック IP>:8000 と入力して、関連する Django ページにアクセスできる。

静的ファイル
本番環境用に Django で静的ファイルを収集する
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')Django のcollectstatic コマンドを使用して、アプリケーションとライブラリからすべての静的ファイルを STATIC_ROOT ディレクトリに収集する。
python manage.py collectstaticDNSレコードを確認
dig your domain or subdomain;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 891
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;m.aicscholar.store. IN Anginx
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
listen [::]:80;
server_name doamin;
charset utf-8;
location /static/ {
autoindex on;
alias /var/www/django_app/staticfiles/;
}
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT iCourse.wsgi;
uwsgi_param UWSGI_CHDIR /iCourse;
index index.html index.htm;
client_max_body_size 35m;
}
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
uwsgi
[uwsgi]
socket = 127.0.0.1:8000
chdir = /var/www/django_app
wsgi-file = %(chdir)/django_app/wsgi.py
processes = 4
threads = 2
vacuum = true
buffer-size = 65536
chmod-socket = 666
static-map=/static=/var/www/django_app/staticfiles
master = true
chmod-socket = 755
stats=%(chdir)/uwsgi.status
pidfile=%(chdir)/uwsgi.pid6.screen
Django プロジェクトが uwsgi 経由でリモート VPS 上で 24 時間確実に実行され、ローカル コンピューターの電源が入っているかどうかに依存しないようにするには、次の方法を実行する。
screen または tmux をインストール
# Debian/Ubuntu
sudo apt-get install screen
# Or
sudo apt-get install tmux
# CentOS/RHEL
sudo yum install screen
# Or
sudo yum install tmuxscreen をインストールした後、それを使用して、SSH から切断した後も実行を継続する 1 つ以上のセッションを作成する。
screen または tmux を使用してアプリケーションを起動
# screen
screen -S my_django_app
# uwsgi
uwsgi --ini uwsgi.ini
# tmux
tmux new -s my_django_app
# uwsgi
uwsgi --ini uwsgi.ini ここで、 -S my_django_session は、セッションをより簡単に識別して管理できるように、セッションに名前 (この例では my_django_session) を付けるためのオプションのパラメーター。 -S と名前を省略した場合、screen はデフォルトの名前を生成する。
Screen セッションに再接続
以前に作成したスクリーン セッションに再接続する場合は、次のコマンドを使用して、アクティブなスクリーン セッションをすべてリストする。
screen -ls
screen -r my_django_sessionscreen セッション内では、exit コマンドを入力するか Ctrl+D を押すことでセッションを終了できる。
7.SSL Certbot
VPSを用意
ウェブサーバーが稼働している (Apache、NGINX など)
VPS でホストされているウェブサイトは、IP ではなく、アドレスバーにドメイン名を入力して開くように設定されている
ドメインは完全に伝播され、VPS の子ネームサーバーを指している
依存関係をインストール
開始するには、SSH を使用して VPS に接続する
APT ベースのディストリビューション (Debian、 Ubuntu など) の場合は、以下を実行
sudo apt update
sudo apt install python3 python3-venv libaugeas0RPM ベースのディストリビューション (Fedora、CentOS) の場合
sudo dnf install python3 augeas-libsCertbotをインストール
以前のバージョンとの競合を防ぐため、最新バージョンをインストールする前に、すでにインストールされている Certbot パッケージをすべて削除してください。これには、パッケージ マネージャー (apt、dnf、yum など) を使用できる
これをApacheで実行
conda activate dajngo_app
pip install certbot certbot-apache
pip list | grep certbotNginxで実行
conda activate dajngo_app
pip install certbot certbot-nginx
pip list | grep certbot
次に、Certbot を任意のパスから実行できるようにシンボリック リンクを作成
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
ls -l /usr/bin/certbot
SSLをインストールして有効化
Apacheの場合
certbot --apacheNginxの場合
certbot --nginxSaving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): your email
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n
Account registered.
Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Requesting a certificate for example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2024-11-02.
These files will be updated when the certificate renews.
Deploying certificate
Successfully deployed certificate for aicscholar.store to /etc/nginx/conf.d/example.com.conf
Congratulations! You have successfully enabled HTTPS on https://example.com
NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf
https://certbot.org/renewal-setupa
SSL は 90 日間有効であるため、次のコマンドで自動更新を設定する
echo "0 0,12 * * * root /root/anaconda3/envs/django_env/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && sudo /root/anaconda3/envs/django_env/bin/certbot renew -q" | sudo tee -a /etc/crontab > /dev/null毎日 0:00 と 12:00 に、django_env 仮想環境で Python インタープリターを使用して、root ユーザーとしてこのコマンドを実行
SSL確認
/root/anaconda3/envs/django_env/bin/certbot certificates- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: your domain
Serial Number: 33e59413b549636d7f3d5431e9dbc135281
Key Type: ECDSA
Domains: aicscholar.store
Expiry Date: 2024-11-02 10:58:38+00:00 (VALID: 88 days)
Certificate Path: /etc/letsencrypt/live/your domain/fullchain.pem
Private Key Path: /etc/letsencrypt/live/your domain/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -