Django deployment with nginx and gunicorn using jenkins

In this tutorial, I will  demonstrate  how to deploy a Django project with Gunicorn and Nginx using Jenkins to achieve continuous integration .


Prerequisites :

- Basic idea on Django application deployment .
- Your virtual environment ready for the application .
- A Ubuntu based server(or you can try on your local Ubuntu machine)
- Your application in Gitlab/Github

Installing Apache-tomcat and Jenkins

- We will be using apache-tomcat to deploy/host our Jenkins ,as its is preferable for managing traffic      in server .
- As the default Jenkins is a light weight server so it's not suggestible for server .  

Install Apache-tomcat:

- You can download apache-tomcat  .tar file from the following link :

- Extract the apache-tomcat.tar.gz file

Install Jenkins:

- Download jenkins.war file from the following url :

- Extract the jenkins.war file 

Run Jenkins using apache-tomcat :

- Now copy the  extracted jenkins to your apache-tomcat's "webapps" folder.
  e.g :
  /apache-tomcat-7.0.76/webapps/(copy your "jenkins" folder here)

- Run your Jenkins :
  - Go to your apache-tomcat's bin folder
    cd   /apache-tomcat-7.0.76/bin
  - run the startup.sh script
    ./startup.sh
    
- Now your Jenkins is up and running ,to check that hit the url 
  http://127.0.0.1:8080/jenkins(if you are trying on local)
http://your-server-ip:8080/jenkins(if you are trying on server)


Setting up your Django application:

Create a virtual environment :
virtualenv your_virtual_environment_name
Activate the virtual environment :
source /path-of-your-virtual-environment/your_virtual_environment_name/bin/activate
Go to your project directory:
cd /your-project-directory-path/YOUR_PROJECT
Bind with Gunicron and Nginx:
(I am expecting you have done the Nginx setup)
For reference to setup Nginx and Gunicron : 

gunicron bind command :

gunicorn project_name.wsgi:application --bind=server-ip:port-no
(e.g : gunicron myproject.wsgi:application --bind=127.0.0.1:8000)
  gunicorn project_name.wsgi:application --bind=domain-name:port-no
Restarting Nginx:
sudo service nginx restart

Convert all the above steps into a single script :

create a "startup.sh" script with the root of your project folder.

cd /your-project-dicrectory/
touch startup.sh
nano startup.sh


your startup.sh should look like :

#!/bin/bash
#activate virtual env
echo Activate vitualenv.
source /path-of-your-venv/virtualenv-name/bin/activate

#restart nginx
echo Restarting Nginx
sudo service nginx restart

# Start Gunicorn processes
echo Starting Gunicorn.
sudo gunicorn project_name.wsgi:application \
    --bind 0.0.0.0:1312 \
    --workers 3 \
    --daemon

Now ctrl+x to save .

Setting up with jenkins :

Access your jenkins from the urls ("http://127.0.0.1:8080/jenkins/") 0r (http://your-server-ip:8080/jenkins/)

It would have shown to copy a password from a location ,copy the password from that location and complete the initial steps.

-Go to the Jenkins server, select the “Manage Jenkins” option and on the next page click on “Manage Plugins”,click on available tab.
- Search for "Git Plugin" or "Gitlab Plugin" as per your requirement .
- Then restart the jenkins by ,http://127.0.0.1:8080/jenkins/restart

Configuring build :
-On Jenkins homepage, click on “New Item” to create a new Job. Write the job name and select the “Freestyle project” option.




- You can give the build's description in "Descriptions"
-Select the checkbox "Discard old build"
-Click on "Advanced" and select "Use custom Workspace" ,on "Directory your can give your own project creation path.




Source Code Management :

- Select "Git" ,On "Repository URL" option provide your repository url .

- On "Credentials",click on "add" and provide the the following details to clone the code from your repository,

     Kind : Username with Password
     Username : your-git-username
     Password  : your-git password

     "click on add"





- Branch Specifier (blank for 'any')   : */master  (this is for from which branch you want to clone)

Build Triggers :

- Select Poll SCM  to set git pulling  timer . click on "?" to see the help .
  example : H/15 * * * *
  (It will pull the code in every 15mins)




Build :
 - Select "Execute Shell"
Commandcd /your-project-path
                      chmod +x start.sh       //Activate the script
                      sudo ./start.sh             //run the script for deployment




- click on "save" 

Go to your system terminal and give jenkins the super user access to run the "startup.sh" script .

cd /etc/
sudo nano sudoers
jenkins    ALL = NOPASSWD: /your-project-path/start.sh  (Add this line)

Back to Jenkins :
Click on "back to dashboard"
- Click on your project 
- Click on "Build Now"




(Hope this will work fine ,If any queries please comment I will try to help)









Comments

  1. if the Jenkins server and the gunicorn servers are in different locations/VMs, how we can achieve it?

    ReplyDelete
  2. Steps are rellay helpful but don't use 'sudo nano sudoers'
    instead use
    'sudo visudo'
    this will take you to /etc/sudoers and upon saving it will make sure that there is no error in formatting.

    if you make an error in sudoer file, you will lose sudo access and it's happened to me, so always use visudo..

    ReplyDelete
  3. i can get the Gunicorn started from startup.sh in Jenkins. But it doesn’t spawn any Gunicorn processes and its does not expose to internet. Means i cant make any API call to it even though the Gunicorn already started from Jenkins. Then, I tried to use Gunicorn on my Python appa outside of Jenkins, it works and it is expose to internet, and also it spawn Gunicorn processes when run ps -ef from the command line. Why is that though?

    ReplyDelete

Post a Comment