Publish Django to Digital Ocean Practice Notes

Posted by zudo on Thu, 29 Aug 2019 05:07:53 +0200

This article was originally published from January 26, 2018.

This paper contains the general steps to deploy Django code written by Python 3 to Digital Ocean, involving git, pyvenv, mysql, pm2 and other technologies, which can provide some reference for Django deployment.

Before you start deploying, you need to prepare a remote server, a domain name, and resolve this domain name A record to the remote server.

version control

Use Git as a version control tool.

Create a new git account

1. Execute on remote server

$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

2. Generate SSH keys on locally developed mac books

$ cd
$ ssh-keygen

3. Upload the locally generated ssh key pub file to the remote server and add it to the end of the authorization file

$ cd
$ cd .ssh
$ ssh-copy-id -i id_rsa.pub git@<servername.com>

Create a new git repository on the remote server

$ sudo mkdir -p /gitbase/<projectname>
$ sudo chown -R git:git /gitbase
$ su git
$ cd /gitbase/<projectname>
$ git init --bare

Submit local code to remote git server

$ cd ~/projects/<projectname>
$ git init
$ git add .
$ git commit -m 'first commit.'
$ git remote add origin git@<servername.com>:/gitbase/<projectname>
$ git push origin master

Pull code from remote git server to remote server publish directory

$ sudo mkdir -p /deploy/<projectname>
$ sudo chown -R <user>:<user> /deploy/<projectname>
$ cd /deploy/<projectname>
$ git clone git@<servername.com>:/gitbase/<projectname> .

Replace the <user> and <servername.com> above with the actual user name and remote server domain name, as follows.Also available gitee To act as a version control server.More References Git on the Server

Configure the Django runtime environment

Using pyvenv to manage the environment in which python3 runs, python2 code can be escaped to python3 code using python3's lib2to3 module tool.

$ python3 -m lib2to3 -w <mycode.py>

Running environment using python3

$ cd
$ python3 -m venv py3env
$ source py3env/bin/activate
$ cd /deploy/<projectname>
$ pip install -r requirements.txt

Test if the code is working properly, switch to the project root directory, and execute

$ python manage.py migrate
$ python manage.py createsuperuser
$ curl -i localhost:8000

Install and configure mysql-server

Install MySQL

$ sudo apt-get install mysql-client mysql-server

Open mysql-server remote access

$ mysql -uroot -p yourpass
mysql> grant all on *.* to user_name@'%' identified by 'user_password';

Find the line bind-address = 127.0.0.1 and comment it out.

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

Ensure that port 3306 is open

$ sudo netstat -an | grep 3306

Restart mysql service

$ sudo service mysql restart

For more information you can refer to Open Mysql Remote Access Method under Ubuntu.

Synchronize table interfaces and data to remote servers

Using mysql remote user name and password to transfer the backed up data to the remote server, you can either use the mysql command line directly or use visual mysql management tools such as workbench, HeidiSQL, navicat, Sequel, and so on.

Deploying Django code using pm2

Install node and pm2

Download the runnable package of the node linux platform from the node website and install pm2 using npm.

$ cd
$ wget https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-x64.tar.xz
$ xz -d node-v8.9.4-linux-x64.tar.xz
$ tar -xf node-v8.9.4-linux-x64.tar
$ cd node-v8.9.4-linux-x64
$ sudo cp -r {bin,include,lib,share} /usr
$ npm i -g pm2

Configure pm2 run parameters

Switch to the project root directory/deploy/<projectname> and execute

$ touch process.yml
$ vim process.yml

Save the following configuration as process.yml

apps:
  - name: mysite
    script: manage.py
    args: ['runserver', '0.0.0.0:8000']
    cwd: .
    interpreter: ~/py3env/bin/python3

Run pm2

$ pm2 start process.yml

View the results of pm2

$ pm2 status

Test whether the site is accessible

$ curl -i localhost:8000

Use nginx reverse proxy

Confirm that ports 8000 and 80 are turned on, and then test the remote site locally for accessibility. If they are not, troubleshoot the specific reasons.Continue to use nginx to reverse proxy http services on remote servers, map internal site ports to server port 80, map the root directory of corresponding domain names to subdirectories of internal sites, and load static resource files.Specifically, you can refer to the documentation on the Internet, which will not be described in this article.

Topics: node.js git MySQL sudo ssh