"Amazon Cloud Technology Product Review" activity essay collection_uWSGI deploys Django

Prompt: Authorization statement: This article authorizes the right to forward and rewrite the official Amazon Cloud Technology article, including but not limited to the official Amazon Cloud Technology channels such as Developercentre, Zhihu, We Media Platform, and third-party developer media.

Insert image description here

Article Directory

  • Foreword
    1. Create Lightsail instance
  • II. Django environment construction
    • 2.1 Install the pip
    • 2.2 Install uWSGI environment
    • 2.3 Test whether uWSGI is normal
    • 2.4 Set up security groups to preview the test environment
    1. Deploy and test Django environment
    • 3.1 Install Django
    • 3.2 Create Django
    • 3.3 Allow all hosts to be accessible
    • 3.4 Run the project
    1. Configure Nginx direction proxy
  • Sum up

Foreword

Recently registered a AWS new account, free of charge for 12 months EC2 and 3 months Lightsail, ready to deploy one Django, this article is used to record the whole practice process.

My usual language is Python that Python the support for Web development is very good, and a large number of Web frameworks, such as Django, Flask, Tornado and so on, Making Web development easier and more efficient.

  • Introduction to Django

    Django Is an open source Web application framework, written in the Python language, mainly used to build Web projects. This article describes how to use Nginx+uWSGI deployment Django projects in the Linux server.

1. Create Lightsail instance

It Lightsail provides multiple regional nodes for developers to choose. I choose Tokyo here. It is faster for you to choose a closer access, especially in the production environment. The network is often a major bottleneck of the project. Tokyo also has several available areas to choose from. You can choose different available areas according to your needs. For example, to do some disaster recoveryInsert image description here, because we will use the Nginx reverse proxy in the future, the pattern provided Apps + OS here Lightsail directly creates an instance. We can easily choose the serverInsert image description here with Nginx the program directly, and then choose our configuration according to the size of the program and the business situation. Because I am a new user, I can try it for three months free of charge. Here I chose the configuration of.

Enter the instance ID, and then confirm the number of instances to be created. Click the Create button to complete the creation. After the creation is completed, the server comes with it Nginx. That is to say, one Nginx of the public IP addresses that we directly access the server is running. Let’s try to access it:

II. Django environment construction

Our project needs to be used Python3. Generally, the operating system comes with one Python2.7. Let’s run the command to check:

python3 --version 

The following output shows that the system has been installed Python3 and the version is relatively new.

2.1 Install the pip

Then we check pip whether it comes with us. We need this package management tool when we need to install python dependencies. After checking, we find that it does not.

We need to install one ourselves, and the installation process is as follows:

  • Get pip
wget https://bootstrap.pypa.io/get-pip.py 
  • Install the pip
python3 get-pip.py 

After installation, there is a prompt, although the installation is successful, but the environment variables are not set, or can not be used globally, so we need to set the environment variables.

WARNING: The script wheel is installed in '/home/bitnami/.local/bin' which is not on PATH.  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. 
  • Set environment variable to.bashrc

Put the pip directory path where the executable file is located in.

echo 'export PATH=/home/bitnami/.local/bin:$PATH' >> .bashrc 
  • Put environment variables into effect
source .bashrc 
pip3 --version 

Check pip again whether it can be used globally, and it is normal to see the following output

Insert image description here

2.2 Install uWSGI environment

Because we need to use uWSGI startup Django, we also need to install this to facilitate the startup of the project.

  • Execute the installation command
pip3 install uwsgi 

FoundInsert image description here here in build uwsgi the time reported an error, we need to install python3 the development library.

  • Install python3-dev
sudo apt-get install python3-dev 
  • Reinstall uwsgi
pip3 install uwsgi 

To see this echo is to install successfully.

2.3 Test whether uWSGI is normal

  • Create a test directory
mkdir www
cd www/ 
  • Create a test file
vim index.py 
  • Write the following in the file
def application(env,start_response):        
            start_response('200 ok',[('Content-Type','text/html')])
            return [b"Hello World"] 
  • Start the test project using uwsgi
uwsgi --http 0.0.0.0:8001 --wsgi-file index.py 

Through the public network IP access, I found that I still could not access, and then I thought that the security group had not been set on the instance.

2.4 Set up security groups to preview the test environment

Add a rule in the instance details page networking : TCP 8001 This is our traffic type and traffic port.

Insert image description here Keep visiting IP+8001 again, and find that the test project can be accessed normally, and the relevant information of this request can also be seen on the terminal as expected Hello World Insert image description here.

3. Deploy and test Django environment

3.1 Install Django

pip3 install Django 

3.2 Create Django

After django pip installation, there will be an django-admin executable command, and we will start a project template through the start command.

django-admin startproject my_website 

After execution, you will see a my_website new directory in the current directory.

3.3 Allow all hosts to be accessible

  • Modify the settings configuration
vi my_website/my_website/settings.py 

Insert image description here Set ALLOWED_HOSTS = ["*"] to save and exit

3.4 Run the project

  • Enter the project directory and run
cd my_website
python3 manage.py runserver 0.0.0.0:8002 

A new port 8002 is startedInsert image description here here, and we also need to add an admission ruleInsert image description here in the networking preview again to find that the access has been successful.

4. Configure Nginx direction proxy

Thanks to Lightsail’s own Nginx, we don’t need to install and configure it ourselves. We can use it directly. We use Nginx to reverse proxy to our Django project.

  • Modify the Nginx configuration and add the following forwarding conditions
location /django{
    proxy_pass http://localhost:8002;
} 

This configuration means that when we access IP+/django it, it is forwarded to the local 8002 service.

Insert image description here

  • Modified Reload Configuration
sudo nginx -s reload 

Then directly access the public network IP, do not need to bring the 8002 port, and find that the Django project has been accessed normally.

Insert image description here

This is because django there is no such route. It can be configured in the framework. The terminal we found django also saw the relevant information of this request.

Insert image description here At this point, the project is deployed


Sum up

By building the uWSGI + Django project in Lightsail the example, we found that the whole process was relatively smooth. Then we looked at the resource consumption of the whole process, looked at the console, and went to the Metrics tab bar.

Insert image description here Insert image description here

The CPU usage is very low and the memory is flat, indicating that the configuration python django has little loss to the server. Lightsail’s situational awareness is intuitive and clear.