Ubuntu Nginx Gunicorn Django 腾讯云 部署

第一步 创建root用户及使用

创建root用户:

sudo passwd root //会让你输入当前用户密码。输入按下回车输入两次root密码

使用root用户:

su root //提示输入root密码。输入即可

 第二步: 进入root账号更新系统

root@VM-0-16-ubuntu:~# sudo apt update # 回车,执行

更新完软件源,更新软件,大概300多M。

root@VM-0-16-ubuntu:~# sudo apt upgrade  # 回车,执行

 更新完毕后立即重启系统:

root@VM-0-16-ubuntu:~# shutdown -r now

第三步:安装Nginx

root@VM-0-16-ubuntu:~# sudo apt install nginx -y

上面这个命令也可以不用sudo,因为本身就是在root账户下操作的。

root@VM-0-16-ubuntu:~# ps aux |grep nginx
### 以下为输出,可以看到安装完毕后,nginx服务就自动启动了
root      2634  0.0  0.0 141112  1548 ?        Ss   18:08   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  2635  0.0  0.3 143788  6304 ?        S    18:08   0:00 nginx: worker process
root      2959  0.0  0.0  13772  1108 pts/0    S+   18:09   0:00 grep --color=auto nginx

在浏览器中输入服务器公网IP地址49.233.111.205,就可以看到以下页面,说明NGINX已经可以响应Web请求。

 第四步:安装 MySQL

打开图中的网址,点击里面的红色圈圈,下载

root@VM-0-16-ubuntu:~# wget https://repo.mysql.com//mysql-apt-config_0.8.14-1_all.deb
root@VM-0-16-ubuntu:~# ls
mysql-apt-config_0.8.14-1_all.deb  #下载下来就是这样一个deb包
root@VM-0-16-ubuntu:~#

安装这个deb包

root@VM-0-16-ubuntu:~# sudo dpkg -i mysql-apt-config_0.8.14-1_all.deb

回车之后,出来这么个界面,可以看到默认选中 MySQL8.0的server和Cluster:

 如果选择了下面这个OK,如图所示,则直接将MySQL的安装源搞成了默认的8.0的源

点击 上下左右方向键的 “朝右方向键”,则会选中下面这个OK。如图所示:

在这个OK上回车,则出现以下界面:

在这个界面是可以选择安装MySQL5.7的。我们就按照默认的,安装MySQL8.0吧,如下所示:

 在这个状态下,敲回车键就可以设置MySQL的安装源了。

sudo apt update #必须要更新一下源,才能安装mysql8.0,否则就安装5.7了
sudo apt-get install mysql-server
# 最后安装一下这个,因为后面要使用 mysqlclient 这个python驱动
sudo apt-get install libmysqlclient-dev

可以看到MySQLserver的服务进程mysqld已经启动了,但是我发现这安装的是5.7的版本。5.7就5.7吧。 

 接下来,是创建MySQL的连接账户:

登录mysql数据库
输入 mysql -u root -p ,回车输入密码进入数据库;
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
密码? 我也不知道密码是什么呀? 没事的,mysql默认安装时会有一个随机的密码

打开一个文件

sudo vim /etc/mysql/debian.cnf

 看到如下文件(如果提示 sudo vim 找不到命令 原因是因为没有安装vim, 安装一下,  sudo apt install vim)

 在这个文件里面有着MySQL默认的用户名和用户密码,最最重要的是:

 用户名默认的不是root,而是debian-sys-maint,如上图所示

密码会随即给一个很复杂的,这个时候,要进入MySQL的话,就是需要在终端把root更改为debian-sys-maint,如下代码

mysql -u debian-sys-maint -p
Enter password: #输入文件中的密码即可成功登陆(password)

 

在上面的mysql会话控制台更新用户名和密码:

# 使用 ‘mysql’ 这个数据库
mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed

#   查询 plugin 字段,这个输出有可能为空
mysql> select plugin from user where user = 'root';
    +-------------+
    | plugin      |
    +-------------+
    | auth_socket |
    +-------------+
    1 row in set (0.00 sec)

#   更新 plugin 字段为 mysql 默认值:
mysql> update user set plugin='mysql_native_password';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 4  Changed: 1  Warnings: 0

#  更新成功,开始更改密码:
mysql> update user set authentication_string=password('qaz@123.qaz') where user='root' and host='localhost';
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    Rows matched: 1  Changed: 0  Warnings: 1

# 刷新权限:
mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

# 退出 mysql控制台客户端
mysql> \q
    Bye

# 回到Ubuntu系统控制台,重启MySQL服务
ubuntu@VM-0-16-ubuntu:~$ sudo service mysql restart


# 用 root 账号登陆,密码就是前面设置过的 qaz@123.qaz,如果成功进入MySQL的控制台客户端,说明root账号密码创建成功

ubuntu@VM-0-16-ubuntu:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> select Host,User from user;

 配置root远程登陆

mysql> grant all on *.* to root@'%' identified by 'qaz@123.qaz' with grant option;
    Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

mysql> \q
    Bye

ubuntu@VM-0-16-ubuntu:~$ cat /etc/mysql/mysql.conf.d/mysqld.cnf

把上面的侦听地址 127.0.0.1 修改为  0.0.0.0

vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 修改
bind-address = 0.0.0.0

mysql 设置字符集为 utf-8

由于 mysql 的默认字符集为 latin1,不满足我们平常插入数据的需求,我们一般将其改为 utf-8。我们输入如下命令:

 sudo vim /etc/mysql/my.cnf

在文件中添加这样的语句:

[mysqld]
character_set_server = utf8

如果文件中已经存在 [mysqld] 字样,直接添加第二句话在其下方即可。

当然若有时插入数据还是有错,比如数据中有富文本编辑中常用的表情Emoji字符的话,就会出错,这时可以将 utf8 改为 utf8mb4。

接下来重启MySQL服务.

顺便说一下启动,重启,停止,代码如下:

启动mysql:
方式一:sudo /etc/init.d/mysql start
方式二:sudo service mysql start

停止mysql:
方式一:sudo /etc/init.d/mysql stop
方式二:sudo service mysql stop

重启mysql:
方式一:sudo/etc/init.d/mysql restart
方式二:sudo service mysql restart

Navicat 远程连接 

设置服务器安全组,这里简单起见,开放全部端口

设置Navicat远程连接参数 

连接成功后是这样哒

删除MySQL的方法,省的之后再找度娘 

sudo apt-get autoremove --purge mysql-server-5.7
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common

MySQL8.0 的远程登录授权

在MySQL8.0中直接为 root 用户 授予远程登陆访问的权限会失败,如下所示:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> grant all on *.* to root@'%' identified by 'xxx-password' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'qaz@123.qaz' with grant option' at line 1

必须要重新创建一个用户,然后给这个新用户授予远程登陆权限,用Navicat连接时用新用户的账号链接MySQL,操作如下:

# 首先 以 root 账号登陆 MySQL
root@VM-0-16-ubuntu:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 然后创建一个可以用任意IP地址登录的新用户,

mysql> create user 'xmmt'@'%' identified with mysql_native_password by 'your password';
Query OK, 0 rows affected (0.01 sec)

mysql> select host,user from user
    -> ;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | xmmt             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

# 可以看到上面出现了一个用户名为 xmmt 的 host 为 % 的新用户

# 直接这样授权是不行的:
mysql> grant all privileges on . to 'xmmt'@'%' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. to 'xmmt'@'%' with grant option' at line 1

# 应该像下面这样:

mysql> grant all on *.* to 'xmmt'@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

# 接下来退出 root 账号登陆的mysql,改用 xmmt 这个用户登陆
mysql> \q
Bye
root@VM-0-16-ubuntu:~# mysql -u xmmt -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

# 能够成功登陆,说明 MySQL 的 新用户 xmmt 就创建成功了。
# 下面可以使用 windows的Navcat 远程登陆了

创建了新用户,在Navcat 中使用 新用户 登陆,如下:

 用 Navcat 在远程MySQl中创建一个数据库: xmmtdb

然后再终端中验证是否有这个数据库:

好啦,就这样搞定啦!后面在django的settings.py中设置数据库链接的时候,用户名就是 xmmt (不是root), 数据库就是 xmmtdb 啦!

第五步:安装 MiniConda和Python

下载 miniconda:

官网下载 wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh 太慢

在清华镜像源下载:

https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/ ,搜索最新版:Miniconda3-latest-Linux-x86_64.sh,复制链接地址

root@VM-0-16-ubuntu:~# wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
--2019-10-25 09:29:23--  https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 70489432 (67M) [application/octet-stream]
Saving to: ‘Miniconda3-latest-Linux-x86_64.sh’

Miniconda3-latest-Linux-x86_64.sh    100%[=====================================================================>]  67.22M  8.83MB/s    in 7.0s

2019-10-25 09:29:30 (9.66 MB/s) - ‘Miniconda3-latest-Linux-x86_64.sh’ saved [70489432/70489432]
root@VM-0-16-ubuntu:~# sh Miniconda3-latest-Linux-x86_64.sh

    Welcome to Miniconda3 4.7.12

    In order to continue the installation process, please review the license
    agreement.
    Please, press ENTER to continue
>>># 一直回车键

cryptography
    A Python library which exposes cryptographic recipes and primitives.

# 把协议读完,出现这个,写 yes 
Do you accept the license terms? [yes|no]
[no] >>>
Please answer 'yes' or 'no':'

>>> yes

Miniconda3 will now be installed into this location:
/root/miniconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/miniconda3] >>>  # 直接回车

 上面安装miniconda总出现 unpacking pyload 就不往下进行了,那就直接装 Anaconda算了,虽然有些大

root@VM-0-16-ubuntu:~# wget https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh
## 输出
--2019-10-25 09:39:02--  https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-        x86_64.sh
Resolving repo.anaconda.com (repo.anaconda.com)... 104.16.130.3, 104.16.131.3, 2606:4700::6810:8303, ...
Connecting to repo.anaconda.com (repo.anaconda.com)|104.16.130.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 530308481 (506M) [application/x-sh]
Saving to: ‘Anaconda3-2019.10-Linux-x86_64.sh’

Anaconda3-2019.10-Linux-x86_64.sh    100%[=====================================================================>] 505.74M  9.86MB/s    in 52s

2019-10-25 09:39:55 (9.65 MB/s) - ‘Anaconda3-2019.10-Linux-x86_64.sh’ saved [530308481/530308481]

下载完毕后,就开始安装,下面是过程:

# 赋予执行权限
root@VM-0-16-ubuntu:~# chmod +x Anaconda3-2019.10-Linux-x86_64.sh

# 启动安装脚本

root@VM-0-16-ubuntu:~# ./Anaconda3-2019.10-Linux-x86_64.sh

Welcome to Anaconda3 2019.10

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
===================================
Anaconda End User License Agreement
===================================
# 一直回车键,直到出现 

Do you accept the license terms? [yes|no]
[no] >>>
Please answer 'yes' or 'no':'
yes # 敲入 yes

# Anaconda将会被安装到这里 /root/anaconda3
Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> # 敲回车
PREFIX=/root/anaconda3
Unpacking payload ...
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /root/anaconda3
# 下面就开始安装了
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Preparing transaction: done
Executing transaction: done
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>> yes  # 让安装器初始化 conda 的环境变量配置
no change     /root/anaconda3/condabin/conda
no change     /root/anaconda3/bin/conda
no change     /root/anaconda3/bin/conda-env
no change     /root/anaconda3/bin/activate
no change     /root/anaconda3/bin/deactivate
no change     /root/anaconda3/etc/profile.d/conda.sh
no change     /root/anaconda3/etc/fish/conf.d/conda.fish
no change     /root/anaconda3/shell/condabin/Conda.psm1
no change     /root/anaconda3/shell/condabin/conda-hook.ps1
no change     /root/anaconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change     /root/anaconda3/etc/profile.d/conda.csh
modified      /root/.bashrc

==> 要想让新的环境生效,关闭当前终端,重新打开一个 <==

==》 如果你不想每次打开新的终端时,conda 的 base 环境都被激活,那么就执行下面这个命令:

conda config --set auto_activate_base false

感谢你安装 Anaconda3!
=======================打开新的终端之后,进入root账户========================

Last login: Fri Oct 25 09:12:52 2019 from 115.154.34.70
ubuntu@VM-0-16-ubuntu:~$ su root
Password:
(base) root@VM-0-16-ubuntu:/home/ubuntu# cd
(base) root@VM-0-16-ubuntu:~#
发现base环境被自动激活了,所知执行以下命令取消自动激活
conda config --set auto_activate_base false
再次打开新的root终端,就不会有(base)了

安装完之后,我们看到在 unbuntu这个账号下的python版本没有变化,还是Ubuntu18自带的:

Last login: Fri Oct 25 09:52:58 2019 from 115.154.34.70
ubuntu@VM-0-16-ubuntu:~$ python -V
Python 2.7.15+
ubuntu@VM-0-16-ubuntu:~$ python3 -V
Python 3.6.8
ubuntu@VM-0-16-ubuntu:~$

进入 root 账号,再查看下:

root@VM-0-16-ubuntu:~# python -V
Python 2.7.15+
root@VM-0-16-ubuntu:~# python3 -V
Python 3.6.8
root@VM-0-16-ubuntu:~#
这个root  账号下的python版本也没有变化,还是Ubuntu18自带的

输入conda info 命令查看如下:# Python版本是3.7.4

root@VM-0-16-ubuntu:~# conda info

     active environment : None
            shell level : 0
       user config file : /root/.condarc
 populated config files : /root/.condarc
          conda version : 4.7.12
    conda-build version : 3.18.9
         python version : 3.7.4.final.0 # Python版本是3.7.4
       virtual packages :
       base environment : /root/anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /root/anaconda3/pkgs
                          /root/.conda/pkgs
       envs directories : /root/anaconda3/envs
                          /root/.conda/envs
               platform : linux-64
             user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.4 Linux/4.15.0-66-generic ubuntu/18.04.3 glibc/2.27
                UID:GID : 0:0
             netrc file : None
           offline mode : False

root@VM-0-16-ubuntu:~#

创建一个 Python 3.7.4 的 虚拟环境,如下:

# 虚拟环境的名称叫 djg205, 因为打算安装 django 2.0.5 的版本,随便起名
root@VM-0-16-ubuntu:~# conda create -n djg205 python==3.7.4

## 输出
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##
   #虚拟环境中Python解释器的安装位置
  environment location: /root/anaconda3/envs/djg205

  added / updated specs:
    - python==3.7.4


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2019.10.16 |                0         131 KB
    openssl-1.1.1d             |       h7b6447c_3         3.7 MB
    pip-19.3.1                 |           py37_0         1.9 MB
    ------------------------------------------------------------
                                           Total:         5.7 MB

The following NEW packages will be INSTALLED:

  _libgcc_mutex      pkgs/main/linux-64::_libgcc_mutex-0.1-main
  ca-certificates    pkgs/main/linux-64::ca-certificates-2019.10.16-0
  certifi            pkgs/main/linux-64::certifi-2019.9.11-py37_0
  libedit            pkgs/main/linux-64::libedit-3.1.20181209-hc058e9b_0
  libffi             pkgs/main/linux-64::libffi-3.2.1-hd88cf55_4
  libgcc-ng          pkgs/main/linux-64::libgcc-ng-9.1.0-hdf63c60_0
  libstdcxx-ng       pkgs/main/linux-64::libstdcxx-ng-9.1.0-hdf63c60_0
  ncurses            pkgs/main/linux-64::ncurses-6.1-he6710b0_1
  openssl            pkgs/main/linux-64::openssl-1.1.1d-h7b6447c_3
  pip                pkgs/main/linux-64::pip-19.3.1-py37_0
  python             pkgs/main/linux-64::python-3.7.4-h265db76_1
  readline           pkgs/main/linux-64::readline-7.0-h7b6447c_5
  setuptools         pkgs/main/linux-64::setuptools-41.4.0-py37_0
  sqlite             pkgs/main/linux-64::sqlite-3.30.0-h7b6447c_0
  tk                 pkgs/main/linux-64::tk-8.6.8-hbc83047_0
  wheel              pkgs/main/linux-64::wheel-0.33.6-py37_0
  xz                 pkgs/main/linux-64::xz-5.2.4-h14c3975_4
  zlib               pkgs/main/linux-64::zlib-1.2.11-h7b6447c_3


Proceed ([y]/n)? y


Downloading and Extracting Packages
openssl-1.1.1d       | 3.7 MB    | ######################################################################################################## | 100%
ca-certificates-2019 | 131 KB    | ######################################################################################################## | 100%
pip-19.3.1           | 1.9 MB    | ######################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate djg205
#
# To deactivate an active environment, use
#
#     $ conda deactivate

# 安装完虚拟环境后,激活之,然后查看 python版本和pip版本
root@VM-0-16-ubuntu:~# conda activate djg205
(djg205) root@VM-0-16-ubuntu:~# python -V
Python 3.7.4
(djg205) root@VM-0-16-ubuntu:~# pip -V
pip 19.3.1 from /root/anaconda3/envs/djg205/lib/python3.7/site-packages/pip (python 3.7)
(djg205) root@VM-0-16-ubuntu:~#
(djg205) root@VM-0-16-ubuntu:~# pip list
Package    Version
---------- ---------
certifi    2019.9.11
pip        19.3.1
setuptools 41.4.0
wheel      0.33.6
(djg205) root@VM-0-16-ubuntu:~#

Python的虚拟环境管理终于安装好了。

安装依赖项的时候要用到pip,所以修改一下pip镜像源,我们发现在root目录下,腾讯云已经帮我们做好了:

# 如果大家要是没有这个文件,就自己搞一个,把内容填进去
root@VM-0-16-ubuntu:~# cat .pip/pip.conf
[global]
index-url = http://mirrors.tencentyun.com/pypi/simple  #腾讯云镜像,可以换成其他的,阿里源,豆瓣源等
trusted-host = mirrors.tencentyun.com #腾讯云镜像,
root@VM-0-16-ubuntu:~#

 第六步:上传工程代码,安装依赖项,配置数据库,收集静态资源文件

下载一个FileZilla 开源免费速度极快的FTP软件在你的PC上,设置好远程服务器的链接,上传工程代码,我的工程代码文件夹叫PicturesAppVd,Vd就是第d个版本啦。,代码目录在PyCharm中如下所示:

大家注意我的工程文件夹的名字是 PicturesAppVd,而工程内部存放各种配置文件的文件夹是  PicturesApp。传统的Django工程这两个文件夹名称是一样的。只是我把最外面那个文件夹改了个名字而已。后面配置NGINX的静态资源代理的时候注意不要把目录搞混了。

 

如上图所示,我们把工程压缩包 PicturesAppVd.zip 用 FilleZilla 上传到了服务器的 ubuntu 账号下。然后我们再用命令行搬到root\WebProjects\录下,解压开。操作过程如下所示:

root@VM-0-16-ubuntu:~# ls /home/ubuntu/
PicturesAppVd.zip
root@VM-0-16-ubuntu:~# mkdir WebProjects
root@VM-0-16-ubuntu:~# ls
anaconda3  Anaconda3-2019.10-Linux-x86_64.sh  mysql-apt-config_0.8.14-1_all.deb  WebProjects
root@VM-0-16-ubuntu:~# cp /home/ubuntu/PicturesAppVd.zip ./WebProjects/
root@VM-0-16-ubuntu:~# cd WebProjects/
root@VM-0-16-ubuntu:~/WebProjects# ls
PicturesAppVd.zip
root@VM-0-16-ubuntu:~/WebProjects# unzip PicturesAppVd.zip
.......................
.....................
...........
root@VM-0-16-ubuntu:~/WebProjects# ls
PicturesAppVd  PicturesAppVd.zip
root@VM-0-16-ubuntu:~/WebProjects#

 激活之前创建的djg205的虚拟环境,安装requirements.txt文件中的依赖项 

future==0.17.1
httplib2==0.12.0
MarkupSafe==1.1.0
Pygments==2.3.1
pyparsing==2.3.1
pyzmq==17.1.2
mysqlclient==1.4.1
requests==2.21.0
django==2.0.5
django-reversion==3.0.3
django-ckeditor==5.6.1
django-crispy-forms==1.7.2
django-formtools==2.1
XlsxWriter==1.1.2
django-import-export==1.0.0
Markdown==3.0.1
Pillow==5.4.1
django-markdownx==2.0.28
django-pure-pagination==0.3.0
django-simple-captcha==0.5.6
# restful framework
coreapi==2.3.3
markdown==3.0.1
djangorestframework==3.9.1
django-filter==2.1.0
django-guardian==1.5.0
django-cors-headers==2.4.0
djangorestframework-jwt==1.11.0
root@VM-0-16-ubuntu:~/WebProjects# conda activate djg205
(djg205) root@VM-0-16-ubuntu:~/WebProjects# cd PicturesAppVd
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# ls
apps  db_tools  extra_apps  manage.py  media  PicturesApp  requirements_django2.0.5.txt  static  templates
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# pip install -r requirements_django2.0.5.txt

# 修改工程文件中的settings.py文件,创建并迁移数据库,收集 静态文件

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# ls
apps  db_tools  extra_apps  manage.py  media  PicturesApp  requirements_django2.0.5.txt  static  templates
# 修改 settings.py 中的数据库连接配置
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# sudo vim PicturesApp/settings.py
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd#

 

在上图中,点确定后就会在远程服务器创建名叫 picturesvd 的数据库,可以查询一下创建成功没有:

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 864
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| picturesvd         |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use picturesvd;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql>

可以看到数据库 picturesvd 已经创建成功,但是,里面没有数据表,接下来就是迁移数据库的数据表和传输本地数据到远程数据库。

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# python manage.py makemigrations
    No changes detected

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# python manage.py migrate

    Operations to perform:
      Apply all migrations: admin, auth, authtoken, contenttypes, operations, pictures, reversion, sessions, taxonomies, users, xadmin
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0001_initial... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying auth.0009_alter_user_last_name_max_length... OK
      Applying users.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying authtoken.0001_initial... OK
      Applying authtoken.0002_auto_20160226_1747... OK
      Applying taxonomies.0001_initial... OK
      Applying pictures.0001_initial... OK
      Applying operations.0001_initial... OK
      Applying operations.0002_auto_20190420_1039... OK
      Applying pictures.0002_advertise... OK
      Applying pictures.0003_video... OK
      Applying pictures.0004_auto_20191023_0139... OK
      Applying pictures.0005_video_videoid... OK
      Applying reversion.0001_squashed_0004_auto_20160611_1202... OK
      Applying sessions.0001_initial... OK
      Applying taxonomies.0002_auto_20190422_1658... OK
      Applying taxonomies.0003_auto_20190425_1518... OK
      Applying taxonomies.0004_auto_20191022_1452... OK
      Applying xadmin.0001_initial... OK
      Applying xadmin.0002_log... OK
      Applying xadmin.0003_auto_20160715_0100... OK
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd#

可以看到成功执行完数据库表迁移 后,服务器的数据库中也有了数据表。

 但是服务器的数据库中还没有数据,所以用Nvicat进行数据迁移,把本地数据库的数据传输到服务器上。如下所示:

如图所示,在picturesvd数据库上点右键,选择 “数据传输” 菜单项,打开数据传输对话框

如上图所示传输完数据。 

接下来是收集静态资源文件。

在本地开发的调试模式下,setting.py 中的  DEBUG = True,静态资源放在工程目录的 static 目录下,通过下面的配置指定本地服务器找到静态资源文件的路径:

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

而在部署的时候,我们要用nginx的静态资源代理功能,所以要把所有的静态资源文件全部收集到一个文件夹集中存放,不仅仅包括 “static” 这个,还有比如 xadmin和散落在其他地方的静态文件。所以我们在工程目录下再新建一个 文件夹 ,名字随便起,我们就叫 “statics”,加个 's' 以示区别。如下所示:

 接下来,就按照上图的意思修改服务器上的工程目录和代码:

修改前后对比:

然后运行收集静态资源的命令:python  manage.py collectstatic ,一定要实现激活虚拟环境

收集完成之后的效果:

同时,用vim将服务器上 settings.py 文件中的 DEBUG=True 改为  DEBUG = False

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# vim PicturesApp/settings.py
 

第七步:安装 Gunicorn 配置 Nginx, 并用它启动 django 工程

安装 Gunicorn : 先激活虚拟环境 ,然后 pip install gunicorn 就OK了

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# pip install gunicorn
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting gunicorn
  Downloading http://mirrors.tencentyun.com/pypi/packages/8c/da/b8dd8deb741bff556db53902d4706774c8e1e67265f69528c14c003644e6/gunicorn-19.9.0-py2.py3-none-any.whl (112kB)
     |████████████████████████████████| 122kB 706kB/s
Installing collected packages: gunicorn
Successfully installed gunicorn-19.9.0
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# gunicorn -V
usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: unrecognized arguments: -V
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# gunicorn -v
gunicorn (version 19.9.0)

配置 Nginx:先用命令把nginx的默认配置文件备份一下,然后把它copy到工程目录的conf文件夹(自己建一个),开始放心修改它:

# 先建个 conf 文件夹 集中存放各种 配置文件
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# mkdir conf
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# ls
apps  conf  db_tools  extra_apps  manage.py  media  PicturesApp  requirements_django2.0.5.txt  static  statics  templates

# 备份原始的nginx配置文件
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

# 将原始的nginx配置文件拷贝一份到 工程目录下的 conf 文件夹下进行修改
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cp /etc/nginx/nginx.conf ./conf/
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# ls ./conf/
nginx.conf
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cat ./conf/nginx.conf
.........
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd#

原始的 nginx.conf 文件长这样:

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cat ./conf/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

上面这个文件是个主配置文件,而 include 命令包含进来的 /etc/nginx/conf.d/*.conf 下面 默认是空的,啥也没有。主要的服务器站点配置项在 include /etc/nginx/sites-enabled/* 下面的 default 配置文件中,如下所示:这是一个 php 的配置案例

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cat /etc/nginx/sites-enabled/default
##
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

我们修改nginx配置文件的主要思路是:将上面这个文件中 默认的 server 配置项 也 挪动到主配置文件对应的 include 命令处,替换它的位置,然后加入 Django的一些配置:(最好在vscode中编辑好,再上传到服务器的工程目录下的conf文件夹下)。

完整配置文件如下所示:

user root;  # 把这个用户由 www-data 改为 root 用户
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##
        # 这个文件夹下面其实没文件,所以注释掉
        # include /etc/nginx/conf.d/*.conf;  
        # 这个下面有个默认的 default 文件,写着默认服务器配置,把它取出来
        # 放在下边,所以这个也注释掉
        # include /etc/nginx/sites-enabled/*; 
        
        # Nginx会将一般的非静态资源文件的请求转发到下面的django-server,
        # 由django-server来响应一般HTTP请求。‘django_server’ 这个名字是我起的
        upstream django_server {
                # 下面是通过unix文件套接字进行连接,比网络端口方式稍快一点,
                # 这意味着gunicorn要在unix:/tmp/gunicorn.sock启动django服务
                server unix:/tmp/gunicorn.sock fail_timeout=0;
                # 下面是通过网络端口套接字连接,这意味着gunicorn要在8888端口启动django服务
                # server 127.0.0.1:8888; 
        }

        # 服务器配置
        #
        server {
                # 网站对外提供服务的端口号,默认即 80 端口
                listen 80;

                # 网站提供服务的 IP 地址和域名
		        server_name 49.213.15.16 www.xxx.top xxx.top;
		        charset     utf-8;

                # 上传文件大小的最大值
		        client_max_body_size 75M;   # adjust to taste

                # 多媒体资源文件代理, 指向django的media目录
                location /media  {
                        alias /root/WebProjects/PicturesAppVd/media;  
                }

                # 静态资源文件代理, 指向django的statics目录
                location /static {
    			        alias /root/WebProjects/PicturesAppVd/statics; 
		        }

                location / {
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      			        proxy_set_header X-Forwarded-Proto $scheme;
      			        proxy_set_header Host $http_host;
      			        proxy_redirect off;
      			        proxy_pass http://django_server; # 这个名称 django_server 必须与前面一致
                }
        }
}

# 下面的邮件服务代理是默认注释掉的
#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

我们将上面这个文件上传到服务器的工程目录下的conf文件下(命名为 picsapp_nginx_http.conf),然后替换/etc/nginx/nginx.conf 这个默认配置文件,再重启NGINX服务器,操作流程如下:


(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# mv picsapp_nginx_http.conf conf/
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# ls conf/
nginx2.conf  nginx.conf  picsapp_nginx_http.conf

# 替换默认配置文件 /etc/nginx/nginx.conf 
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cp conf/picsapp_nginx_http.conf /etc/nginx/nginx.conf

# 测试一下修改好的 nginx 配置文件是否合语法规范
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 测试通过,重启nginx,用浏览器访问服务器公网IP看,还能不能看到NGinx的欢迎页了。
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# sudo service nginx restart

# 查看 nginx 进程
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# ps aux | grep nginx
root      1690  0.0  0.0  13772  1148 pts/0    S+   17:26   0:00 grep --color=auto nginx
root     32514  0.0  0.0 141144  1556 ?        Ss   17:13   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
root     32519  0.0  0.3 143800  6160 ?        S    17:13   0:00 nginx: worker process
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd#

访问服务器出现这样一个页面。

配置 gunicorn 启动 django 服务 

首先在 工程目录下创建logs日志文件夹,再在下面创建gunicorn文件夹用于存放gunicorn的日志,还可以创建nginx的文件夹存放nginx的日志,如下所示: 

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# mkdir logs
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cd logs/
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd/logs# mkdir gunicorn
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd/logs# pwd
/root/WebProjects/PicturesAppVd/logs
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd/logs# cd gunicorn/
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd/logs/gunicorn# pwd
/root/WebProjects/PicturesAppVd/logs/gunicorn

然后写 gunicorn 的 启动配置文件,如下:

# gunicorn 的启动配置文件是一个.py文件,内容如下所示
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cat conf/gunicorn_config.py
import multiprocessing
chdir = '/root/WebProjects/PicturesAppVd' # 工程目录文件夹
#user= 1000
#group= 1000
umask= 0
initgroups= False
proc_name = 'gunicorn'    #进程的名称
worker_class='sync'
workers = multiprocessing.cpu_count() * 2 + 1  # workers的数量
#workers = 2
threads = 1
bind = ['unix:/tmp/gunicorn.sock'] # 启动django应用的时候绑定的Unix文件套接字,
# bind = [127.0.0.1:8888] # 启动django应用的时候绑定的网络端口套接字,端口与nginx的配置要一致
timeout = 30
Debugging = True # 设置是否开启Debug模式
daemon = False  # 是否以守护进程的模式启动,如果设为True,则运行gunicorn命令后会释放控制台,否则会独占控制台,知道用户ctrl + c 退出进程
loglevel='info' # 记录日志的水平
accesslog='/root/WebProjects/PicturesAppVd/logs/gunicorn/access.log' # 访问日志,可以注释掉
errorlog='/root/WebProjects/PicturesAppVd/logs/gunicorn/error.log'   # 错误日志,可以注释掉

最后,就是用 gunicorn命令启动 Django 应用啦:

# 在工程目录的conf文件夹下创建一个py文件,把上面的内容拷贝到里面,保存退出。
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# vim conf/gunicorn_config.py

# 下面就是用 gunicorn 命令启动Django的应用来响应HTTP请求啦。由于开启了Debug和日志记录,所以可以看到Django应用运行时抛出的一些异常信息
(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# gunicorn -c conf/gunicorn_config.py PicturesApp.wsgi

/root/anaconda3/envs/djg205/lib/python3.7/site-packages/rest_framework/pagination.py:198: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'taxonomies.models.Moter'> QuerySet.
  paginator = self.django_paginator_class(queryset, page_size)
/root/anaconda3/envs/djg205/lib/python3.7/site-packages/rest_framework/pagination.py:198: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'taxonomies.models.Moter'> QuerySet.
  paginator = self.django_paginator_class(queryset, page_size)
/root/anaconda3/envs/djg205/lib/python3.7/site-packages/rest_framework/pagination.py:198: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'taxonomies.models.Moter'> QuerySet.
  paginator = self.django_paginator_class(queryset, page_size)

我们还可以看看日志文件的内容:可以看到我使用火狐浏览器访问了服务器,每次请求都被记录下来了

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cat logs/gunicorn/access.log
 
- - [25/Oct/2019:17:42:35 +0800] "GET / HTTP/1.0" 200 30848 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"
 - - [25/Oct/2019:17:42:40 +0800] "GET /video/ HTTP/1.0" 200 4659 "http://49.xx.xx.xx/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"
 - - [25/Oct/2019:17:43:55 +0800] "GET /publishers-list/ HTTP/1.0" 200 8619 "http://49.xx.xx.xx/video/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"
 - - [25/Oct/2019:17:44:22 +0800] "GET /publisher/2/detail/ HTTP/1.0" 200 14854 "http://49.xxx.xx.xx/publishers-list/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"
 - - [25/Oct/2019:17:44:41 +0800] "GET /publisher/1/detail/ HTTP/1.0" 200 14612 "http://49.xx.xx.xx/publishers-list/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"
 - - [25/Oct/2019:17:45:03 +0800] "GET /albums-list/?area=1 HTTP/1.0" 200 19542 "http://49.xx.xx.xx/publisher/1/detail/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"

下面是 error日志文件,记录的日志水平 是 ‘info’级别的 。进程的开启,子进程的开辟等都在这个日志文件中。

(djg205) root@VM-0-16-ubuntu:~/WebProjects/PicturesAppVd# cat logs/gunicorn/error.log
[2019-10-25 17:41:49 +0800] [3737] [INFO] Starting gunicorn 19.9.0
[2019-10-25 17:41:49 +0800] [3737] [INFO] Listening at: unix:/tmp/gunicorn.sock (3737)
[2019-10-25 17:41:49 +0800] [3737] [INFO] Using worker: sync
[2019-10-25 17:41:49 +0800] [3740] [INFO] Booting worker with pid: 3740
[2019-10-25 17:41:49 +0800] [3741] [INFO] Booting worker with pid: 3741
[2019-10-25 17:41:49 +0800] [3742] [INFO] Booting worker with pid: 3742
[2019-10-25 18:06:11 +0800] [3737] [INFO] Handling signal: int
[2019-10-25 18:06:11 +0800] [3741] [INFO] Worker exiting (pid: 3741)
[2019-10-25 18:06:11 +0800] [3740] [INFO] Worker exiting (pid: 3740)
[2019-10-25 18:06:11 +0800] [3742] [INFO] Worker exiting (pid: 3742)
[2019-10-25 18:06:12 +0800] [3737] [INFO] Shutting down: Master

 

最后我们修改一下 conf/gunicorn_config.py文件, 把Debug改为False, 日志记录也改为 ‘error’ 级别,daemon = True,开启守护进程。

然后启动gunicorn,让Django应用一直开启提供Web服务。如下图所示,nginx和gunicorn都在工作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值