All the version used in this tutorial
sudo apt update
sudo apt upgrade -y
sudo apt install python3-pip python3-venv
pip3 install gunicorn
sudo apt install nginx -y
. now you can check with sudo nginxNow let us do a little test, we're going to start our application by gunicorn (what is it? will discuss it in later.) -gunicorn --bind 0.0.0.0:8000 project.wsgi:application
, after running this we'll see something like 👇🏻
and now we can access our application from our instance IP address, like this xx.xx.xx.xx:8000
sudo nano /etc/systemd/system/django.service
[Unit]
Description=Topsoil Dev Service
After=network.target
[Service]
User=ubuntu
Restart=on-failure
WorkingDirectory=/home/ubuntu/project_dir
ExecStart=/home/ubuntu/env/bin/gunicorn --bind 0.0.0.0:8000 --timeout 0 --workers=1 --threads=3 --worker-class=gthread main_proj_dir.wsgi:application
Environment=You can set here the environment variable
[Install]
WantedBy=multi-user.target
sudo systemctl restart django.service
sudo systemctl enable django.service
server {
listen 80;
server_name 54.193.19.108;
client_max_body_size 10M;
location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
#fastcgi_read_timeout 300;
#proxy_read_timeout 300;
}
}
Our nginx details is up and running 😁. You can directly access your app from Public IP Address (Lightsail)
* Requirements: domain address which is mapped to your Instance Public IP.
sudo apt install certbot python3-certbot-nginx
sudo certbot certonly --nginx
sudo service nginx restart
You might notice that in production, CSS/JS/Images/Fonts files are not working for /admin.This is because of the Static folder issue. In the local development environment Django use Python HTTPServer, and it automatically handles Static files. But in production our server is running through nginx, that's why we have to do little setup. Let's do the setup.
STATIC_ROOT = BASE_DIR / 'static/'
Go to your nginx django.conf file and add the below code after the location code block
location /static/ {
autoindex on;
alias /home/ubuntu/PROJECT_FOLDER/static/;
}