AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
-
Upload
rapidvalue -
Category
Technology
-
view
896 -
download
2
Transcript of AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
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/blog
www.rapidvaluesolutions.com
+1 877.643.1850