项目作者: WilliamYMH

项目描述 :
Background tasks with django, celery and redis.
高级语言: JavaScript
项目地址: git://github.com/WilliamYMH/django-celery.git
创建时间: 2020-10-25T17:34:49Z
项目社区:https://github.com/WilliamYMH/django-celery

开源协议:

下载


django-celery

Background tasks with django, celery and redis.

Requirements

Execute following commands:

  1. sudo apt update
  2. sudo apt-get install postgresql
  3. sudo apt-get install python-psycopg2
  4. sudo apt-get install libpq-dev

Execute project local:

create virtual enviroment (execute these commands):

  1. virtualenv 'name_enviroment'
  2. source 'name_enviroment'/bin/activate
  3. pip3 install -r requirements.txt

executing the project:

  1. python manage.py makemigrations
  2. python manage.py migrate
  3. python manage.py runserver

Don’t forget

  • create an celery broker and add it to settings.py
  • add EMAIL_HOST to settings.py
  • add the bd settings

Execute project with nginx-gunicorn

Installing ngninx

  1. sudo apt install nginx

two step, set up firewall

  1. sudo ufw app list
  2. sudo ufw allow 'Nginx HTTP'

check firewall

  1. sudo ufw status

check server web

  1. systemctl status nginx

enter the localhost to verify. (http://localhost/)

other commands

  1. sudo systemctl stop nginx
  2. sudo systemctl start nginx
  3. sudo systemctl restart nginx
  4. sudo systemctl reload nginx
  5. sudo systemctl disable nginx
  6. sudo systemctl enable nginx

Check that gunicorn works

  1. gunicorn --bind 0.0.0.0:8000 django_celery.wsgi

check localhost:8000

create socket and services files

The Gunicorn socket will be created on startup and will listen for connections. When a connection is established, systemd will automatically start the Gunicorn process to handle the connection.

  1. create the file socket systemd for gunicorn
    1. sudo nano /etc/systemd/system/gunicorn.socket
    with these content:
    ```
    [Unit]
    Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

  1. save and close the file.
  2. 2. create the file service systemd for gunicorn

sudo nano /etc/systemd/system/gunicorn.service

  1. copy and paste the following:

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

  1. Now, we will open the [Service] section. We will specify the user and the group with which we want the process to run. We will grant ownership of the process to our normal user account, as it has ownership of all relevant files. We will grant group ownership to the www-data group so that Nginx can easily communicate with Gunicorn.
  2. Next, we will map the working directory and specify the command that will be used to start the service. In this case, we will have to specify the full path to the Gunicorn executable, which is installed in our virtual environment. We will bind the process to the Unix socket we created in the / run directory so that the process can communicate with Nginx. We log all the data to standard output so that the journald process can collect the Gunicorn logs. We can also specify any optional Gunicorn settings here. For example, we specify 3 worker processes in this case:
  3. #### note:
  4. The WorkingDirectory is the same where is manage.py.

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

[Service]
User=william
Group=www-data
WorkingDirectory=/home/william/project/django-celery
ExecStart=/home/william/project/django-celery/env_celery/bin/gunicorn \
—access-logfile - \
—workers 3 \
—bind unix:/run/gunicorn.sock \
django_celery.wsgi:application

  1. Lastly, we will add an [Install] section. This will tell systemd what to bind this service to if we enable it to load on startup. We want this service to start when the normal multi-user system is up and running:

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

[Service]
User=william
Group=www-data
WorkingDirectory=/home/william/project/django-celery/django_celery
ExecStart=/home/william/project/django-celery/env_celery/bin/gunicorn \
—access-logfile - \
—workers 3 \
—bind unix:/run/gunicorn.sock \
django_celery.wsgi:application

[Install]
WantedBy=multi-user.target

  1. save and close the file.
  2. Now we can start and enable the Gunicorn socket. This will create the socket file in /run/gunicorn.sock now and on startup.

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

  1. check the status of service:

sudo systemctl status gunicorn.socket

  1. To test the socket triggering mechanism, we can send a connection to the socket via curl by typing the following:

curl —unix-socket /run/gunicorn.sock localhost

  1. You should see the HTML output of your application in the terminal. This indicates that Gunicorn has started and was able to present its Django app.
  2. To restart the service of gunicorn if the file [/etc/systemd/systemd/system/gunicorn.service] has been changed, typing the following:

sudo systemctl daemon-reload
sudo systemctl restart gunicorn

  1. ## Configure Nginx for authorization pass to Gunicorn
  2. Begin by creating and opening a new server block in the Nginx sites-available directory:

sudo nano /etc/nginx/sites-available/myproject

  1. copy and paste the following:

server {
listen 80;
server_name 127.0.0.1;

  1. location = /favicon.ico { access_log off; log_not_found off; }
  2. location /static/ {
  3. root /home/william/project/django-celery/django_celery;
  4. }
  5. location / {
  6. include proxy_params;
  7. proxy_pass http://unix:/run/gunicorn.sock;
  8. }

}

  1. save and close the file.
  2. we enable the file by linking it to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

  1. Test your Nginx configuration to rule out syntax errors by typing the following:

sudo nginx -t

  1. Restart nginx

sudo systemctl restart nginx

  1. Finally, we must open our firewall to normal traffic on port 80. As we no longer need access to the development server, we can remove the rule to also open port 8000:

sudo ufw allow ‘Nginx Full’
```
this guide was taken from:
https://www.digitalocean.com/community/tutorials/como-configurar-django-con-postgres-nginx-y-gunicorn-en-ubuntu-18-04-es