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
      ```bash
      # 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

posted @ 2018-05-08 20:11  小鑫同学  阅读(108)  评论(0编辑  收藏  举报

© 2018 追梦网 | Created by Cai M$ | 博客园 | 推荐使用 Microsoft EdgeWindows 10