- Linux serverda loyihani Github dan clone qilib olamiz
git clone https://github.com/
- Virtual muhit yaratib olish
python -m venv venv
- virtual muhitni faollashtirish
venv\Scripts\activate
- Kerakli kutubxonalarni requirements.txt faylidan o'qib, muhitga o'rnatish
pip install -r requirements.txt
- Loyiha to'g'ri ishlashi uchun va admin panelga kirish imkoniyati bo'lishi uchun Django tomonidan tayyorlab qo'yilgan migratsiyalarni yaratib olamiz
python manage.py makemigrations
python manage.py migrate
- superuser yaratib olamiz (admin panelga kirish imkoniyati bo'lishi uchun)
python manage.py createsuperuser
- Django loyihani ishlatib ko'rish, agar quyidagicha rasm ko'rinsa, unda hammasi joyida bo'ladi
python manage.py runserver
- Serverni CTRL + C bilan to'xtatamiz
- root yo'nalish bo'yicha o'tib olamiz
cd /root/
- Nginx web serverini o'rnatib olamiz
sudo apt update
sudo apt install python3-venv
sudo apt install nginx -y
- Virtual muhitni o'chirib olamiz
deactivate
- 8000 portga ruxsat beramiz (linux server loyihamizni 8000 chi portda ishlatish imkoniga ega bo'lishi uchun)
sudo ufw allow 8000
- gunicorn faylni yaratamiz (Django loyihani run qilib beradigan asosiy fayl)
sudo nano /etc/systemd/system/gunicorn.socket
- gunicorn fayli ichiga quiydagilarni yozamiz
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
- Yuqorida yaratgan gunicorn faylini ishga tushiradigan service fayl yaratib olamiz
sudo nano /etc/systemd/system/gunicorn.service
- service fayl ichiga quyidagilarni yozamiz
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=<USERNAME>
Group=www-data
WorkingDirectory=/<USERNAME>/<PROJECT_FOLDER>
ExecStart=/<USERNAME>/<PROJECT_FOLDER_NAME>/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
<PROJECT_ROOT_FOLDER>.wsgi:application
[Install]
WantedBy=multi-user.target
- gunicorn faylini ishlatish uchun buyruq beramiz (bu bizga Django loyihamizni avtomatik ishlatib (run) qilib beradi)
sudo systemctl start gunicorn.socket
- Linux server har sfar o'chib yonganida yuqoridagi komanda avtomatik ishga tushishi uchun quyidagi komandani teramiz
sudo systemctl enable gunicorn.socket
- Kiritilgan o'zgarishlar ishga tushishi uchun quyidagi komandani teramiz
sudo systemctl daemon-reload
- gunicorn faylini ham bir marta restart qilib yuboramiz
sudo systemctl restart gunicorn
- Linux serverga kelgan so'rovlar avtomatik Django loyihaga yuborilishi uchun bu uchun Nginx fayl yozamiz va qaysi manzillardan so'rov kelsa loyihani ishlatib berish kerakligi aytamiz
sudo nano /etc/nginx/sites-available/PROJECT_FOLDER
- Yuqorida yaratib olgan faylimizi ichini quyidagilar bilan to'ldiramiz
server {
listen 80;
server_name <SERVER_IP_ADDRESS>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
<USERNAME> /<PROJECT_FOLDER>/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
- Hozirgi yozgan o'zgarishlarimiz ishga tushishi uchun nginx fayliga restart berib yuboramiz
sudo systemctl restart nginx
- Nginx uchun barcha ruxsatlarni beramiz
sudo ufw allow 'Nginx Full'
- gunicorn faylini yana bir marta restart qilib yuboramiz
sudo systemctl restart gunicorn
Agar loyihaning har qanday fayliga biron kichik o'zgartirish kiritilsa ham, home.html, views.py, urls.py, ... Har safar quyidagi buruqni ishlatib yuborish kerak. Ya'ni 25-qadamdagi buyruqni ☝️
sudo systemctl restart gunicorn
online ko'rishimiz mumkin. E'tiboringiz uchun raxmat 🙂