sudo apt install python3 python3-pip nginx
source environment_name/bin/activate # use name created above "environment_name"
pip install django pip install gunicorn // or directly install both pip install django gunicorn
django-admin startproject myproject # use any name "myproject"
nano gunicorn_conf.py # use any name but using same better "gunicorn_conf.py"
import multiprocessing bind = '127.0.0.1:8000' # Django running port/link workers = multiprocessing.cpu_count() * 2 + 1
cd myproject # navigate to Django Project "myproject"
gunicorn myapp.wsgi:application -c ../gunicorn_conf.py # here myapp is myproject # here "myapp" is a Django project name, and at last gunicorn_conf.py is a gunicorn configuration file which created above/before.
code- oss # to open vs code from terminal
# example code ALLOWED_HOSTS = ['localhost', '0.0.0.0', '127.0.0.1'] # use IP address here
ifconfig
sudo nano /etc/nginx/sites-available/myapp # use any name "myapp" # better use vs code instead of nano sudo code- oss /etc/nginx/sites-available/myapp # check code-oss
server { listen 80; server_name your_domain.com; # Replace with your domain or IP address # example for aboveline: server_name 'ipaddress' 'another domain' ; location / { proxy_pass http://127.0.0.1:8000; # Gunicorn's default address proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled # change "myapp" to nginx config file name used above
sudo nginx -t
sudo systemctl restart nginx
sudo apt install ufw
sudo ufw allow 'Nginx Full'
sudo systemctl start nginx
gunicorn myapp.wsgi:application -c ../gunicorn_conf.py # in above code "myapp" is a Django project name # last gunicorn_conf.py is a gunicorn configuration file name
sudo systemctl stop nginx
联系 DM - Twitter(X)
联系邮箱 - sanya.san@myyahoo.com
以上是使用 Gunicorn 和 Nginx 构建可扩展的 Django 应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!