SlideShare a Scribd company logo
AWS EC2 Ubuntu Instance
Deploying Python Django on Apache with virtualenv
By: Shahjahan Tapadar, Senior Software Architect
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 2
Contents
Introduction ...................................................................................................................................................................3
Prerequisite....................................................................................................................................................................3
Installing Dependencies.................................................................................................................................................3
Installing Pip and other dependencies ...........................................................................................................................3
Debugging......................................................................................................................................................................4
Install GIT and some required components ...................................................................................................................4
Setting up virtualenv.......................................................................................................................................................5
Install virtualenv wrapper ...............................................................................................................................................5
Creating a virtual environment for the project ................................................................................................................6
Uploading project files and installing dependencies.......................................................................................................6
Download files................................................................................................................................................................6
Install dependencies ......................................................................................................................................................7
Installing and configuring Apache ..................................................................................................................................7
Install apache2 and mod_wsgi.......................................................................................................................................7
Create a virtual host.......................................................................................................................................................8
Create the wsgi file.......................................................................................................................................................10
Static files.....................................................................................................................................................................11
Operations on AWS Instances .....................................................................................................................................12
Operations on Apache/Python /Web Servers...............................................................................................................12
Tuning Apache for Python/Django ...............................................................................................................................12
The Conclusion ............................................................................................................................................................14
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 3
Introduction
This article will help you fetch details about the Ubuntu based AWS EC2 instance. You need to deploy the
Python (2.7) based REST Services in Apache webserver. The core of application is Python DJango
framework, which uses a custom virtual environment (vitualenv). The Apache uses mod_wsgi for
connecting the WSGI application and mod_sec for security purposes.
Deploying Django with Apache and mod_wsgi is a method to get Django into production. mod_wsgi is an
Apache module which is supposed to host any Python WSGI application, which includes Django. Django
can work with any version of Apache that supports mod_wsgi.
Read the article further, to understand the step-by-step deployment process.
Prerequisite
To start with, let us assume that you have an EC2 instance, with fresh Ubuntu server installation, with
Python 2.7 installed. Furthermore, you should be able to have shell access to the server.
Installing Dependencies
Installing Pip and other dependencies
Pip is a tool for installing and managing Python packages. If not installed, you can install pip, easily, via
below command. Make sure that the instances can access the internet to download the required files.
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 4
$ sudo apt-get install python-pip python-dev build-essential
Debugging
You might get warnings about some missing archive files after the command is run. Error messages will
look similar to the ones below:
Err http://eu-central-1a.clouds.archive.ubuntu.com/ubuntu/ trusty-updates/main l ibc-dev-bin amd64 2.19-0ubuntu6.3
404 Not Found [IP: 91.189.92.201 80]
Err http://security.ubuntu.com/ubuntu/ trusty-security/main libc-dev-bin amd64 2 .19-0ubuntu6.3
404 Not Found [IP: 91.189.92.200 80]
If you get similar warnings, run the following command to update the package. This will download the
missing packages.
$ sudo apt-get update
Now run the command again.
$ sudo apt-get install python-pip python-dev build-essential
Then upgrade to latest version.
$ sudo pip install --upgrade pip
The python-dev and build-essential packages are recommended to install along, because it isn't possible
to install any Python module that ships with a C extension without them later on.
Install GIT and some required components
If the code uses Git based code repository, to work with Git repository using command line, you will need
to install git packages. Below command installs required git packages.
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 5
$ sudo apt-get install git libev4 libev-dev python-setuptools libtiff4-
dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev
tcl8.6-dev tk8.6-dev python-tk
Setting up virtualenv
Virtualenv is a tool for creating isolated Python environments. Virtualenv helps you create several
environments that have their own installation directories for setting up private python environments and
their libraries that are specific to your project and its needs. It’s really a neat tool that is extremely
recommended especially on your development machine for dealing with multiple Django projects and
their dependencies. On top of virtualenv, it has virtualenvwrapper, which is basically a set of extensions
that include wrappers, which make it much easier to manage your virtualenvs, such as creating, deleting
and switching between them.
Install virtualenv wrapper
So let’s install virtualenvwrapper via pip, and virtualenv (and its prerequisite) will get fetched and installed,
automatically.
$ sudo pip install virtualenvwrapper
Next
You need to set the location where the virtual environments will be live and the location of the script, you
just installed (virtualenvwrapper.sh), can be accessed later on. To do that, you'll have to add a couple of
lines to the login shell startup file, which is .bash_profile in this case. This file is located within user's
home directory.
Open the file $ sudo vi ~/.bash_profile
Set the following lines:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
Reload the startup file
$ source ~/.bash_profile
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 6
Now, that the file is loaded, you can access the script which will ease managing our virtual environments.
Creating a virtual environment for the project
To create a new virtual environment for our project, execute.
$ mkvirtualenv myprojectenv --no-site-packages
This creates a new virtual environment on the server, just for our project, without any inherent packages
(thanks to the not-site-packages) option. Myprojectenv, now, has just the essentials Python, and Pip.
 To List out all available virtual environment:
$ ls ~/.virtualenvs
 To know the current virtual environment in use:
$ workon
 To get out of currently loaded virtualenv:
$ deactivate
 To activate virtual environment:
$ workon myprojectenv
Uploading project files and installing dependencies
Now, that the virtual environment is ready, you need to upload your project files to some directory on the
server. The location, we have, is: /mnt/coderepo.
Download files
Now, upload your project files to your server via git to /mnt/coderepo
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 7
$ sudo git clone https://github.com/myproject/myproject.git
You will be prompted to provide the username and password.
Install dependencies
After the upload, navigate to your upload directory.
$ cd /mnt/coderepo/myproject
Make sure you're working on the virtualenv of that particular project, if not.
$ workon myprojectenv
Install the dependencies in requirements.txt via pip.
$ sudo pip install -r requirements.txt
Installing and configuring Apache
Now, that the project files are in place and the python requirements installed, you need to set up apache
to execute a .wsgi file that will launch our django application.
Install apache2 and mod_wsgi
Install apache2 and some required components
$ sudo apt-get install apache2 apache2-data apache2-bin apache2-mpm-
prefork apache2-utils libexpat1
Now, let’s install mod_wsgi, which enables you to serve python web applications from Apache server. It is
also one of the recommended ways of getting Django into production.
$ sudo apt-get install libapache2-mod-wsgi
After installing, make sure you restart apache for the changes to take effect.
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 8
$ sudo service apache2 restart
ERROR SCENERIO:
If you get the following errors:
AH00557: apache2: apr_sockaddr_info_get() failed for ip-172-32-20-11
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using
127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Run the following command.
$ echo "ServerName localhost" | sudo tee /etc/apache2/conf-
available/fqdn.conf
$ sudo a2enconf fqdn
Create a virtual host
Open up or create the virtualhost file for your domain.
$ sudo nano /etc/apache2/sites-available/www.myproject.com.conf
$ sudo vi /etc/apache2/sites-available/www.myproject.com.conf
Create your new virtual host node which should look something like this. <ip of the server> in the xml
should be replaced with the server’s ip.
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 9
A couple of things to observe here:
 WSGIScriptAlias denotes the location of wsgi file that is going to get executed when our domain
gets accessed. This file will have some Python code which will run our Django application as
you'll see later. The other alias denotes the location of the static files for my django project. These
contents of the static folder will contain all static files belonging to all the applications within my
django project. The content will later be generated via the collectstatic command.
 You will also notice that I have put those files away from my home folder.
/mnt/webapp/www.myproject.com. It is just a convention which I prefer, especially, for static
content being under the "document root".
Enable site
$ sudo a2ensite www.myproject.com.conf
To activate the new configuration, you need to run:
$ sudo service apache2 reload
This might show an error as below:
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 10
ERROR : Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in
the server configuration
To fix this enable wsgi mod in Apache by using the following command
$ sudo a2enmod wsgi
If you come across below error.
ERROR: Module mod-wsgi does not exist!
You will have to install mod wsgi as below. What you have to do is run the following commands,
$ sudo apt-get install libapache2-mod-wsgi
$ sudo a2enmod wsgi
$ sudo service apache2 restart
Create the wsgi file
Now that apache points to /mnt/webapp/www.myproject.com/index.wsgi you need to create that
file.
Create folder www.myproject.com inside mnt/webapp.
$ sudo nano /mnt/webapp/www.myproject.com/index.wsgi
Your Python code in that file should be similar to this
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 11
site.addsitedir() will let you use the site packages and dependencies within the virtualenv you
created initially for your specific project with your specific packages. sys.path.append() adds your
project's directory to the Python path. It is advised you add both the main directory holding your
applications and its child that contains the settings.py file which in this case is " MyProject".
Static files
Now, you need to set our static files directory and generate the static files. Navigate to your settings.py
file in your project and update the following settings:
$ sudo nano /mnt/coderepo/myproject/MyProjectRoot/MyProject/settings.py
Add the following lines.
STATIC_ROOT = '/mnt/webapp/www.myproject.com/static/'
STATIC_URL = '/static/'
Make sure that you have created the static folder. Now, within MyProjectRoot execute collectstatic
$ sudo python manage.py collectstatic
Everything should be set now. Restart apache for the changes on your virtual host to take effect and
everything should work well.
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 12
$ sudo service apache2 restart
Basically, that’s all what it takes to deploy a django project on a vanilla Ubuntu server.
Operations on AWS Instances
Operations on Apache/Python /Web Servers
1. Start Apache Server
$ sudo service apache2 start
2. Stop Apache Server
$ sudo service apache2 stop
3. Restart Apache Server
$ sudo service apache2 restart
4. Locations of files
a. Apache configuration: /etc/apache2/apache2.conf
b. Apache Log: /var/log/apache2/access.log
c. Apache Error Log: /var/log/apache2/error.log
Tuning Apache for Python/Django
Apache works fantastically when it comes to hosting Python web applications, provided it is configured
properly.
Let's go through the steps, one by one, to properly setup Apache and mod_wsgi.
Step 1: Remove unnecessary modules
The first thing to do is to remove all the unnecessary apache modules that has been installed and are
being loaded at runtime. At the end, only these modules should be enabled:
mod_alias.so
mod_authz_host.so
mod_deflate.so
mod_mime.so
mod_negotiation.so
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 13
mod_rewrite.so
mod_wsgi.so
Step 2: Use Apache MPM worker
On UNIX systems, there are two main MPMs that are used. These are the prefork MPM and the worker
MPM. The prefork MPM implements a multi process configuration where each process is single threaded.
The worker MPM implements a multi process configuration but where each process is multi-threaded.
On Ubuntu, you need to follow these steps:
sudo apt-get purge apache2-mpm-prefork
sudo apt-get install apache2-mpm-worker apache2-threaded-dev
Step 3: KeepAlive On
KeepAlive: Whether or not to allow persistent connections (more than one request per connection). Set to
"On" for better request handling.
KeepAlive can be turned off when you are not serving the static files with same server, where On mode is
more beneficial.
Step 4: Daemon Mode of mod_wsgi:
It's best to set up mod_wsgi in daemon mode + mpm-worker and properly configure the MPM settings.
Step 5: Tweaking mpm-worker configuration
Below configuration is for example purpose only. You need to get your own configuration based on
system resources (cores, memory etc.) and usage patterns.
<IfModule mpm_worker_module>
StartServers 10
MinSpareThreads 10
MaxSpareThreads 25
ThreadLimit 25
ThreadsPerChild 25
MaxClients 75
MaxRequestsPerChild 0
</IfModule>
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 14
The Conclusion
Ubuntu Server is lean, fast and powerful. Its services are reliable, predictable and economical. It is the
perfect base on which you can build your instances. Django is a web framework which is written in
Python. One can easily guess that everything, in Django, is also done in Python. Django was developed
to simplify the creation of database driven sites. The best feature in Django is that it, probably, is the
fastest website framework to create a fully functioning website.
AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv
© RapidValue Solutions 15
About RapidValue
RapidValue is a leading provider of end-to-end mobility, omnichannel and cloud solutions to enterprises
worldwide. Armed with a large team of experts in consulting, UX design and application engineering,
along with experience delivering global projects, we offer a range of services across various industry
verticals. RapidValue delivers its services to the world’s top brands and Fortune 1000 companies, and
has offices in the United States and India.
Disclaimer:
This document contains information that is confidential and proprietary to RapidValue Solutions Inc. No part of it may be used,
circulated, quoted, or reproduced for distribution outside RapidValue. If you are not the intended recipient of this report, you are
hereby notified that the use, circulation, quoting, or reproducing of this report is strictly prohibited and may be unlawful.
© RapidValue Solutions
www.rapidvaluesolutions.com/blogwww.rapidvaluesolutions.com
+1 877.643.1850 contactus@rapidvaluesolutions.com

More Related Content

What's hot (20)

PDF
JAX London 2015: Java vs Nodejs
Chris Bailey
 
PDF
Maveryx presentation
Maveryx
 
PDF
InterConnect2016: WebApp Architectures with Java and Node.js
Chris Bailey
 
PPT
Java 6 [Mustang] - Features and Enchantments
Pavel Kaminsky
 
DOCX
Api webservice setupinstructions
Shivaling Sannalli
 
PPTX
10 practices that every developer needs to start right now
Caleb Jenkins
 
PDF
Windmill Testing certification
Vskills
 
DOCX
Keyword driven testing in qtp
TrainigHubin India
 
ODP
EmbbededGF@JavaOneHyd
Prasad Subramanian
 
PDF
Integration testing - A&BP CC
JWORKS powered by Ordina
 
PDF
End to-end testing from rookie to pro
Domenico Gemoli
 
PDF
Breaking Bad CSP
Lukas Weichselbaum
 
PPTX
Maven basics
Vijay Krishnan Ramaswamy
 
PDF
Glassfish JEE Server Administration - Clustering
Danairat Thanabodithammachari
 
PDF
Selenium Handbook
Suresh Thammishetty
 
PPT
High Availability of Azure Applications
Mindfire Solutions
 
PDF
Selenium C# - The Essential Test Automation Guide
RapidValue
 
PDF
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
VMware Tanzu
 
PDF
HDinsight Workshop - Prerequisite Activity
Idan Tohami
 
PDF
Continuous Deployment at Etsy — TimesOpen NYC
Mike Brittain
 
JAX London 2015: Java vs Nodejs
Chris Bailey
 
Maveryx presentation
Maveryx
 
InterConnect2016: WebApp Architectures with Java and Node.js
Chris Bailey
 
Java 6 [Mustang] - Features and Enchantments
Pavel Kaminsky
 
Api webservice setupinstructions
Shivaling Sannalli
 
10 practices that every developer needs to start right now
Caleb Jenkins
 
Windmill Testing certification
Vskills
 
Keyword driven testing in qtp
TrainigHubin India
 
EmbbededGF@JavaOneHyd
Prasad Subramanian
 
Integration testing - A&BP CC
JWORKS powered by Ordina
 
End to-end testing from rookie to pro
Domenico Gemoli
 
Breaking Bad CSP
Lukas Weichselbaum
 
Glassfish JEE Server Administration - Clustering
Danairat Thanabodithammachari
 
Selenium Handbook
Suresh Thammishetty
 
High Availability of Azure Applications
Mindfire Solutions
 
Selenium C# - The Essential Test Automation Guide
RapidValue
 
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
VMware Tanzu
 
HDinsight Workshop - Prerequisite Activity
Idan Tohami
 
Continuous Deployment at Etsy — TimesOpen NYC
Mike Brittain
 

Similar to AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide (20)

ODP
Virtualenv
Jon Nials
 
PDF
Docker module 1
Liang Bo
 
ODP
5 minute intro to virtualenv
amenasse
 
PDF
Omaha (Google Update) server
Dmitry Lyfar
 
PDF
PVS-Studio in the Clouds: Travis CI
Andrey Karpov
 
PPTX
Pwnstaller
Will Schroeder
 
PDF
Princeton RSE: Building Python Packages (+binary)
Henry Schreiner
 
PDF
Arbeiten mit distribute, pip und virtualenv
Markus Zapke-Gründemann
 
PDF
Virtual environment in python on windows / linux os
deepalishinkar1
 
PDF
Installing OpenCV 4 on Ubuntu 18.x
Nader Karimi
 
PDF
Kayobe_desc
ssuser8fea38
 
PDF
Developing and Deploying PHP with Docker
Patrick Mizer
 
PDF
Deployment automation
Riccardo Lemmi
 
PDF
Automate drupal deployments with linux containers, docker and vagrant
Ricardo Amaro
 
PDF
Django district pip, virtualenv, virtualenv wrapper & more
Jacqueline Kazil
 
PDF
Drupalcamp es 2013 drupal with lxc docker and vagrant
Ricardo Amaro
 
PDF
Docker intro
Frei Zhang
 
PDF
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Fabrice Bernhard
 
PPTX
PHP on Heroku: Deploying and Scaling Apps in the Cloud
Salesforce Developers
 
PPT
Nuxeo5 - Continuous Integration
PASCAL Jean Marie
 
Virtualenv
Jon Nials
 
Docker module 1
Liang Bo
 
5 minute intro to virtualenv
amenasse
 
Omaha (Google Update) server
Dmitry Lyfar
 
PVS-Studio in the Clouds: Travis CI
Andrey Karpov
 
Pwnstaller
Will Schroeder
 
Princeton RSE: Building Python Packages (+binary)
Henry Schreiner
 
Arbeiten mit distribute, pip und virtualenv
Markus Zapke-Gründemann
 
Virtual environment in python on windows / linux os
deepalishinkar1
 
Installing OpenCV 4 on Ubuntu 18.x
Nader Karimi
 
Kayobe_desc
ssuser8fea38
 
Developing and Deploying PHP with Docker
Patrick Mizer
 
Deployment automation
Riccardo Lemmi
 
Automate drupal deployments with linux containers, docker and vagrant
Ricardo Amaro
 
Django district pip, virtualenv, virtualenv wrapper & more
Jacqueline Kazil
 
Drupalcamp es 2013 drupal with lxc docker and vagrant
Ricardo Amaro
 
Docker intro
Frei Zhang
 
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Fabrice Bernhard
 
PHP on Heroku: Deploying and Scaling Apps in the Cloud
Salesforce Developers
 
Nuxeo5 - Continuous Integration
PASCAL Jean Marie
 
Ad

More from RapidValue (20)

PDF
How to Build a Micro-Application using Single-Spa
RapidValue
 
PDF
Play with Jenkins Pipeline
RapidValue
 
PDF
Accessibility Testing using Axe
RapidValue
 
PDF
Guide to Generate Extent Report in Kotlin
RapidValue
 
PDF
Automation in Digital Cloud Labs
RapidValue
 
PDF
Microservices Architecture - Top Trends & Key Business Benefits
RapidValue
 
PDF
Uploading Data Using Oracle Web ADI
RapidValue
 
PDF
Appium Automation with Kotlin
RapidValue
 
PDF
Build UI of the Future with React 360
RapidValue
 
PDF
Python Google Cloud Function with CORS
RapidValue
 
PDF
Real-time Automation Result in Slack Channel
RapidValue
 
PDF
Automation Testing with KATALON Cucumber BDD
RapidValue
 
PDF
How to Implement Micro Frontend Architecture using Angular Framework
RapidValue
 
PDF
Video Recording of Selenium Automation Flows
RapidValue
 
PDF
JMeter JMX Script Creation via BlazeMeter
RapidValue
 
PDF
Migration to Extent Report 4
RapidValue
 
PDF
The Definitive Guide to Implementing Shift Left Testing in QA
RapidValue
 
PDF
Data Seeding via Parameterized API Requests
RapidValue
 
PDF
Test Case Creation in Katalon Studio
RapidValue
 
PDF
How to Perform Memory Leak Test Using Valgrind
RapidValue
 
How to Build a Micro-Application using Single-Spa
RapidValue
 
Play with Jenkins Pipeline
RapidValue
 
Accessibility Testing using Axe
RapidValue
 
Guide to Generate Extent Report in Kotlin
RapidValue
 
Automation in Digital Cloud Labs
RapidValue
 
Microservices Architecture - Top Trends & Key Business Benefits
RapidValue
 
Uploading Data Using Oracle Web ADI
RapidValue
 
Appium Automation with Kotlin
RapidValue
 
Build UI of the Future with React 360
RapidValue
 
Python Google Cloud Function with CORS
RapidValue
 
Real-time Automation Result in Slack Channel
RapidValue
 
Automation Testing with KATALON Cucumber BDD
RapidValue
 
How to Implement Micro Frontend Architecture using Angular Framework
RapidValue
 
Video Recording of Selenium Automation Flows
RapidValue
 
JMeter JMX Script Creation via BlazeMeter
RapidValue
 
Migration to Extent Report 4
RapidValue
 
The Definitive Guide to Implementing Shift Left Testing in QA
RapidValue
 
Data Seeding via Parameterized API Requests
RapidValue
 
Test Case Creation in Katalon Studio
RapidValue
 
How to Perform Memory Leak Test Using Valgrind
RapidValue
 
Ad

Recently uploaded (20)

PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
PDF
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PDF
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Kubernetes - Architecture & Components.pdf
geethak285
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 

AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide

  • 1. AWS EC2 Ubuntu Instance Deploying Python Django on Apache with virtualenv By: Shahjahan Tapadar, Senior Software Architect
  • 2. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 2 Contents Introduction ...................................................................................................................................................................3 Prerequisite....................................................................................................................................................................3 Installing Dependencies.................................................................................................................................................3 Installing Pip and other dependencies ...........................................................................................................................3 Debugging......................................................................................................................................................................4 Install GIT and some required components ...................................................................................................................4 Setting up virtualenv.......................................................................................................................................................5 Install virtualenv wrapper ...............................................................................................................................................5 Creating a virtual environment for the project ................................................................................................................6 Uploading project files and installing dependencies.......................................................................................................6 Download files................................................................................................................................................................6 Install dependencies ......................................................................................................................................................7 Installing and configuring Apache ..................................................................................................................................7 Install apache2 and mod_wsgi.......................................................................................................................................7 Create a virtual host.......................................................................................................................................................8 Create the wsgi file.......................................................................................................................................................10 Static files.....................................................................................................................................................................11 Operations on AWS Instances .....................................................................................................................................12 Operations on Apache/Python /Web Servers...............................................................................................................12 Tuning Apache for Python/Django ...............................................................................................................................12 The Conclusion ............................................................................................................................................................14
  • 3. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 3 Introduction This article will help you fetch details about the Ubuntu based AWS EC2 instance. You need to deploy the Python (2.7) based REST Services in Apache webserver. The core of application is Python DJango framework, which uses a custom virtual environment (vitualenv). The Apache uses mod_wsgi for connecting the WSGI application and mod_sec for security purposes. Deploying Django with Apache and mod_wsgi is a method to get Django into production. mod_wsgi is an Apache module which is supposed to host any Python WSGI application, which includes Django. Django can work with any version of Apache that supports mod_wsgi. Read the article further, to understand the step-by-step deployment process. Prerequisite To start with, let us assume that you have an EC2 instance, with fresh Ubuntu server installation, with Python 2.7 installed. Furthermore, you should be able to have shell access to the server. Installing Dependencies Installing Pip and other dependencies Pip is a tool for installing and managing Python packages. If not installed, you can install pip, easily, via below command. Make sure that the instances can access the internet to download the required files.
  • 4. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 4 $ sudo apt-get install python-pip python-dev build-essential Debugging You might get warnings about some missing archive files after the command is run. Error messages will look similar to the ones below: Err http://eu-central-1a.clouds.archive.ubuntu.com/ubuntu/ trusty-updates/main l ibc-dev-bin amd64 2.19-0ubuntu6.3 404 Not Found [IP: 91.189.92.201 80] Err http://security.ubuntu.com/ubuntu/ trusty-security/main libc-dev-bin amd64 2 .19-0ubuntu6.3 404 Not Found [IP: 91.189.92.200 80] If you get similar warnings, run the following command to update the package. This will download the missing packages. $ sudo apt-get update Now run the command again. $ sudo apt-get install python-pip python-dev build-essential Then upgrade to latest version. $ sudo pip install --upgrade pip The python-dev and build-essential packages are recommended to install along, because it isn't possible to install any Python module that ships with a C extension without them later on. Install GIT and some required components If the code uses Git based code repository, to work with Git repository using command line, you will need to install git packages. Below command installs required git packages.
  • 5. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 5 $ sudo apt-get install git libev4 libev-dev python-setuptools libtiff4- dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk Setting up virtualenv Virtualenv is a tool for creating isolated Python environments. Virtualenv helps you create several environments that have their own installation directories for setting up private python environments and their libraries that are specific to your project and its needs. It’s really a neat tool that is extremely recommended especially on your development machine for dealing with multiple Django projects and their dependencies. On top of virtualenv, it has virtualenvwrapper, which is basically a set of extensions that include wrappers, which make it much easier to manage your virtualenvs, such as creating, deleting and switching between them. Install virtualenv wrapper So let’s install virtualenvwrapper via pip, and virtualenv (and its prerequisite) will get fetched and installed, automatically. $ sudo pip install virtualenvwrapper Next You need to set the location where the virtual environments will be live and the location of the script, you just installed (virtualenvwrapper.sh), can be accessed later on. To do that, you'll have to add a couple of lines to the login shell startup file, which is .bash_profile in this case. This file is located within user's home directory. Open the file $ sudo vi ~/.bash_profile Set the following lines: export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh Reload the startup file $ source ~/.bash_profile
  • 6. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 6 Now, that the file is loaded, you can access the script which will ease managing our virtual environments. Creating a virtual environment for the project To create a new virtual environment for our project, execute. $ mkvirtualenv myprojectenv --no-site-packages This creates a new virtual environment on the server, just for our project, without any inherent packages (thanks to the not-site-packages) option. Myprojectenv, now, has just the essentials Python, and Pip.  To List out all available virtual environment: $ ls ~/.virtualenvs  To know the current virtual environment in use: $ workon  To get out of currently loaded virtualenv: $ deactivate  To activate virtual environment: $ workon myprojectenv Uploading project files and installing dependencies Now, that the virtual environment is ready, you need to upload your project files to some directory on the server. The location, we have, is: /mnt/coderepo. Download files Now, upload your project files to your server via git to /mnt/coderepo
  • 7. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 7 $ sudo git clone https://github.com/myproject/myproject.git You will be prompted to provide the username and password. Install dependencies After the upload, navigate to your upload directory. $ cd /mnt/coderepo/myproject Make sure you're working on the virtualenv of that particular project, if not. $ workon myprojectenv Install the dependencies in requirements.txt via pip. $ sudo pip install -r requirements.txt Installing and configuring Apache Now, that the project files are in place and the python requirements installed, you need to set up apache to execute a .wsgi file that will launch our django application. Install apache2 and mod_wsgi Install apache2 and some required components $ sudo apt-get install apache2 apache2-data apache2-bin apache2-mpm- prefork apache2-utils libexpat1 Now, let’s install mod_wsgi, which enables you to serve python web applications from Apache server. It is also one of the recommended ways of getting Django into production. $ sudo apt-get install libapache2-mod-wsgi After installing, make sure you restart apache for the changes to take effect.
  • 8. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 8 $ sudo service apache2 restart ERROR SCENERIO: If you get the following errors: AH00557: apache2: apr_sockaddr_info_get() failed for ip-172-32-20-11 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message Run the following command. $ echo "ServerName localhost" | sudo tee /etc/apache2/conf- available/fqdn.conf $ sudo a2enconf fqdn Create a virtual host Open up or create the virtualhost file for your domain. $ sudo nano /etc/apache2/sites-available/www.myproject.com.conf $ sudo vi /etc/apache2/sites-available/www.myproject.com.conf Create your new virtual host node which should look something like this. <ip of the server> in the xml should be replaced with the server’s ip.
  • 9. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 9 A couple of things to observe here:  WSGIScriptAlias denotes the location of wsgi file that is going to get executed when our domain gets accessed. This file will have some Python code which will run our Django application as you'll see later. The other alias denotes the location of the static files for my django project. These contents of the static folder will contain all static files belonging to all the applications within my django project. The content will later be generated via the collectstatic command.  You will also notice that I have put those files away from my home folder. /mnt/webapp/www.myproject.com. It is just a convention which I prefer, especially, for static content being under the "document root". Enable site $ sudo a2ensite www.myproject.com.conf To activate the new configuration, you need to run: $ sudo service apache2 reload This might show an error as below:
  • 10. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 10 ERROR : Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configuration To fix this enable wsgi mod in Apache by using the following command $ sudo a2enmod wsgi If you come across below error. ERROR: Module mod-wsgi does not exist! You will have to install mod wsgi as below. What you have to do is run the following commands, $ sudo apt-get install libapache2-mod-wsgi $ sudo a2enmod wsgi $ sudo service apache2 restart Create the wsgi file Now that apache points to /mnt/webapp/www.myproject.com/index.wsgi you need to create that file. Create folder www.myproject.com inside mnt/webapp. $ sudo nano /mnt/webapp/www.myproject.com/index.wsgi Your Python code in that file should be similar to this
  • 11. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 11 site.addsitedir() will let you use the site packages and dependencies within the virtualenv you created initially for your specific project with your specific packages. sys.path.append() adds your project's directory to the Python path. It is advised you add both the main directory holding your applications and its child that contains the settings.py file which in this case is " MyProject". Static files Now, you need to set our static files directory and generate the static files. Navigate to your settings.py file in your project and update the following settings: $ sudo nano /mnt/coderepo/myproject/MyProjectRoot/MyProject/settings.py Add the following lines. STATIC_ROOT = '/mnt/webapp/www.myproject.com/static/' STATIC_URL = '/static/' Make sure that you have created the static folder. Now, within MyProjectRoot execute collectstatic $ sudo python manage.py collectstatic Everything should be set now. Restart apache for the changes on your virtual host to take effect and everything should work well.
  • 12. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 12 $ sudo service apache2 restart Basically, that’s all what it takes to deploy a django project on a vanilla Ubuntu server. Operations on AWS Instances Operations on Apache/Python /Web Servers 1. Start Apache Server $ sudo service apache2 start 2. Stop Apache Server $ sudo service apache2 stop 3. Restart Apache Server $ sudo service apache2 restart 4. Locations of files a. Apache configuration: /etc/apache2/apache2.conf b. Apache Log: /var/log/apache2/access.log c. Apache Error Log: /var/log/apache2/error.log Tuning Apache for Python/Django Apache works fantastically when it comes to hosting Python web applications, provided it is configured properly. Let's go through the steps, one by one, to properly setup Apache and mod_wsgi. Step 1: Remove unnecessary modules The first thing to do is to remove all the unnecessary apache modules that has been installed and are being loaded at runtime. At the end, only these modules should be enabled: mod_alias.so mod_authz_host.so mod_deflate.so mod_mime.so mod_negotiation.so
  • 13. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 13 mod_rewrite.so mod_wsgi.so Step 2: Use Apache MPM worker On UNIX systems, there are two main MPMs that are used. These are the prefork MPM and the worker MPM. The prefork MPM implements a multi process configuration where each process is single threaded. The worker MPM implements a multi process configuration but where each process is multi-threaded. On Ubuntu, you need to follow these steps: sudo apt-get purge apache2-mpm-prefork sudo apt-get install apache2-mpm-worker apache2-threaded-dev Step 3: KeepAlive On KeepAlive: Whether or not to allow persistent connections (more than one request per connection). Set to "On" for better request handling. KeepAlive can be turned off when you are not serving the static files with same server, where On mode is more beneficial. Step 4: Daemon Mode of mod_wsgi: It's best to set up mod_wsgi in daemon mode + mpm-worker and properly configure the MPM settings. Step 5: Tweaking mpm-worker configuration Below configuration is for example purpose only. You need to get your own configuration based on system resources (cores, memory etc.) and usage patterns. <IfModule mpm_worker_module> StartServers 10 MinSpareThreads 10 MaxSpareThreads 25 ThreadLimit 25 ThreadsPerChild 25 MaxClients 75 MaxRequestsPerChild 0 </IfModule>
  • 14. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 14 The Conclusion Ubuntu Server is lean, fast and powerful. Its services are reliable, predictable and economical. It is the perfect base on which you can build your instances. Django is a web framework which is written in Python. One can easily guess that everything, in Django, is also done in Python. Django was developed to simplify the creation of database driven sites. The best feature in Django is that it, probably, is the fastest website framework to create a fully functioning website.
  • 15. AWS EC2 Ubuntu Instance - Deploying Python Django on Apache with virtualenv © RapidValue Solutions 15 About RapidValue RapidValue is a leading provider of end-to-end mobility, omnichannel and cloud solutions to enterprises worldwide. Armed with a large team of experts in consulting, UX design and application engineering, along with experience delivering global projects, we offer a range of services across various industry verticals. RapidValue delivers its services to the world’s top brands and Fortune 1000 companies, and has offices in the United States and India. Disclaimer: This document contains information that is confidential and proprietary to RapidValue Solutions Inc. No part of it may be used, circulated, quoted, or reproduced for distribution outside RapidValue. If you are not the intended recipient of this report, you are hereby notified that the use, circulation, quoting, or reproducing of this report is strictly prohibited and may be unlawful. © RapidValue Solutions www.rapidvaluesolutions.com/blogwww.rapidvaluesolutions.com +1 877.643.1850 [email protected]