Skip to content

Instantly share code, notes, and snippets.

@RitabrataDas343
Forked from mayankt18/Deploy.md
Created March 2, 2022 10:57
Show Gist options
  • Save RitabrataDas343/c7d622a887180001bd8837d48cf1809f to your computer and use it in GitHub Desktop.
Save RitabrataDas343/c7d622a887180001bd8837d48cf1809f to your computer and use it in GitHub Desktop.
Deploy using nginx and gunicorn and install ssl

Deploy with nginx and gunicorn

  1. Clone and run your project
  2. sudo apt update
  3. sudo apt install python3-pip python3-dev nginx
  4. sudo pip3 install virtualenv
  5. python3 -m venv env
  6. source ./env/bin/activate
  7. pip install gunicorn
  8. deactivate
  9. sudo vim /etc/systemd/system/gunicorn.socket

Paste the content below

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
  1. sudo vim /etc/systemd/system/gunicorn.service

Paste the content below

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=<username>
Group=www-data
WorkingDirectory=/home/<username>/<projectdir>
ExecStart=/home/<username>/<projectdir>/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          <application_name>.wsgi:application

[Install]
WantedBy=multi-user.target
  1. sudo systemctl start gunicorn.socket

  2. sudo systemctl enable gunicorn.socket

  3. sudo vim /etc/nginx/sites-available/<application_name>

Paste the content below

server {
    listen 80;
    server_name www.domain_name.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/<username>/<projectdir>;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
  1. sudo ln -s /etc/nginx/sites-available/<application_name> /etc/nginx/sites-enabled/
  2. sudo systemctl restart nginx

Installing ssl

  1. sudo apt install python3-certbot-nginx certbot
  2. sudo certbot --nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment