How to deploy 2 django projects in Oracle Linux using apache and Gunicorn

Having 2 separate Django Projects in the same server is a common implementation practice. We are going to explore the steps required for activating 2 Django projects with a Virtual Env setup in the same project.

  1. Create a new Gunicorn service
Bash
sudo nano /etc/systemd/system/gunicorn_2.service

Note : If you have a existing Guicorn service create a new file with different name and the extension should be .service.

[Unit]
Description=gunicorn daemon for Django project
After=network.target

[Service]
User=jovaan
Group=jovaan
WorkingDirectory=/home/user/dev/projects/circuit/Django/circuit
ExecStart=/home/user/dev/projects/circuit/env39/bin/gunicorn --workers 3 --bind 0.0.0.0:8000>

[Install]
WantedBy=multi-user.target

Note :

WorkingDirectory is the Project directory of the Django project. If you navigate to the folder manage.py file will be visible along with the app folders

ExecStart : Virtual environment where gunicorn is installed.

2. Reload Gunicorn Services and check status

Bash:

sudo systemctl daemon-reload
sudo systemctl start project1
sudo systemctl enable project1

sudo systemctl start project2
sudo systemctl enable project2

sudo systemctl status project1
sudo systemctl status project2

Note: project1 / project2 are file names of gunicorn services with extension .service

3. Configuring Apache Server with Reverse Proxy

Bash:

sudo nano /etc/httpd/conf.d/project1.conf

Note : project1.conf is the configuration file required for new site
<VirtualHost *:80>
    ServerName custom.server.com
    ServerAlias www.custom.server.com

    ErrorLog /var/log/httpd/server.log
    CustomLog /var/log/httpd/server_access.log combined

    ProxyPass / http://127.0.0.1:8002/
    ProxyPassReverse / http://127.0.0.1:8002/

   <Directory /opt/projects/project1>
        Require all granted
    </Directory>

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.custom.server.com [OR]
    RewriteCond %{SERVER_NAME} =custom.server.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

If multiple Django projects are being executed on the same server make sure the port is not repeated. 8002 is maintained in this example assuming 8000 is being used for the primary site.

4. Ensure Required Apache modules are availabe

sudo dnf install -y mod_proxy mod_proxy_http mod_proxy_uwsgi mod_ssl

sudo systemctl restart httpd
sudo systemctl enable httpd

5. Ensure Firewall setting

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

6. Setup SSL using Lets Encrypt

sudo dnf install certbot python3-certbot-apache -y
sudo certbot --apache -d project1.example.com -d project2.example.com

Renew automatically
sudo systemctl enable certbot-renew.timer

7. Final Refresh of all services

Bash:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload


sudo systemctl daemon-reload
sudo systemctl start project1
sudo systemctl enable project1

sudo systemctl start project2
sudo systemctl enable project2

sudo systemctl status project1
sudo systemctl status project2

sudo systemctl restart httpd
sudo systemctl enable httpd

sudo systemctl restart project1
sudo systemctl restart project2
sudo systemctl restart httpd

Leave a Reply

Your email address will not be published. Required fields are marked *