Django online deployment Tutorial: Tencent cloud + Ubuntu + Django + Uwsgi (reprint)

Site name: east of notebook
paper links: https://www.eastnotes.com/post/29
Disclaimer: All articles in this blog unless otherwise specified, are used BY-NC-SA license. Please indicate the source!

Django finally successfully deployed to the cloud Tencent, also realized the HTTPS function. Now step method, the deployment environment enumerated below, for future viewing.

Website source code has been open source on github: https: //github.com/mxdshr/DjangoEast, welcome to the use of cloning, and give a star ~

1. Environment and deployment tools

  • Tencent cloud
  • Ubuntu Server 16.04.1 LTS 64位
  • Django2.0.8
  • python3.6.7
  • Git
  • Nginx1.10.3
  • Uwsgi
  • Filezilla
    which Filezilla is used to upload files to the server, before I have been trying to install Ubuntu vsftpd, that was set up ftp to transfer files, it is not true, Filezilla log in with the root account is also capable of file transfer, the port is 22.

2. Deployment Steps

Before making deployment, please ensure your Ubuntu is just packed, pure! Also make sure the system login user is root, if not root, use the following command to switch to root.

sudo su

2.1 install python3.6

Since Ubuntu comes python2.7 and python3.5, but this is not the version I need, and therefore need to install python3.6, if you are using a different version of the method is the same. Command is as follows:

apt-get install software-properties-common
add-apt-repository ppa:jonathonf/python-3.6
apt-get update
apt-get install python3.6

After the installation, in a terminal type python version used is 2.7, so we need to make the following changes to the terminal installed by default python3.6 us:

cd /usr/bin rm python3 ln -s python3.6 python3

2.2 Installation pip3

Needless to say pip action, mainly used to manage the installation of various Python package, installation is as follows:

apt-get install python3-pip

Installing only the first step, and Python, we enter the pip in the terminal, or use the system versions. We need to do something to change, with the above principle is the same python.

cd /usr/bin
rm pip3
ln -s pip3.6 pip3

Log back can take effect, then you can use the libraries installed pip3

pip3 install --upgrade pip

I do not know why, after such operation does not take effect immediately, you need to log back into the server about the job. Well, suppose you now log back in, and enter the terminal pip3 is what we want, and now need to do some upgrades to the pip.

pip3 install --upgrade pip

2.3 installed and configured virtual environment virtualenv

We recommend the site to operate in a standalone Python virtual environment, the installation command as follows:

pip3 install virtualenv

Directory where you want to enter a virtual environment, where I chose / home, the name of my virtual environment taken as django, and then create and activate the virtual environment:

virtualenv django
source /home/django/bin/activate

After all, when carrying out the operation, we must remember to activate the virtual environment, installation package python django or operation of manage.py file in django environment.

2.4 install git, download sites

Using Git can easily interoperate with github, help us update the website source code file, installation as follows:

apt-get install git

After installing the need to select a directory to store the program website, here I choose / home / mysite, mysite directory is not the default, you need to create, and then into this directory, use Git pull the site down here upon my open source has to Django blog on github, for example, downloading a web application method is as follows:

git init
git remote add origin https://github.com/mxdshr/DjangoEast.git
git pull origin master

Well, now that we have acquired the site of the program, then we install the required package python Django program

2.5 installation site Operating Environment

Before this, please ensure that you are now in a virtual environment Django:

pip install -r requirements.txt

This time, all the packages python Django program needs to run, have been loaded into your environment, and now the site can not run, we need to install the database.

2.6 Installing MySQL database

Here I chose MySQL5.8 version, installation steps are as follows:

cd /home
wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
dpkg -i mysql-apt-config_0.8.12-1_all.deb
*选择8.0即可*
apt-get update
apt-get install mysql-server

The installation process will prompt you to enter a password, be sure to write a good Ha! After installing you need to create the database, create user and authorization:

mysql -uroot -p
CREATE DATABASE `数据库名` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%';
FLUSH PRIVILEGES;

This time the configuration database we get that done, then, please modify the database configuration program website djangoblog / settings.py in operation after revision finished divided into two cases: - If the database before you want to use, please its introduction MySQL client data (e.g. Navicat). - If you do not have a database, run the following command at the root of the site, create a data table.

python manage.py makemigrations
python manage.py migrate

Well, Django and database has been configured. We can simply run the site, switch to the web root directory and run:

python manage.py runserver 0.0.0.0:80

0.0.0.0:80 represents any ip accessible, so you can use the server to set ip address to access your site. But using only python runserver tool that comes with running a web application is not enough. Etc. We also need Apache or nginx server management software to better run our site, and we also need to install uwsgi.

2.7 Installation uwsgi

Before installing uwsgi we need to install python development kits:

apt-get install python3.6-dev

Then install uwsgi, please exit this django virtual environment prior to installation, we install it in a public environment.

pip3 install uwsgi

After installation, we can use uwsgi to test whether the site can successfully run, use the following command:

uwsgi --http :8888 --chdir /home/mysite --home /home/django --module djangoblog.wsgi:application

--http: used to specify the port, where I specify 8888, please open the port in Tencent cloud security group
--chdir: used to specify the site's root directory
--home: used to specify the address of the virtual environment
--module: with Django project to specify your address
and then we go to http in the address: // your server ip address: 8888, you can see the page no css styles. The reason why you do not see the style, because uwsgi only provide dynamic link service, if you need to install access static files nginx:

2.8 install nginx

apt-get install nginx

After installing nginx we need to do some configuration, but first we need to know what are the nginx configuration file:

主配置文件:/etc/nginx/nginx.conf一般不用动
可用配置文件:/etc/nginx/sites-available我们一般更改这里的配置文件
已用配置文件:/etc/nginx/sites-enabled,已经启用的配置文件 我们在可用配置文件目录,新建一个mysite.conf此文件用于配置我们的项目。
cd /etc/nginx/sites-available
vim mysite.conf

Then write in the file following configuration:

upstream django {
    server 127.0.0.1:8888; # for a web port socket (we'll use this first)
}

server{
    listen 80;
    server_name mysite;
    charset utf-8;
    client_max_body_size 75M;  #上传文件大小限制

    # 网站静态文件所在目录
    location /static{
        alias /home/mysite/static;
    }
    
    # 上传文件所在目录
    location /media{
        alias /home/mysite/media;
    }
    
    # 动态文件交给uwsgi处理
    location / {
        uwsgi_pass 127.0.0.1:8888;
        include /etc/nginx/uwsgi_params;
    }
}

Then this file and then create a soft link sites-enabled and indicates that the profile is enabled:

ln -s /etc/nginx/sites-available/mysite.conf   /etc/nginx/sites-enabled/mysite.conf

In addition, we also need to start nginx instead of the user root, or else 403 forbidden error occurs. /Etc/nginx/nginx.conf open file, the user behind the user in the first row of root can be changed.

2.9 Configuration uwsgi

Before we simply test it uwsgi, but has not configure it. Now we need to be configured, first create a mysite_uwsgi directory under / home directory, and then create a mysite.ini file in the directory for saving uwsgi configuration options.

cd /home
mkdir mysite_uwsgi
cd mysite_uwsgi
vim mysite.ini

Write the following content

[uwsgi]
chdir = /home/mysite
home = /home/django
module = djangoblog.wsgi:application
master = True
processes = 4
max-requests = 5000
harakiri = 60
socket = 127.0.0.1:8888
uid = root
gid = root
pidfile = /home/mysite_uwsgi/master.pid
daemonize = /home/mysite_uwsgi/mysite.log
vacuum = True

Well, now nginx configuration finished, uwsgi also configure finished, we can test whether the site can be successful, then open the file uwsgi with mysite.ini.

uwsgi --ini /home/mysite_uwsgi/mysite.ini 

Uwsgi check whether successful start command:

ps -aux | grep uwsgi

If the result of multiple processes, we can start to see it succeed.

2.10 Configuring HTTPS

The website now basically standard HTTPS, if not, then the browser will prompt you to unsafe websites. Also affect the search engines, so we need to upgrade the site to HTTPS.

I use the cloud Tencent, Tencent cloud can apply for a free TrustAsia TLS RSA CA certificate, the SSL certificate can be used year after the filing of two files you can get a .crt, one is .key. We need these two files with Filezilla reached under / etc / nginx / directory.

Then write /etc/nginx/nginx.conf documents in the http module in the following configuration file, the contents of the http requests to https requests, the domain name into your own website:

server{
    listen 80;
    server_name eastnotes.com www.eastnotes.com;
    rewrite ^(.*) https://$host$1 permanent;
}

Then open /etc/nginx/sites-available/mysite.conf filled at the contents inside:

listen 443 ssl http2;
server_name www.eastnotes.com,eastnotes.com;
ssl_certificate 1_www.eastnotes.com_bundle.crt;
ssl_certificate_key 2_www.eastnotes.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

Restart nginx

nginx -t
service nginx restart

Well, now open the site should see the prefix https, but not enough, we need to be connected to a 301 redirect with the www connect all without the www in / etc / nginx / sites-available location of which add the following code:

if ($host != 'www.eastnotes.com' ) {
    rewrite ^/(.*)$  https://www.eastnotes.com/$1   permanent;
}

At this point, Django online deployment completed ...... TM really struggling ah !!!

3. Common Commands

# 检查nginx配置文件是够有错误

nginx -t

# 重启nginx
service nginx restart

# 查看uwsgi进程
ps -aux | grep uwsgi

# 正常关闭uwsgi进程
uwsgi --stop /home/mysite_uwsgi/master.pid

# 强制关闭全部uwsgi进程
ps -aux | grep uwsgi |awk '{print $2}'|xargs kill -9

# 重新加载uwsgi
uwsgi --reload /home/mysite_uwsgi/master.pid

x lsof -i:9000      #查看端口占用状态​

netstat -lnp| grep 9000  #查看端口占用状态​

kill -9 +PID号

Guess you like

Origin www.cnblogs.com/chenych/p/11075070.html
Recommended