Toggle navigation

Django Image Guide(Linux)

Version Django1.11.6

Websoft9 Django stack is a pre-configured, ready to run image for running Django application on Alibaba Cloud.

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

Environment Software(Django1.11.6,Python)
Django applicaton root directory: /data/wwwroot
Example demo program: /data/wwwroot/example (this demo used the sqlite database)

Application Server(Nginx1.12.1,uWSGI2.0.12 )
uwsgi configuration file directory: /etc/uwsgi/apps-available/
nginx configuration file directory: /etc/nginx/sites-available/

Database(MySQL5.7)
MySQL Configuration file: /etc/mongod.conf
MySQL data directory: /etc/mysql/mysql.conf.d/mysqld.cnf

Django’s primary deployment platform is uWSGI, the Python standard for web servers and applications.

After the installation of Image,please verify it

  • Login on Alibaba Cloud console,get you Internet IP Address
  • Open you Chrome or Firefox on your local PC,visit the http://Internet IP Address,you can see the welcome information like “It worked!Congratulations on your first Django-powered page.”,if there no response from browser,please check the Security Group settings to ensure that port 80 is been allowed

Two usernames and passwords are required for application installation, application using, and application maintenance:

1、MySQL Database:usename and password is root/123456

2、Linux Server:Username is root or ubuntu,Password is set by youself when buying.
Please Using the Putty for ssh remote and WinSCP for SFTP

Note:You can reset the password of OS through Cloud Console if you have forgotten it

Connect to a Linux instance using Windows OS

This section uses PuTTY as an example. PuTTY can be downloaded here.

You can connect to a Linux instance via PuTTY as follows:

  1. Start Putty.exe.
  2. Enter the public IP address of the instance in Host Name (or IP address).
  3. Use the default port 22.
  4. Select SSH as Connection Type.
  5. Type a session name in Saved Sessions, and then click Save. In later logins, you may directly load the session without re-entering the IP address.
  6. Click Open to connect.
  7. Upon first connection, the following dialog box will be displayed. Click Yes.
  8. As prompted, enter the username and password for the Linux ECS instance. The password will not be displayed on-screen. Press the Enter key to complete connection to the instance.

When you connect your computer to the Linux instance successfully, you can operate the instance from your computer.

Use SFTP you can mange file,upload and download file,configure Server.

  1. Start WinSCP. Login Dialog will appear.
  2. Then select your File protocol(SFTP) on the dialog
  3. Enter your host name to Host name field, username to User name and password to Password like below
  4. You may want to save your session details to a site so you do not need to type them in every time you want to connect. Press Save button and type site name.
  5. When appear the dialog below,please select the “yes”
  6. You can manane Linux file below
  7. WinSCP can integrate the Putty and transfer the log in information to Putty
  8. Now you can use Putty(Not need to log in) from the menu bar of WinSCP

This stack using the uwsgi for  WebServer and Nginx for reverse proxy.Below is is the common methods for adding new application on the Django Stack:

  1. Add a new folder (assume folder name is “your_app”) to the /data/wwwroot/
  2. Install the dependencies and frameworks required for the installer under the directory /data/wwwroot/your_app
    [root@iZbp1i4rjdsy216yq7lgucZ ~]# cd /data/wwwroot/your_app
    [root@iZbp1i4rjdsy216yq7lgucZ your_app]# pip install -r requirements.txt
  3. Add a new database and the user if necessary(the default account of MySQL is root/123456)
  4. Modify your Django application configuration file(the default name is settings.py most of the time )
  5. After running the command “python manage.py runserver “,you may receive the feedback message “Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.”,it means the application running successfully.
    [root@iZbp1i4rjdsy216yq7lgucZ your_app]# python manage.py runserver
  6. Configure the Webserver: Modify the the configuration file /etc/uwsgi/apps-available/app.ini,below is the example for you:
    [uwsgi]
    plugins       = python
    master        = true
    protocol      = uwsgi
    socket        = 127.0.0.1:8080  //make sure the port is available
    wsgi-file     = /data/wwwroot/example/example/wsgi.py  //make sure the path of wsgi.py is true
    chdir         = /data/wwwroot/example //
    buffer-size   = 8192
    enable-threads= true
    close-on-exec = true
    umask         = 0022
    uid           = www-data
  7. Configure the Nginx: /etc/nginx/sites-available/default,make sure the port is same as uWSGI configuration.
    note:The Nginx confiuration need more details settings for yourself
  8. Restart the services
    systemctl restart uwsgi 
    netstat -anopt |grep uwsgi // show the status of uwsgi

uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C.

https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/uwsgi/

You can use the command to manage the services for Server.Please choose the corresponding command mode according to different types of operating system

  • Ngnix Service
    ~# systemctl restart nginx.service
  • Other Service

If you want use Domain for this application,you should complete below steps:

  • Domain resolution if you want to use (Add an A record to the Internet IP Address of Server from your Domain Control Panel)
  • Modiy the Apache configuration file: /etc/nginx/sites-available/default

    server {
    ...
            listen 80 default_server;
            server_name _;//add you domain here
    ...
    }
    
  • Restart the services

Flowing is some useful command of Django:

//View Django version
root@iZuf65n6gzuzce01ma4usiZ:~# django-admin --version

 

Routine backup (archives) of the database and application setup is essential to ensure failover is smooth.

Backup is based on the server snapshot automatic backup and manual local backup in two ways, both of which have their own advantages and disadvantages, it is recommended to use together

Automatic Backup by Snapshot of Server

IaaS provider have the “snapshot” function for Server, the snapshot is for the server disk. Snapshot tool can record the specified point in time the hard disk data, all backed up, and can achieve a key recovery.

If you have an automatic snapshot of the disk settings, the following scenes you can be more calm face:

  • Work a few days of the results, was mistakenly deleted
  • Hackers damage your site
  • The content has been messed up

Done a snapshot backup, a key to restore to the backup point of time state. That is, on-site reduction, very good function.

Different cloud vendor snapshot settings slightly different, specific reference:

This document is original from Websoft9.In order to ensure the seriousness and usability of the documentation, no one may misappropriate or modify the second distribution. The need for friends reproduced please mark the source, otherwise we reserve the right to give legal action.

We will periodically update the documentation. Please visit the online documentation for the latest version

This document URL: http://en.websoft9.com/xdocs/django-image-guide/
Support & Partnership: http://www.websoft9.com