Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nullx5/da984e73fc34f3bf64587391c6a87458 to your computer and use it in GitHub Desktop.
Save nullx5/da984e73fc34f3bf64587391c6a87458 to your computer and use it in GitHub Desktop.

Deploy Django en produccion Apache + Gnicorn con Socket Unix | HAY ERRORES INVESTIGAR MÁS

sudo mkdir /srv/myproject; cd /srv/myproject


sudo python3 -m venv env

source env/bin/activate

sudo env/bin/python3 -m pip install django
sudo env/bin/python3 -m pip install gunicorn
pip freeze

sudo env/bin/django-admin startproject config .

sudo env/bin/python3 manage.py makemigrations
sudo env/bin/python3 manage.py migrate
sudo env/bin/python3 manage.py runserver

sudo nvim config/settings.py
DEBUG = False
ALLOWED_HOSTS = ["192.168.100.50"]

sudo nvim config/views.py
from django.http import HttpResponse

def response(request):
    res = "<h1>Django + Gunicorn + Socket Unix</h1>"
    return HttpResponse(res)

sudo nvim config/urls.py
from config.views import response

path("response/", response)

sudo env/bin/python3 manage.py runserver 192.168.100.50:8000 # 404 Not Found
sudo env/bin/python3 manage.py runserver 192.168.100.50:8000/response # 200 OK

sudo adduser --system --no-create-home --group gunicorn
sudo chown -R gunicorn:gunicorn /srv/myproject


sudo apt install apache2 -y
sudo apt-get install libapache2-mod-proxy-uwsgi
sudo a2enmod proxy proxy_http proxy_uwsgi
a2query -m

sudo systemctl restart apache2
sudo systemctl status apache2

### Sistema con 8 CPU cores y 24 GB RAM

sudo nvim /lib/systemd/system//gunicorn.service
[Unit]
Description=Gunicorn con alto rendimiento
After=network.target

[Service]
User=gunicorn
Group=gunicorn
WorkingDirectory=/srv/myproject/
ExecStart=/srv/myproject/env/bin/gunicorn --workers 16 --threads 4 --worker-class gevent \
    --bind unix:/run/gunicorn.sock --keep-alive 2 --max-requests 5000 \
    --timeout 30 --log-level info config.wsgi
Restart=always

[Install]
WantedBy=multi-user.target



sudo systemctl daemon-reload
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn


sudo nvim /etc/apache2/sites-available/myproject.conf
<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        ServerName 192.168.100.50
        DocumentRoot /srv/myproject

        ProxyPass "/" "unix:/run/gunicorn.sock|http://localhost/"
        ProxyPassReverse "/" "unix:/run/gunicorn.sock|http://localhost/"

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

sudo a2ensite myproject.conf
a2query -s

sudo systemctl restart apache2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment