Django is a Python Web Platform of high standards that promotes fast development and clean, pragmatic design. A description on using uWSGI and nginx on Ubuntu 14.04 is given in this document.
Before You Begin
1. Get acquainted with the starting guide and finalize the steps for determining the hostname and timezone of your cloud server.
2. This guide uses an example of django. Complete securing your Server Guide sections to create a django account, improve access to SSH and disable unwanted network services. With your particular program, you need to build additional firewall rules.
3. update your system: upgrade your system
sudo apt-get update && sudo apt-get upgrade
Install nginx, Python Tools and uWSGI
1. Install the required framework packages for SQLite Python and Python Tools: SQLite Python bindings
sudo apt-get install build-essential nginx python-dev python-pip python-sqlite sqlite
2. Now Install virtualenv and virtualenvwrapper:
sudo pip install virtualenv virtualenvwrapper
Virtualenv and virtualenvwrapper are instruments for building Python environment in isolated environments. These help handle dependencies, versions and permissions easier. To function correctly with virtualenvwrapper, execute the following commands:
echo “export WORKON_HOME=~/Env” >> ~/.bashrc
echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.bashrc
3. Enable virtualenvwrapper in the current session:
4. Using pip to install uWSGI:
sudo pip install uwsgi
Set up a Sample Django Application
1. Make sure you are in the home directory of Django users and build the application’s virtual environment for the application :
cd /home/django && mkvirtualenv sample
Once this command has been executed, your prompt will turn to something like email@example.com which means you are using the virtual setup sample. Type deactivate to exit the virtual environment.
2. Set up the framework for Django:
pip install Django
3. Build a new preview of the Django app on
django-admin.py startproject sample
4. Switch to the directory of the Django application and launch the database of SQLite:
cd ~/sample && ./manage.py migrate
5. It is essential to configure Django when running Django with Nginx to place all the static assets in the
static folder of your application in
settings.py , define your location:
echo ‘STATIC_ROOT = os.path.join(BASE_DIR, “static/”)’ >> sample/settings.py
6. To transfer all static assets in the above directory, run the following command:
7. To check the sample code, open a development server:
./manage.py runserver 0.0.0.0:8080
Visit http:/abc.com:8080 on your computer browser to check the right setup and functionality of the sample program. See the check page for Django:
8. If you will get the message “It Worked,Congratulations on your first Django-powered page”
Then stop the development server with Ctrl-C.
1. Create an uWSGI file settings directory:
sudo mkdir -p /etc/uwsgi/sites
2. Create the
sample.ini configuration file with the contents below:
3. Create an uWSGI upstart job:
In Emperor mode, this job will start uWSGI to track the directory of
/etc/uwsgi/sites and to create instances (vasals) for any configuration file it finds. The Emperor will immediately restart the vassals if a config file is updated.
4. Start the service uwsgi:
sudo service uwsgi start
1. Delete the default nginx site configuration:
sudo rm /etc/nginx/sites-enabled/default
2. Creates your Django application’s nginx site configuration file:
3. To allow your site configuration file, set up a symlink to the nginx
sudo ln -s /etc/nginx/sites-available/sample /etc/nginx/sites-enabled
4. Check and restart nginx settings:
sudo service nginx configtest
sudo service nginx restart
5. By now visiting a hostname or IP address on port 80 you should be able to access your Django program.