Deploy Django Application on Alibaba Cloud

Image for post
Image for post

Django is an open-source, Python-based web framework that can be helpful to developers that want to launch web applications in just a few hours. The framework follows the Model View Template (MVT) to build applications. It reduces the complexity of Web development, enabling developers to focus on the task of writing applications. It offers the necessary provisions for site maps, content administration, user authentication, and RSS feeds, among other things. DjangoOne interesting thing to note is that some major, heavy traffic websites use Django because of its ability to quickly and flexibly scale to meet traffic peaks.

This blog post goes over how you can launch and deploy a Django application on Alibaba Cloud. In particular, you will set up and launch an Alibaba Cloud Elastic Compute Service (ECS) instance installed with Linux, then you will install and deploy a Django application on this instance.

Prerequisites

As your first prerequisite, you’ll need to have virtualenv and virtualenv wrapper installed for your Python-based application to create isolated environments for your Python projects. For this, you can do so with the following steps:

  • Install
sudo apt-get install python-pip
  • Install
sudo pip install virtualenv
  • Create a to store your virtualenvs
mkdir ~/.virtualenvs
  • Install
sudo pip install virtualenvwrapper
  • Set to your virtualenv dir
export WORKON_HOME=~/.virtualenvs
  • Set to your virtualenv dir
export WORKON_HOME=~/.virtualenvs
  • Add to

Add this line to the end of so that the commands are loaded.

/usr/local/bin/virtualenvwrapper.sh

Exit and re-open your shell, or reload with the command or source and you are ready to go.

1. Install git.

apt-get install git

2. Install Nginx as a web server so that you can run your application behind it.

Sudo apt-get install nginx

Procedure

Now, it’s time to get on to setting up and launching your Alibaba Cloud ECS instance.

Launching your first Linux instance

Let’s take a quick look at the steps involved in running an ECS instance using the Alibaba Cloud Management Console.

1. Log in to your account and navigate to Elastic Compute Service under the Product & Services section. Click on Overview in the sidebar menu. It displays the list of instances already running. Click on Buy Instance to purchase an instance from any of the regions or go on to the next step to create a new instance.

Image for post
Image for post

2. Click on Instances in the sidebar menu. Select the required region in the Instance List and click on Create Instance in the top right bar.

Image for post
Image for post

3. You will be redirected to the Product Purchase console where you need to select your preferred package, which can either be the Quick Launch option, which happens to offer a special pricing on ECS instances and data transfer, or the Custom Launch option, which features two different pricing models. More specifically, you can opt for either subscription billing, which is paid on a monthly or yearly basis, or pay-as-you-go billing based on your needs. In this tutorial, we have selected Pay-As-You-Go as the billing method.

Image for post
Image for post

4. In this window, select the Datacenter Region and Zone where you wish to launch the ECS instance. If you select the Region but do not select the Zone, then the system will place the instance in a random zone.

Image for post
Image for post

5. Now you need to choose the Instance Type that you wish to create. Select the type of instance Generation from the Generation tabs based on your requirement. Generation types represent different instance types based on the configuration and computing power used.

6. Next, select Network Type to launch your ECS instance. It can be either Classic Network or VPC depending on your needs. In a Classic Network, Alibaba Cloud assigns the IP address in a distributed way. This is suitable for users requiring simple and fast use of ECS. VPC is a logically isolated private network that supports dedicated connections. It is suitable for users comfortable with a more involved network management process.

Image for post
Image for post

7. Now, you need to choose the Operating System. Under each option, a list of different OS versions is available. Select the Ubuntu option.

Image for post
Image for post

8. Choose the type of System Disk from the drop-down menu based on your needs. You can also add more disks to this list by clicking on Add a disk.

9. In the Security Setup section, you can create a password for enhanced security.

Image for post
Image for post

10. In the Purchase Plan section, type the name of your Instance and set the number of instances you want to launch.

Image for post
Image for post

11. Check the configuration details and the total price in the Overview section and click on Buy Now.

12. Click on Activate to confirm your order and launch your instance.

13. Once your instance is launched, you can view it in the console, under the Instances tab.

Image for post
Image for post

Installing and Deploying Django Application

Now that you have created and launched your ECS instance using the Alibaba Cloud Management Console, let’s look at how you can install and deploy a Django application.

1. Log in to your server using a SSH command:

Image for post
Image for post

2. Enter the password.

Image for post
Image for post

3. Set up the environment for deploying the Django application by creating a new virtualenv:

mkvirtualenv DjangoApp

To exit your new virtualenv, use deactivate. Now, you can switch between environments with workon. To load or switch between virtualenvs, use the workon command:

workon DjangoApp

4. Install Django on your current environment.

pip install Django

5. Create a Sample Project by using django-admin command and change directory to the project folder.

django-admin startproject todoAppcd todoApp/

6. Migrate or bootstrap your database.

python manage.py migrate

7. Create a superuser to access admin panel.

python manage.py createsuperuser

8. Once you have set up your user, test your application by running the runserver command which is handled by manage.py.

python manage.py runserver 0.0.0.0:8000

You will see the following running on port 8000.

Image for post
Image for post

Go to , which is your admin panel, where you can manage the application.

Image for post
Image for post

Now put your application behind the web server by using Nginx.

9. Create the Database Schema, and activate the Python environment. You can do so with the following steps:

  • Change directory to Django Project directory
  • Run the following command
python manage.py migrate

10. Collect all the static files, including the CSS and JS files. You can do so with the following steps:

Image for post
Image for post
  • Run the following command to collect all the static files at any particular location.
Python manage.py collectstatic --noinput
  • The developer bears the responsibility of setting the path to the location where all the static files are to be collected.
  • These variables are defined in inside the Project directory.
Image for post
Image for post

11. Install the library and start the server using server. You can do so by implementing the following steps:

pip install uWSG
  • Create an file, which will ne used to deploy your django application.
vim uwsgi.ini
Image for post
Image for post
  • Save it to on the . For a better understanding of how to write an ini file, refer to Quickstart for Python/WSGI applications. Run this command to start your application.
uwsgi uwsgi.ini (your ini file)

12. Change the nginx config file to serve the application.

server   {       listen 80 default_server;                listen [::]:80 default_server ipv6only=on;                server_name localhost;  location /static/   {                    include uwsgi_params;                    alias /root/todoApp/public/;  } location  /    {                    include uwsgi_params;                    uwsgi_pass unix:/tmp/uwsgi.sock; } }

13. Restart nginx and your application is up and running behind nginx at port 80.

Image for post
Image for post

Conclusion

In this post, we discussed how you can deploy a Django application on Alibaba Cloud. To summarize things, the first step involved with this was launching and running an ECS instance with Ubuntu as the operating system. Next was installing and deploying the Django application on this instance. Keep in mind that, as part of the prerequisites to this tutorial, you should be in a position to complete the deployment and have a valid Alibaba Cloud account.

Below is a list of products that are related to this tutorial and would be helpful if you were to deploy a Django application in a real-world production scenario.

Original Source:

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store