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

转载于:https://www.cnblogs.com/cmsax/p/9010667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值