项目作者: viniciosbarretos

项目描述 :
Learn how to configure multiple Django projects in a single server
高级语言:
项目地址: git://github.com/viniciosbarretos/Multiple-Django-Single-Server.git


Multiple-Django-Single-Server

Learn how to deal with multiple Django services in a single server using Nginx and Gunicorn.

Requirements

  • Python
  • Django
  • Virtualenv
  • Gunicorn

Important

Django and dependencies installation is not covered by this tutorial.
I recommend you create different environments for each project.

If you never set up any Django production environment, I recommend you read this first: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

1. Creating Gunicorn Service

  1. cd /etc/systemd/system/
  2. sudo vim project1.gunicorn.service

Paste the following code changing everything inside {} for your own values:

  1. [Unit]
  2. Description=gunicorn daemon
  3. After=network.target
  4. [Service]
  5. User={user}
  6. Group=www-data
  7. WorkingDirectory={/home/user/project1}
  8. ExecStart={/home/user/project1/your-virtual-environment}/bin/gunicorn --access-logfile - --workers 3 --bind unix:{/home/user/project1/project1}.sock {project1}.wsgi:application
  9. [Install]
  10. WantedBy=multi-user.target

Save and close file.

2. Creating Gunicorn Socket

  1. cd /etc/systemd/system/
  2. sudo vim project1.gunicorn.socket

Paste the following code changing everything inside {} for your own values:

  1. [Unit]
  2. Description=gunicorn socket
  3. [Socket]
  4. ListenStream={/home/user/project1/project1}.sock
  5. [Install]
  6. WantedBy=sockets.target

Save and close file.

3. Enabling and Starting Socket

This will create the socket file at {/home/user/project1/} now and at boot. When a connection is made to that socket, systemd will automatically start the gunicorn.service to handle it:

  1. sudo systemctl start project1.gunicorn.socket
  2. sudo systemctl enable project1.gunicorn.socket

To check if everything is ok use the following commands:

  1. sudo systemctl status project1.gunicorn.socket
  2. sudo systemctl status gunicorn

If you got any problem, use the following commands to check logs:

  1. sudo journalctl -u gunicorn

After change socket or service file is necessary reload daemon and gunicorn typing:

  1. sudo systemctl daemon-reload
  2. sudo systemctl restart gunicorn

4. Nginx Settings

Create a server block for your website. I will use only http to this tutorial, and if you want to set up https see this other link: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

  1. sudo vim /etc/nginx/sites-available/project1.com

Paste the following code changing everything inside {} for your own values:

  1. server {
  2. listen 80;
  3. server_name {project1.com};
  4. location = /favicon.ico { access_log off; log_not_found off; }
  5. location /static/ {
  6. root {/home/user/project1};
  7. }
  8. location / {
  9. include proxy_params;
  10. proxy_pass http://unix:{/home/user/project1/project1}.sock;
  11. }
  12. }

Now, you need to enable server block and restart Nginx:

  1. sudo ln -s /etc/nginx/sites-available/project1.com /etc/nginx/sites-enabled
  2. sudo systemctl restart nginx

If you have any issue, check if nginx settings are ok:

  1. nginx -t

5. Repeat Steps

Now, all you need is replicate previous steps to other django installations.