Deploy Django on Ubuntu 16.4 LTS

Source: Internet
Author: User
Tags install django virtualenv

Preface

This note aims to simplify the route to deploy DJANGO2.X<2 on Ubuntu-LTS server. It takes about 4 minutes.

Before you start your Ubuntu server, you ' d better the Change your SSH ports to another one, and never use ' 22 '. Because hackers would try many times to access your server. You can switch your SSH port by following scripts:

cd /etc/init.d/sshlssudo# a configuration file like this# in vim mode, type ‘i‘ to insert# annotate the default port setting as ‘# port 22‘# add a new line after ‘port 22‘ as ‘port 23442‘, just an example.# after you‘ve done above thingssudo /etc/init.d/ssh restart
Install instruction
  • Install & Configure MySql Server
    • Install

      sudo apt updatesudo apt-get install mysql-server mysql-client
    • Configure

      service mysql statusservice#restart/stop
    • Initialize Databases

      mysql -u root -pmysql: create database Django_Database_Name default charset utf8 collate utf8_general_ci;# Set default charset as UTF-8 to prevent error in future
  • Install Django
    • Install dependencies:virtualenv (Optional)

      # install virtualenvsudo apt install virtualenvvirtualenv# e.g. Fuckercd Fuckersource# activate virtual-environment
      • Deactivate virtual-environment

        # in virtual-environment root dirdeactivate
    • Install Dependencies:python3 + PIP3

      sudo apt-get install python3sudo apt-get install python3-pip
    • Install Dependencies:uwsgi + Django

      pip3# --user option to prevent Permission Denied Error in future
  • Install Dependency:nginx

    sudo apt-get install nginxsystemctl# check nginx statussudo# test nginxcurl 127.0.0.1
Debug your Site/app
  • Upload your Django files via SFTP, with the same port as ssh
  • Do following jobs:

    source ./bin/activatepython3  manage.py makemigrationspython3  manage.py migratepython3  manage.py createsuperuser# set username, pwd, email  python3  manage.py runserver 0.0.0.0:8000# then some error might occur< /span>  
  • Trouble shooting
    • Model Fields Error
      • Check your field setting
      • Check your MySQL charset
    • MYSQL Connection Error
      • Re-configure your MySQL settings in settings.py
      • Checkout your MySQL service status
    • Not allowed IP or so
      • Add your server public IP address to the settings.py in "Allow" like List
    • Makemigrations or migrate do not work well
      • Use following scripts to clear your specific app ' s migrations cache

        python3 manage.py makemigrations --empty YOUR-APP-NAMEpython3 manage.py makemigrationspython3 manage.py migrate        
Configuration
    • Django Daemon Service Configuration

    • Serve Static files If DEBUG ' s been set False

    • Nginx Configuration

Deploy Django on Ubuntu 16.4 LTS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.