SlideShare a Scribd company logo
1 of 15
Download to read offline
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

JAX London 2015: Java vs Nodejs
JAX London 2015: Java vs NodejsJAX London 2015: Java vs Nodejs
JAX London 2015: Java vs NodejsChris Bailey
 
Maveryx presentation
Maveryx presentationMaveryx presentation
Maveryx presentationMaveryx
 
InterConnect2016: WebApp Architectures with Java and Node.js
InterConnect2016: WebApp Architectures with Java and Node.jsInterConnect2016: WebApp Architectures with Java and Node.js
InterConnect2016: WebApp Architectures with Java and Node.jsChris Bailey
 
Java 6 [Mustang] - Features and Enchantments
Java 6 [Mustang] - Features and Enchantments Java 6 [Mustang] - Features and Enchantments
Java 6 [Mustang] - Features and Enchantments Pavel Kaminsky
 
Api webservice setupinstructions
Api webservice setupinstructionsApi webservice setupinstructions
Api webservice setupinstructionsShivaling Sannalli
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right nowCaleb Jenkins
 
Windmill Testing certification
Windmill Testing certificationWindmill Testing certification
Windmill Testing certificationVskills
 
End to-end testing from rookie to pro
End to-end testing  from rookie to proEnd to-end testing  from rookie to pro
End to-end testing from rookie to proDomenico Gemoli
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure ApplicationsMindfire Solutions
 
Selenium C# - The Essential Test Automation Guide
Selenium C# - The Essential Test Automation GuideSelenium C# - The Essential Test Automation Guide
Selenium C# - The Essential Test Automation GuideRapidValue
 
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...VMware Tanzu
 
HDinsight Workshop - Prerequisite Activity
HDinsight Workshop - Prerequisite ActivityHDinsight Workshop - Prerequisite Activity
HDinsight Workshop - Prerequisite ActivityIdan Tohami
 
Continuous Deployment at Etsy — TimesOpen NYC
Continuous Deployment at Etsy — TimesOpen NYCContinuous Deployment at Etsy — TimesOpen NYC
Continuous Deployment at Etsy — TimesOpen NYCMike Brittain
 

What's hot (20)

JAX London 2015: Java vs Nodejs
JAX London 2015: Java vs NodejsJAX London 2015: Java vs Nodejs
JAX London 2015: Java vs Nodejs
 
Maveryx presentation
Maveryx presentationMaveryx presentation
Maveryx presentation
 
InterConnect2016: WebApp Architectures with Java and Node.js
InterConnect2016: WebApp Architectures with Java and Node.jsInterConnect2016: WebApp Architectures with Java and Node.js
InterConnect2016: WebApp Architectures with Java and Node.js
 
Java 6 [Mustang] - Features and Enchantments
Java 6 [Mustang] - Features and Enchantments Java 6 [Mustang] - Features and Enchantments
Java 6 [Mustang] - Features and Enchantments
 
Api webservice setupinstructions
Api webservice setupinstructionsApi webservice setupinstructions
Api webservice setupinstructions
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right now
 
Windmill Testing certification
Windmill Testing certificationWindmill Testing certification
Windmill Testing certification
 
Keyword driven testing in qtp
Keyword driven testing in qtpKeyword driven testing in qtp
Keyword driven testing in qtp
 
EmbbededGF@JavaOneHyd
EmbbededGF@JavaOneHydEmbbededGF@JavaOneHyd
EmbbededGF@JavaOneHyd
 
Integration testing - A&BP CC
Integration testing - A&BP CCIntegration testing - A&BP CC
Integration testing - A&BP CC
 
End to-end testing from rookie to pro
End to-end testing  from rookie to proEnd to-end testing  from rookie to pro
End to-end testing from rookie to pro
 
Breaking Bad CSP
Breaking Bad CSPBreaking Bad CSP
Breaking Bad CSP
 
Maven basics
Maven basicsMaven basics
Maven basics
 
Glassfish JEE Server Administration - Clustering
Glassfish JEE Server Administration - ClusteringGlassfish JEE Server Administration - Clustering
Glassfish JEE Server Administration - Clustering
 
Selenium Handbook
Selenium HandbookSelenium Handbook
Selenium Handbook
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
Selenium C# - The Essential Test Automation Guide
Selenium C# - The Essential Test Automation GuideSelenium C# - The Essential Test Automation Guide
Selenium C# - The Essential Test Automation Guide
 
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
 
HDinsight Workshop - Prerequisite Activity
HDinsight Workshop - Prerequisite ActivityHDinsight Workshop - Prerequisite Activity
HDinsight Workshop - Prerequisite Activity
 
Continuous Deployment at Etsy — TimesOpen NYC
Continuous Deployment at Etsy — TimesOpen NYCContinuous Deployment at Etsy — TimesOpen NYC
Continuous Deployment at Etsy — TimesOpen NYC
 

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

Docker module 1
Docker module 1Docker module 1
Docker module 1Liang Bo
 
5 minute intro to virtualenv
5 minute intro to virtualenv5 minute intro to virtualenv
5 minute intro to virtualenvamenasse
 
Omaha (Google Update) server
Omaha (Google Update) serverOmaha (Google Update) server
Omaha (Google Update) serverDmitry Lyfar
 
PVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CIPVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CIAndrey Karpov
 
Arbeiten mit distribute, pip und virtualenv
Arbeiten mit distribute, pip und virtualenvArbeiten mit distribute, pip und virtualenv
Arbeiten mit distribute, pip und virtualenvMarkus Zapke-Gründemann
 
Installing OpenCV 4 on Ubuntu 18.x
Installing OpenCV 4 on Ubuntu 18.xInstalling OpenCV 4 on Ubuntu 18.x
Installing OpenCV 4 on Ubuntu 18.xNader Karimi
 
Developing and Deploying PHP with Docker
Developing and Deploying PHP with DockerDeveloping and Deploying PHP with Docker
Developing and Deploying PHP with DockerPatrick Mizer
 
Automate drupal deployments with linux containers, docker and vagrant
Automate drupal deployments with linux containers, docker and vagrant Automate drupal deployments with linux containers, docker and vagrant
Automate drupal deployments with linux containers, docker and vagrant Ricardo Amaro
 
Django district pip, virtualenv, virtualenv wrapper & more
Django district  pip, virtualenv, virtualenv wrapper & moreDjango district  pip, virtualenv, virtualenv wrapper & more
Django district pip, virtualenv, virtualenv wrapper & moreJacqueline Kazil
 
Drupalcamp es 2013 drupal with lxc docker and vagrant
Drupalcamp es 2013  drupal with lxc docker and vagrant Drupalcamp es 2013  drupal with lxc docker and vagrant
Drupalcamp es 2013 drupal with lxc docker and vagrant Ricardo Amaro
 
DevNet Associate : Python introduction
DevNet Associate : Python introductionDevNet Associate : Python introduction
DevNet Associate : Python introductionJoel W. King
 
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Fabrice Bernhard
 
PHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the CloudPHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the CloudSalesforce Developers
 
Nuxeo5 - Continuous Integration
Nuxeo5 - Continuous IntegrationNuxeo5 - Continuous Integration
Nuxeo5 - Continuous IntegrationPASCAL Jean Marie
 
How to deploy PHP projects with docker
How to deploy PHP projects with dockerHow to deploy PHP projects with docker
How to deploy PHP projects with dockerRuoshi Ling
 

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

Virtualenv
VirtualenvVirtualenv
Virtualenv
 
Docker module 1
Docker module 1Docker module 1
Docker module 1
 
5 minute intro to virtualenv
5 minute intro to virtualenv5 minute intro to virtualenv
5 minute intro to virtualenv
 
Omaha (Google Update) server
Omaha (Google Update) serverOmaha (Google Update) server
Omaha (Google Update) server
 
PVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CIPVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CI
 
Pwnstaller
PwnstallerPwnstaller
Pwnstaller
 
Arbeiten mit distribute, pip und virtualenv
Arbeiten mit distribute, pip und virtualenvArbeiten mit distribute, pip und virtualenv
Arbeiten mit distribute, pip und virtualenv
 
Installing OpenCV 4 on Ubuntu 18.x
Installing OpenCV 4 on Ubuntu 18.xInstalling OpenCV 4 on Ubuntu 18.x
Installing OpenCV 4 on Ubuntu 18.x
 
Kayobe_desc
Kayobe_descKayobe_desc
Kayobe_desc
 
Developing and Deploying PHP with Docker
Developing and Deploying PHP with DockerDeveloping and Deploying PHP with Docker
Developing and Deploying PHP with Docker
 
Deployment automation
Deployment automationDeployment automation
Deployment automation
 
Automate drupal deployments with linux containers, docker and vagrant
Automate drupal deployments with linux containers, docker and vagrant Automate drupal deployments with linux containers, docker and vagrant
Automate drupal deployments with linux containers, docker and vagrant
 
Django district pip, virtualenv, virtualenv wrapper & more
Django district  pip, virtualenv, virtualenv wrapper & moreDjango district  pip, virtualenv, virtualenv wrapper & more
Django district pip, virtualenv, virtualenv wrapper & more
 
Drupalcamp es 2013 drupal with lxc docker and vagrant
Drupalcamp es 2013  drupal with lxc docker and vagrant Drupalcamp es 2013  drupal with lxc docker and vagrant
Drupalcamp es 2013 drupal with lxc docker and vagrant
 
DevNet Associate : Python introduction
DevNet Associate : Python introductionDevNet Associate : Python introduction
DevNet Associate : Python introduction
 
Docker intro
Docker introDocker intro
Docker intro
 
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
 
PHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the CloudPHP on Heroku: Deploying and Scaling Apps in the Cloud
PHP on Heroku: Deploying and Scaling Apps in the Cloud
 
Nuxeo5 - Continuous Integration
Nuxeo5 - Continuous IntegrationNuxeo5 - Continuous Integration
Nuxeo5 - Continuous Integration
 
How to deploy PHP projects with docker
How to deploy PHP projects with dockerHow to deploy PHP projects with docker
How to deploy PHP projects with docker
 

More from RapidValue

How to Build a Micro-Application using Single-Spa
How to Build a Micro-Application using Single-SpaHow to Build a Micro-Application using Single-Spa
How to Build a Micro-Application using Single-SpaRapidValue
 
Play with Jenkins Pipeline
Play with Jenkins PipelinePlay with Jenkins Pipeline
Play with Jenkins PipelineRapidValue
 
Accessibility Testing using Axe
Accessibility Testing using AxeAccessibility Testing using Axe
Accessibility Testing using AxeRapidValue
 
Guide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in KotlinGuide to Generate Extent Report in Kotlin
Guide to Generate Extent Report in KotlinRapidValue
 
Automation in Digital Cloud Labs
Automation in Digital Cloud LabsAutomation in Digital Cloud Labs
Automation in Digital Cloud LabsRapidValue
 
Microservices Architecture - Top Trends & Key Business Benefits
Microservices Architecture -  Top Trends & Key Business BenefitsMicroservices Architecture -  Top Trends & Key Business Benefits
Microservices Architecture - Top Trends & Key Business BenefitsRapidValue
 
Uploading Data Using Oracle Web ADI
Uploading Data Using Oracle Web ADIUploading Data Using Oracle Web ADI
Uploading Data Using Oracle Web ADIRapidValue
 
Appium Automation with Kotlin
Appium Automation with KotlinAppium Automation with Kotlin
Appium Automation with KotlinRapidValue
 
Build UI of the Future with React 360
Build UI of the Future with React 360Build UI of the Future with React 360
Build UI of the Future with React 360RapidValue
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORSRapidValue
 
Real-time Automation Result in Slack Channel
Real-time Automation Result in Slack ChannelReal-time Automation Result in Slack Channel
Real-time Automation Result in Slack ChannelRapidValue
 
Automation Testing with KATALON Cucumber BDD
Automation Testing with KATALON Cucumber BDDAutomation Testing with KATALON Cucumber BDD
Automation Testing with KATALON Cucumber BDDRapidValue
 
How to Implement Micro Frontend Architecture using Angular Framework
How to Implement Micro Frontend Architecture using Angular FrameworkHow to Implement Micro Frontend Architecture using Angular Framework
How to Implement Micro Frontend Architecture using Angular FrameworkRapidValue
 
Video Recording of Selenium Automation Flows
Video Recording of Selenium Automation FlowsVideo Recording of Selenium Automation Flows
Video Recording of Selenium Automation FlowsRapidValue
 
JMeter JMX Script Creation via BlazeMeter
JMeter JMX Script Creation via BlazeMeterJMeter JMX Script Creation via BlazeMeter
JMeter JMX Script Creation via BlazeMeterRapidValue
 
Migration to Extent Report 4
Migration to Extent Report 4Migration to Extent Report 4
Migration to Extent Report 4RapidValue
 
The Definitive Guide to Implementing Shift Left Testing in QA
The Definitive Guide to Implementing Shift Left Testing in QAThe Definitive Guide to Implementing Shift Left Testing in QA
The Definitive Guide to Implementing Shift Left Testing in QARapidValue
 
Data Seeding via Parameterized API Requests
Data Seeding via Parameterized API RequestsData Seeding via Parameterized API Requests
Data Seeding via Parameterized API RequestsRapidValue
 
Test Case Creation in Katalon Studio
Test Case Creation in Katalon StudioTest Case Creation in Katalon Studio
Test Case Creation in Katalon StudioRapidValue
 
How to Perform Memory Leak Test Using Valgrind
How to Perform Memory Leak Test Using ValgrindHow to Perform Memory Leak Test Using Valgrind
How to Perform Memory Leak Test Using ValgrindRapidValue
 

More from RapidValue (20)

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

Recently uploaded

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

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 contactus@rapidvaluesolutions.com