Deploy Django on Ubuntu 16.4 LTS

Posted cmsax

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Deploy Django on Ubuntu 16.4 LTS相关的知识,希望对你有一定的参考价值。

Preface

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

Before you start your Ubuntu server, you‘d better change your ssh port to another one, never use ‘22‘. Because hackers will try many times to access your server. You can switch your ssh port by following scripts:

cd /etc/init.d/ssh
ls
sudo vim sshd_config # 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 things
sudo /etc/init.d/ssh restart

Install Instruction

  • Install & Configure mysql Server
    • Install

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

      service mysql status
      service mysql start #restart/stop
    • Initialize Databases

      mysql -u root -p
      mysql: 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 virtualenv
      sudo apt install virtualenv
      virtualenv Your_Django_App_Root_Name # e.g. Fucker
      cd Fucker
      source ./bin/activate # activate virtual-environment
      • Deactivate virtual-environment

        # in virtual-environment root dir
        deactivate
    • Install Dependencies: Python3 + pip3

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

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

    sudo apt-get install nginx
    systemctl nginx status # check nginx status
    sudo apt install curl # test nginx
    curl 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/activate
    python3 manage.py makemigrations
    python3 manage.py migrate
    python3 manage.py createsuperuser
    # set username, pwd, email
    python3 manage.py runserver 0.0.0.0:8000
    # then some error might occur
  • 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-NAME
        python3 manage.py makemigrations
        python3 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的主要内容,如果未能解决你的问题,请参考以下文章

Django + uWSGI 通过 Ubuntu 11.10 上的 NGINX

在 AWS Ubuntu EC2 上部署 Django 时缺少模块

ImportError: No module named mysql.base, in django project on Ubuntu 11.04 server

错误:Someip:port 不是有效的端口号或地址:端口对

How To Deploy OpenShift Container Platform 4.8 on KVM

How To Deploy OpenShift Container Platform 4.8 on KVM